functional-php/tests/FunctionPipelineTest.php
2026-10-27 22:04:21 +01:00

100 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(): void
{
$this->mock = $this->createMock(PipelineSteps::class);
}
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'));
}
}
class PipelineSteps
{
public function a() {}
public function b() {}
public function c() {}
}