forked from slimphp/Slim
/
DeferredCallableTest.php
109 lines (88 loc) · 3.2 KB
/
DeferredCallableTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Slim\CallableResolver;
use Slim\DeferredCallable;
use Slim\Tests\Mocks\CallableTest;
class DeferredCallableTest extends TestCase
{
public function testItResolvesCallable()
{
$containerProphecy = $this->prophesize(ContainerInterface::class);
$containerProphecy->has('CallableTest')->willReturn(true);
$containerProphecy->get('CallableTest')->willReturn(new CallableTest());
$resolver = new CallableResolver($containerProphecy->reveal());
$deferred = new DeferredCallable('CallableTest:toCall', $resolver);
$deferred();
$this->assertEquals(1, CallableTest::$CalledCount);
}
public function testItBindsClosuresToContainer()
{
$containerProphecy = $this->prophesize(ContainerInterface::class);
$resolver = new CallableResolver($containerProphecy->reveal());
$assertCalled = $this->getMockBuilder('StdClass')->setMethods(['foo'])->getMock();
$assertCalled
->expects($this->once())
->method('foo');
$test = $this;
$closure = function () use ($containerProphecy, $test, $assertCalled) {
$assertCalled->foo();
$test->assertSame($containerProphecy->reveal(), $this);
};
$deferred = new DeferredCallable($closure, $resolver);
$deferred();
}
public function testItReturnsInvokedCallableResponse()
{
$containerProphecy = $this->prophesize(ContainerInterface::class);
$resolver = new CallableResolver($containerProphecy->reveal());
$test = $this;
$foo = 'foo';
$bar = 'bar';
$closure = function ($param) use ($test, $foo, $bar) {
$test->assertEquals($foo, $param);
return $bar;
};
$deferred = new DeferredCallable($closure, $resolver);
$response = $deferred($foo);
$this->assertEquals($bar, $response);
}
public function testFunctionNameIfNoResolver()
{
$deferredTrim = new DeferredCallable('trim');
$this->assertSame('foo', $deferredTrim(' foo '));
}
public function testClosureIfNoResolver()
{
$closure = function ($a, $b) {
return $a + $b;
};
$deferredClosure = new DeferredCallable($closure);
$this->assertSame(42, $deferredClosure(31, 11));
}
public static function getFoo(): string
{
return 'foo';
}
public function testClassNameMethodNameNotation()
{
// Test `ClassName::methodName` notation for a static method.
$deferredGetFoo = new DeferredCallable(get_class($this) . '::getFoo');
$this->assertSame('foo', $deferredGetFoo());
}
/**
* @expectedException RuntimeException
* @expectedExceptionMessage Slim callable notation CallableTest:toCall is not allowed without callable resolver.
*/
public function testSlimCallableNotationThrowsExceptionIfNoResolver()
{
new DeferredCallable('CallableTest:toCall');
}
}