functional-php/tests/FunctionPipelineTest.php
2018-07-16 21:47:06 +02:00

95 lines
2.6 KiB
PHP

<?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 FunctionPipelineTest 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(['a', 'b', 'c'])
->getMock();
}
public function testPipelineCalling()
{
$pipeline = f\pipe(
[$this->mock, 'a'],
[$this->mock, 'b']
);
$this->mock->method('a')->willReturn('b');
$this->mock->method('b')->willReturn('c');
$this->mock->expects($this->once())->method('a')->with('a');
$this->mock->expects($this->once())->method('b')->with('b');
$this->assertSame('c', $pipeline('a'));
}
public function testPipelineForward()
{
$pipeline = f\pipe(
[$this->mock, 'a']
);
$this->mock->method('a')->willReturn('b');
$this->mock->expects($this->once())->method('a')->with('a');
$this->assertSame('b', $pipeline('a'));
}
public function testPipelineAppend()
{
$pipeline = f\pipe(
[$this->mock, 'a'],
[$this->mock, 'b']
);
$second = $pipeline->append([$this->mock, 'c']);
$this->mock->method('a')->willReturn('b');
$this->mock->method('b')->willReturn('c');
$this->mock->method('c')->willReturn('d');
$this->mock->expects($this->exactly(2))->method('a')->with('a');
$this->mock->expects($this->exactly(2))->method('b')->with('b');
$this->mock->expects($this->once())->method('c')->with('c');
$this->assertSame('c', $pipeline('a'));
$this->assertSame('d', $second('a'));
}
public function testPipelinePrepend()
{
$pipeline = f\pipe(
[$this->mock, 'b'],
[$this->mock, 'c']
);
$second = $pipeline->prepend([$this->mock, 'a']);
$this->mock->method('a')->willReturn('b');
$this->mock->method('b')->willReturn('c');
$this->mock->method('c')->willReturn('d');
$this->mock->expects($this->exactly(1))->method('a')->with('a');
$this->mock->expects($this->exactly(2))->method('b')->with('b');
$this->mock->expects($this->exactly(2))->method('c')->with('c');
$this->assertSame('d', $pipeline('b'));
$this->assertSame('d', $second('a'));
}
}