93 lines
1.9 KiB
PHP
93 lines
1.9 KiB
PHP
<?php
|
|
|
|
use function Kadet\Functional\apply;
|
|
use function Kadet\Functional\arify;
|
|
use function Kadet\Functional\ref;
|
|
|
|
/**
|
|
* Copyright 2018 Kacper Donat
|
|
*
|
|
* @author Kacper "Kadet" Donat <kacper@kadet.net>
|
|
*
|
|
* Full license available in separate LICENSE file
|
|
*/
|
|
|
|
class TestObject
|
|
{
|
|
public function identity($x)
|
|
{
|
|
return $x;
|
|
}
|
|
|
|
private function priv()
|
|
{
|
|
return 'foo';
|
|
}
|
|
|
|
public function test()
|
|
{
|
|
return ref([$this, 'priv']);
|
|
}
|
|
}
|
|
|
|
class ReferencingTest extends \PHPUnit\Framework\TestCase
|
|
{
|
|
public function testReferenceClosure()
|
|
{
|
|
$f = function () { };
|
|
|
|
$this->assertSame($f, ref($f));
|
|
}
|
|
|
|
public function testApplicationOfArrayNotation()
|
|
{
|
|
$object = new TestObject();
|
|
$this->assertSame(10, apply([$object, 'identity'], 10));
|
|
}
|
|
|
|
public function testReferencingPrivateFunctionsFromInsideOfObject()
|
|
{
|
|
$object = new TestObject();
|
|
$reference = $object->test();
|
|
|
|
$this->assertSame('foo', $reference());
|
|
}
|
|
|
|
/**
|
|
* @expectedException \Kadet\Functional\Exception\NoAccessException
|
|
*/
|
|
public function testReferencingPrivateFunctionsFromOutsideOfObject()
|
|
{
|
|
$object = new TestObject();
|
|
|
|
ref([$object, 'priv']);
|
|
}
|
|
|
|
public function testSetFunctionArity()
|
|
{
|
|
$f = function (...$args) {
|
|
return count($args);
|
|
};
|
|
|
|
$f = arify($f, 2);
|
|
|
|
$this->assertSame(1, $f(1));
|
|
$this->assertSame(2, $f(1, 2));
|
|
$this->assertSame(2, $f(1, 2, 3));
|
|
$this->assertSame(2, $f(1, 2, 3, 4));
|
|
}
|
|
|
|
public function testReferencingWithSpecificArity()
|
|
{
|
|
$f = function (...$args) {
|
|
return count($args);
|
|
};
|
|
|
|
$f = ref($f, 2);
|
|
|
|
$this->assertSame(1, $f(1));
|
|
$this->assertSame(2, $f(1, 2));
|
|
$this->assertSame(2, $f(1, 2, 3));
|
|
$this->assertSame(2, $f(1, 2, 3, 4));
|
|
}
|
|
} |