<?php
/**
 * Copyright 2018 Kacper Donat
 *
 * @author Kacper "Kadet" Donat <kacper@kadet.net>
 *
 * Full license available in separate LICENSE file
 */

use Kadet\Functional as f;

class MemoizedFunctionTest extends \PHPUnit\Framework\TestCase
{
    /** @var \PHPUnit\Framework\MockObject\MockObject */
    private $mock;

    protected function setUp()/* The :void return type declaration that should be here would cause a BC issue */
    {
        $this->mock = $this
            ->getMockBuilder('stdClass')
            ->setMethods(['memoized'])
            ->getMock();
    }

    public function testCalledOnlyOnce()
    {
        $memoized = f\memoize([$this->mock, 'memoized']);

        $this->mock->expects($this->once())->method('memoized')->with();

        for ($i = 0; $i < 10; $i++) {
            $memoized();
        }
    }

    public function testCalledOnlyOncePerArgs()
    {
        $memoized = f\memoize([$this->mock, 'memoized']);

        $this->mock->expects($this->exactly(2))->method('memoized')->withConsecutive(
            [ 'a' ],
            [ [] ]
        );

        for ($i = 0; $i < 10; $i++) {
            $memoized('a');
            $memoized([]);
        }
    }
}