* * 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']); } public static function twice($x) { return $x * 2; } } class ReferencingTest extends \PHPUnit\Framework\TestCase { public function testReferenceStaticMethodArrayCallable() { $this->assertSame(4, ref([TestObject::class, 'twice'])(2)); } 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()); } public function testReferencingPrivateFunctionsFromOutsideOfObject() { $this->expectException(\Kadet\Functional\Exception\NoAccessException::class); $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)); } }