1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Slick\Di\Definition; |
4
|
|
|
|
5
|
|
|
use PhpSpec\Exception\Example\FailureException; |
6
|
|
|
use PhpSpec\ObjectBehavior; |
7
|
|
|
use Slick\Di\ContainerInterface; |
8
|
|
|
use Slick\Di\Definition\FluentObjectDefinitionInterface; |
9
|
|
|
use Slick\Di\Definition\Object\DefinitionData; |
10
|
|
|
use Slick\Di\Definition\Object\ResolverInterface; |
11
|
|
|
use Slick\Di\Definition\ObjectDefinition; |
12
|
|
|
use Slick\Di\DefinitionInterface; |
13
|
|
|
use Slick\Di\Exception\ClassNotFoundException; |
14
|
|
|
use Slick\Di\Exception\MethodNotFoundException; |
15
|
|
|
use Slick\Di\Exception\PropertyNotFoundException; |
16
|
|
|
|
17
|
|
|
class ObjectDefinitionSpec extends ObjectBehavior |
18
|
|
|
{ |
19
|
|
|
function let() |
20
|
|
|
{ |
21
|
|
|
$this->beConstructedWith(InitializableService::class); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function it_is_initializable() |
25
|
|
|
{ |
26
|
|
|
$this->shouldHaveType(ObjectDefinition::class); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
function its_a_definition() |
30
|
|
|
{ |
31
|
|
|
$this->shouldBeAnInstanceOf(DefinitionInterface::class); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function its_a_fluent_object_definition() |
35
|
|
|
{ |
36
|
|
|
$this->shouldBeAnInstanceOf(FluentObjectDefinitionInterface::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function it_resolves_objects_using_an_object_resolver_interface( |
40
|
|
|
ResolverInterface $resolver, |
41
|
|
|
ContainerInterface $container |
42
|
|
|
) { |
43
|
|
|
$resolver->setContainer($container)->willReturn($resolver); |
44
|
|
|
$resolver->resolve(new DefinitionData(InitializableService::class))->shouldBeCalled(); |
45
|
|
|
$this->setResolver($resolver); |
46
|
|
|
$this->setContainer($container); |
47
|
|
|
$this->resolve(); |
48
|
|
|
$resolver->setContainer($container)->shouldHaveBeenCalled(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
function it_throws_class_not_found_exception_if_class_name_cannot_be_loaded() |
52
|
|
|
{ |
53
|
|
|
$this->shouldThrow(ClassNotFoundException::class) |
54
|
|
|
->during('__construct', ['some\class\Path']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
function it_can_define_constructor_arguments() |
58
|
|
|
{ |
59
|
|
|
$this->with('hello', 'world'); |
60
|
|
|
$this->getDefinitionData()->shouldHaveArgumentsEquals(['hello', 'world']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
function it_can_define_a_method_call_with_arguments() |
64
|
|
|
{ |
65
|
|
|
$this->call('doSomething')->with('hello', 'world'); |
66
|
|
|
$this->getDefinitionData()->shouldHaveACallEquals( |
67
|
|
|
[ |
68
|
|
|
'type' => DefinitionData::METHOD, |
69
|
|
|
'name' => 'doSomething', |
70
|
|
|
'arguments' => ['hello', 'world'] |
71
|
|
|
] |
72
|
|
|
|
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
function it_throws_method_not_found_exception_if_defining_an_undefined_method_call() |
77
|
|
|
{ |
78
|
|
|
$this->beConstructedWith(InitializableService::class); |
79
|
|
|
$this->shouldThrow(MethodNotFoundException::class) |
80
|
|
|
->during('call', ['test']); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function it_can_define_an_assignment_to_a_property() |
84
|
|
|
{ |
85
|
|
|
$this->assign('test')->to('scope'); |
86
|
|
|
$this->getDefinitionData()->shouldHaveACallEquals( |
87
|
|
|
[ |
88
|
|
|
'type' => DefinitionData::PROPERTY, |
89
|
|
|
'name' => 'scope', |
90
|
|
|
'arguments' => 'test' |
91
|
|
|
] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
function it_throws_property_not_found_exception_if_defining_an_undefined_property_call() |
96
|
|
|
{ |
97
|
|
|
$this->beConstructedWith(InitializableService::class); |
98
|
|
|
$this->shouldThrow(PropertyNotFoundException::class) |
99
|
|
|
->during('to', ['test']); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getMatchers() |
103
|
|
|
{ |
104
|
|
|
return [ |
105
|
|
|
'haveArgumentsEquals' => function ($subject, $arguments) { |
106
|
|
|
return $subject->arguments == $arguments; |
107
|
|
|
}, |
108
|
|
|
'haveACallEquals' => function (DefinitionData $subject, $expectedCall) { |
109
|
|
|
$call = null; |
110
|
|
|
foreach ($subject->calls as $call) { |
111
|
|
|
if ($call == $expectedCall) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
$message = "should have an expected {$expectedCall['type']} call, but it hasn't. Check it out:"; |
116
|
|
|
try { |
117
|
|
|
\PHPUnit_Framework_Assert::assertEquals($expectedCall, $call); |
118
|
|
|
} catch (\PHPUnit_Framework_ExpectationFailedException $caught) { |
119
|
|
|
$message .= "\n". $caught->getComparisonFailure()->getDiff(); |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
throw new FailureException($message); |
124
|
|
|
} |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
class InitializableService |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
|
133
|
|
|
public $scope = null; |
134
|
|
|
|
135
|
|
|
function doSomething() |
136
|
|
|
{ |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.