-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCallback.php
98 lines (83 loc) · 2.52 KB
/
Callback.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
<?php
namespace Test\Feature;
use Respect\Config\Container;
class ItemConsuptionViaCallback extends \PHPUnit_Framework_TestCase
{
public function testLateDefinitionOfVariableExpansionThroughContainerCallbackReturnsContainer()
{
$c = new Container(<<<INI
foo = [undef]
bar = [foo]
INI
);
$definition = array('undef' => 'Hello');
$result = $c($definition);
$this->assertEquals(
'Hello',
$result->bar,
'Calling the container as a function will append the array passed as content to it.' . PHP_EOL .
'It will return the itself, as well.'
);
$this->assertSame(
$result->bar,
$c->bar,
"But it doesn't matter on which instance of the container you call."
);
}
public function testLateDefinitionOfVariableExpansionThroughItemCallbackReturnsValue()
{
$c = new Container(<<<INI
foo = [undef]
bar = [foo]
INI
);
$result = $c->bar(array('undef'=>'Hello'));
$this->assertEquals('Hello', $result);
}
public function testRetrievalOfItemThroughInstanceTypeOnContainerCallbackReturnsValue()
{
$called = false;
$c = new Container(<<<INI
[instanceof DateTime]
time = now
INI
);
$result = $c(function(\DateTime $date) use (&$called) {
$called = true;
return $date;
});
$result2 = $c['DateTime'];
$this->assertInstanceOf('DateTime', $result);
$this->assertInstanceOf('DateTime', $result2);
$this->assertTrue($called);
}
public function testRetrievalOfInstanceTypeThroughContainerCallbackReturnsValueEvenWithoutDeclaringItsType()
{
$c = new Container();
$c(new \DateTime);
$called = false;
$result = $c(function(\DateTime $date) use (&$called) {
$called = true;
return $date;
});
$result2 = $c['DateTime'];
$this->assertInstanceOf('DateTime', $result);
$this->assertInstanceOf('DateTime', $result2);
$this->assertTrue($called);
}
public function testContainerCallbackReceivingACallableCallsItAndReturnsValue()
{
$c = new Container();
$c(new \DateTime);
$result = $c(array('Test\Stub\TimePrinter', 'returnTimePassedAsArgument'));
$this->assertInstanceOf('DateTime', $result);
}
}
namespace Test\Stub;
class TimePrinter
{
public function returnTimePassedAsArgument(\DateTime $time)
{
return $time;
}
}