82 lines
2.6 KiB
PHP
82 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;
|
|
use const Kadet\Functional\_;
|
|
use Kadet\Functional\Reflection\ReflectionAppliedParameter;
|
|
use Kadet\Functional\Reflection\ReflectionPartiallyAppliedFunction;
|
|
|
|
|
|
class ReflectionPartialAppliedFunctionTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
/**
|
|
* @var \Kadet\Functional\Utils\PartiallyAppliedFunction
|
|
*/
|
|
private $function;
|
|
|
|
protected function setUp()
|
|
{
|
|
$this->function = f\partial(function (array $a, array $b, array $c, array $d = ['d']) {
|
|
return array_merge($a, $b, $c, $d);
|
|
}, ['a'], _, ['c']);
|
|
}
|
|
|
|
public function testNumberOfParameters()
|
|
{
|
|
$reflection = new ReflectionPartiallyAppliedFunction($this->function);
|
|
|
|
$this->assertSame(2, $reflection->getNumberOfParameters());
|
|
}
|
|
|
|
public function testNumberOfRequiredParameters()
|
|
{
|
|
$reflection = new ReflectionPartiallyAppliedFunction($this->function);
|
|
|
|
$this->assertSame(1, $reflection->getNumberOfRequiredParameters());
|
|
}
|
|
|
|
public function testNumberOfAppliedParameters()
|
|
{
|
|
$reflection = new ReflectionPartiallyAppliedFunction($this->function);
|
|
|
|
$this->assertSame(2, $reflection->getNumberOfAppliedParameters());
|
|
}
|
|
|
|
public function testParameters()
|
|
{
|
|
$reflection = new ReflectionPartiallyAppliedFunction($this->function);
|
|
$parameters = $reflection->getParameters();
|
|
|
|
$this->assertContainsOnlyInstancesOf(ReflectionParameter::class, $parameters);
|
|
$this->assertCount(2, $parameters);
|
|
$this->assertEquals([1, 3], array_keys($parameters));
|
|
}
|
|
|
|
public function testAppliedParameters()
|
|
{
|
|
$reflection = new ReflectionPartiallyAppliedFunction($this->function);
|
|
$parameters = $reflection->getAppliedParameters();
|
|
|
|
$this->assertContainsOnlyInstancesOf(ReflectionAppliedParameter::class, $parameters);
|
|
$this->assertCount(2, $parameters);
|
|
$this->assertEquals([0, 2], array_keys($parameters));
|
|
$this->assertEquals([0 => ['a'], 2 => ['c']], array_map(function(ReflectionAppliedParameter $parameter) {
|
|
return $parameter->getValue();
|
|
}, $parameters));
|
|
}
|
|
|
|
public function testIsCurried()
|
|
{
|
|
$reflection = new ReflectionPartiallyAppliedFunction($this->function);
|
|
$this->assertFalse($reflection->isCurried());
|
|
|
|
$curried = new ReflectionPartiallyAppliedFunction(f\curry($this->function->getDecorated()));
|
|
$this->assertTrue($curried->isCurried());
|
|
}
|
|
} |