|
3 | 3 | namespace spec\Http\Discovery; |
4 | 4 |
|
5 | 5 | use Http\Discovery\ClassDiscovery; |
| 6 | +use Puli\Discovery\Binding\ClassBinding; |
| 7 | +use Puli\GeneratedPuliFactory; |
| 8 | +use Puli\Discovery\Api\Discovery; |
| 9 | +use Puli\Repository\Api\ResourceRepository; |
6 | 10 | use PhpSpec\ObjectBehavior; |
7 | 11 |
|
8 | 12 | class ClassDiscoverySpec extends ObjectBehavior |
9 | 13 | { |
10 | | - function let() |
11 | | - { |
12 | | - $this->beAnInstanceOf('spec\Http\Discovery\DiscoveryStub'); |
| 14 | + function let( |
| 15 | + GeneratedPuliFactory $puliFactory, |
| 16 | + ResourceRepository $repository, |
| 17 | + Discovery $discovery |
| 18 | + ) { |
| 19 | + $puliFactory->createRepository()->willReturn($repository); |
| 20 | + $puliFactory->createDiscovery($repository)->willReturn($discovery); |
| 21 | + |
| 22 | + $this->beAnInstanceOf('spec\Http\Discovery\ClassDiscoveryStub'); |
| 23 | + $this->setPuliFactory($puliFactory); |
13 | 24 | } |
14 | 25 |
|
15 | | - function it_is_initializable() |
| 26 | + function letgo() |
16 | 27 | { |
17 | | - $this->shouldHaveType('Http\Discovery\ClassDiscovery'); |
| 28 | + $this->resetPuliFactory(); |
18 | 29 | } |
19 | 30 |
|
20 | | - function it_registers_a_class() |
| 31 | + function it_is_initializable() |
21 | 32 | { |
22 | | - $this->reset(); |
23 | | - |
24 | | - $this->register('spec\Http\Discovery\AnotherClassToFind'); |
25 | | - |
26 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
| 33 | + $this->shouldHaveType('Http\Discovery\ClassDiscovery'); |
27 | 34 | } |
28 | 35 |
|
29 | | - function it_registers_a_class_with_a_condition() |
| 36 | + function it_has_a_puli_factory(GeneratedPuliFactory $puliFactory) |
30 | 37 | { |
31 | | - $this->reset(); |
32 | | - |
33 | | - $this->register('spec\Http\Discovery\AnotherClassToFind', 'spec\Http\Discovery\TestClass'); |
34 | | - $this->register('spec\Http\Discovery\ClassToFind', false); |
35 | | - |
36 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
| 38 | + $this->getPuliFactory()->shouldReturn($puliFactory); |
37 | 39 | } |
38 | 40 |
|
39 | | - function it_registers_a_class_with_a_callable_condition() |
| 41 | + function it_has_a_puli_discovery(Discovery $discovery) |
40 | 42 | { |
41 | | - $this->reset(); |
42 | | - |
43 | | - $this->register('spec\Http\Discovery\AnotherClassToFind', function() { return true; }); |
44 | | - $this->register('spec\Http\Discovery\ClassToFind', false); |
45 | | - |
46 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
| 43 | + $this->getPuliDiscovery()->shouldReturn($discovery); |
47 | 44 | } |
48 | 45 |
|
49 | | - function it_registers_a_class_with_a_boolean_condition() |
| 46 | + function it_throws_an_exception_when_binding_not_found(Discovery $discovery) |
50 | 47 | { |
51 | | - $this->reset(); |
52 | | - |
53 | | - $this->register('spec\Http\Discovery\AnotherClassToFind', true); |
54 | | - $this->register('spec\Http\Discovery\ClassToFind', false); |
| 48 | + $discovery->findBindings('InvalidBinding')->willReturn([]); |
55 | 49 |
|
56 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
57 | | - } |
58 | | - |
59 | | - function it_registers_a_class_with_an_array_condition() |
60 | | - { |
61 | | - $this->reset(); |
62 | | - |
63 | | - $this->register( |
64 | | - 'spec\Http\Discovery\AnotherClassToFind', |
65 | | - [ |
66 | | - true, |
67 | | - 'spec\Http\Discovery\AnotherClassToFind', |
68 | | - ] |
69 | | - ); |
70 | | - $this->register( |
71 | | - 'spec\Http\Discovery\ClassToFind', |
72 | | - [ |
73 | | - false, |
74 | | - 'spec\Http\Discovery\ClassToFind', |
75 | | - ] |
76 | | - ); |
77 | | - |
78 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
| 50 | + $this->shouldThrow('Http\Discovery\NotFoundException')->duringFindOneByType('InvalidBinding'); |
79 | 51 | } |
80 | 52 |
|
81 | | - function it_registers_a_class_with_an_invalid_condition() |
| 53 | + function it_returns_a_class_binding(Discovery $discovery, ClassBinding $binding) |
82 | 54 | { |
83 | | - $this->reset(); |
| 55 | + $binding->hasParameterValue('depends')->willReturn(false); |
| 56 | + $binding->getClassName()->willReturn('ClassName'); |
84 | 57 |
|
85 | | - $this->register('spec\Http\Discovery\AnotherClassToFind', true); |
86 | | - $this->register('spec\Http\Discovery\ClassToFind', new \stdClass); |
| 58 | + $discovery->findBindings('Binding')->willReturn([$binding]); |
87 | 59 |
|
88 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
| 60 | + $this->findOneByType('Binding')->shouldReturn('ClassName'); |
89 | 61 | } |
90 | 62 |
|
91 | | - function it_resets_cache_when_a_class_is_registered() |
92 | | - { |
93 | | - $this->reset(); |
| 63 | + function it_returns_a_class_binding_with_dependency( |
| 64 | + Discovery $discovery, |
| 65 | + ClassBinding $binding1, |
| 66 | + ClassBinding $binding2 |
| 67 | + ) { |
| 68 | + $binding1->hasParameterValue('depends')->willReturn(true); |
| 69 | + $binding1->getParameterValue('depends')->willReturn(false); |
94 | 70 |
|
95 | | - $this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind'); |
| 71 | + $binding2->hasParameterValue('depends')->willReturn(false); |
| 72 | + $binding2->getClassName()->willReturn('ClassName'); |
96 | 73 |
|
97 | | - $this->register('spec\Http\Discovery\AnotherClassToFind'); |
| 74 | + $discovery->findBindings('Binding')->willReturn([ |
| 75 | + $binding1, |
| 76 | + $binding2, |
| 77 | + ]); |
98 | 78 |
|
99 | | - $this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind'); |
100 | | - } |
101 | | - |
102 | | - function it_caches_a_found_class() |
103 | | - { |
104 | | - $this->reset(); |
105 | | - |
106 | | - $this->find()->shouldHaveType('spec\Http\Discovery\ClassToFind'); |
107 | | - |
108 | | - $this->registerWithoutCacheReset('spec\Http\Discovery\AnotherClassToFind'); |
109 | | - |
110 | | - $this->find()->shouldhaveType('spec\Http\Discovery\ClassToFind'); |
111 | | - } |
112 | | - |
113 | | - function it_throws_an_exception_when_no_class_is_found() |
114 | | - { |
115 | | - $this->resetEmpty(); |
116 | | - |
117 | | - $this->shouldThrow('Http\Discovery\NotFoundException')->duringFind(); |
| 79 | + $this->findOneByType('Binding')->shouldReturn('ClassName'); |
118 | 80 | } |
119 | 81 | } |
120 | 82 |
|
121 | | -class DiscoveryStub extends ClassDiscovery |
| 83 | +class ClassDiscoveryStub extends ClassDiscovery |
122 | 84 | { |
123 | | - protected static $cache; |
124 | | - |
125 | | - /** |
126 | | - * @var array |
127 | | - */ |
128 | | - protected static $classes; |
129 | | - |
130 | | - /** |
131 | | - * Reset classes |
132 | | - */ |
133 | | - public function reset() |
134 | | - { |
135 | | - static::$cache = null; |
136 | | - |
137 | | - static::$classes = [ |
138 | | - [ |
139 | | - 'class' => 'spec\Http\Discovery\ClassToFind', |
140 | | - 'condition' => 'spec\Http\Discovery\ClassToFind' |
141 | | - ], |
142 | | - ]; |
143 | | - } |
144 | | - |
145 | | - public function registerWithoutCacheReset($class, $condition = null) |
146 | | - { |
147 | | - $definition = [ |
148 | | - 'class' => $class, |
149 | | - 'condition' => isset($condition) ? $condition : $class, |
150 | | - ]; |
151 | | - |
152 | | - array_unshift(static::$classes, $definition); |
153 | | - } |
154 | | - |
155 | | - public function resetEmpty() |
156 | | - { |
157 | | - static::$cache = null; |
158 | | - |
159 | | - static::$classes = []; |
160 | | - } |
161 | 85 | } |
162 | | - |
163 | | -class ClassToFind {} |
164 | | -class AnotherClassToFind {} |
165 | | -class TestClass {} |
0 commit comments