1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the puli/discovery package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Puli\Discovery\Test; |
13
|
|
|
|
14
|
|
|
use Puli\Discovery\Api\Type\BindingParameter; |
15
|
|
|
use Puli\Discovery\Api\Type\BindingType; |
16
|
|
|
use Puli\Discovery\Test\Fixtures\Foo; |
17
|
|
|
use Puli\Discovery\Test\Fixtures\StringBinding; |
18
|
|
|
use Webmozart\Expression\Expr; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @since 1.0 |
22
|
|
|
* |
23
|
|
|
* @author Bernhard Schussek <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractPersistentDiscoveryTest extends AbstractEditableDiscoveryTest |
26
|
|
|
{ |
27
|
2 |
|
public function testAddBindingKeepsStoredBindings() |
28
|
|
|
{ |
29
|
2 |
|
$discovery = $this->createDiscovery(); |
30
|
2 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
31
|
2 |
|
$discovery->addBinding($binding1 = new StringBinding('string1', Foo::clazz)); |
32
|
|
|
|
33
|
2 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery); |
34
|
2 |
|
$discovery->addBinding($binding2 = new StringBinding('string2', Foo::clazz)); |
35
|
|
|
|
36
|
2 |
|
$this->assertEquals(array($binding1, $binding2), $discovery->getBindings()); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function testAddBindingInitializesLoadedBindings() |
40
|
|
|
{ |
41
|
1 |
|
$binding1 = new StringBinding('string1', Foo::clazz); |
42
|
1 |
|
$binding2 = new StringBinding('string2', Foo::clazz); |
43
|
|
|
|
44
|
1 |
|
$this->initializer->expects($this->once()) |
45
|
1 |
|
->method('acceptsBinding') |
46
|
1 |
|
->willReturn(true); |
47
|
|
|
|
48
|
1 |
|
$this->initializer->expects($this->exactly(2)) |
49
|
1 |
|
->method('initializeBinding') |
50
|
1 |
|
->withConsecutive( |
51
|
1 |
|
array($binding1), |
52
|
1 |
|
array($binding2) |
53
|
|
|
); |
54
|
|
|
|
55
|
1 |
|
$discovery = $this->createDiscovery(); |
56
|
1 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
57
|
1 |
|
$discovery->addBinding($binding1); |
58
|
|
|
|
59
|
1 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer)); |
60
|
1 |
|
$discovery->addBinding($binding2); |
61
|
1 |
|
} |
62
|
|
|
|
63
|
2 |
View Code Duplication |
public function testRemoveBindingsDoesNotInitializeLoadedBindings() |
|
|
|
|
64
|
|
|
{ |
65
|
2 |
|
$this->initializer->expects($this->never()) |
66
|
2 |
|
->method('acceptsBinding'); |
67
|
|
|
|
68
|
2 |
|
$this->initializer->expects($this->never()) |
69
|
2 |
|
->method('initializeBinding'); |
70
|
|
|
|
71
|
2 |
|
$discovery = $this->createDiscovery(); |
72
|
2 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
73
|
2 |
|
$discovery->addBinding(new StringBinding('string1', Foo::clazz)); |
74
|
2 |
|
$discovery->addBinding(new StringBinding('string2', Foo::clazz)); |
75
|
|
|
|
76
|
2 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer)); |
77
|
2 |
|
$discovery->removeBindings(); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
2 |
View Code Duplication |
public function testRemoveBindingsWithTypeDoesNotInitializeLoadedBindings() |
|
|
|
|
81
|
|
|
{ |
82
|
2 |
|
$this->initializer->expects($this->never()) |
83
|
2 |
|
->method('acceptsBinding'); |
84
|
|
|
|
85
|
2 |
|
$this->initializer->expects($this->never()) |
86
|
2 |
|
->method('initializeBinding'); |
87
|
|
|
|
88
|
2 |
|
$discovery = $this->createDiscovery(); |
89
|
2 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
90
|
2 |
|
$discovery->addBinding(new StringBinding('string1', Foo::clazz)); |
91
|
2 |
|
$discovery->addBinding(new StringBinding('string2', Foo::clazz)); |
92
|
|
|
|
93
|
2 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer)); |
94
|
2 |
|
$discovery->removeBindings(Foo::clazz); |
95
|
2 |
|
} |
96
|
|
|
|
97
|
2 |
|
public function testRemoveBindingsWithTypeAndParameterWorksOnLoadedDiscovery() |
98
|
|
|
{ |
99
|
2 |
|
$binding1 = new StringBinding('string1', Foo::clazz, array('param2' => 'bar')); |
100
|
2 |
|
$binding2 = new StringBinding('string2', Foo::clazz); |
101
|
2 |
|
$binding3 = new StringBinding('string3', Foo::clazz, array('param1' => 'bar')); |
102
|
|
|
|
103
|
2 |
|
$discovery = $this->createDiscovery(); |
104
|
2 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING, array( |
105
|
2 |
|
new BindingParameter('param1', BindingParameter::OPTIONAL, 'foo'), |
106
|
2 |
|
new BindingParameter('param2'), |
107
|
|
|
))); |
108
|
2 |
|
$discovery->addBinding($binding1); |
109
|
2 |
|
$discovery->addBinding($binding2); |
110
|
2 |
|
$discovery->addBinding($binding3); |
111
|
|
|
|
112
|
2 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery); |
113
|
|
|
|
114
|
|
|
// Bindings need to be initialized for this to work |
115
|
2 |
|
$discovery->removeBindings(Foo::clazz, Expr::method('getParameterValue', 'param1', Expr::same('foo'))); |
116
|
|
|
|
117
|
2 |
|
$this->assertEquals(array($binding3), $discovery->findBindings(Foo::clazz)); |
118
|
2 |
|
$this->assertEquals(array($binding3), $discovery->getBindings()); |
119
|
2 |
|
} |
120
|
|
|
|
121
|
2 |
View Code Duplication |
public function testRemoveBindingTypeDoesNotInitializeLoadedBindings() |
|
|
|
|
122
|
|
|
{ |
123
|
2 |
|
$this->initializer->expects($this->never()) |
124
|
2 |
|
->method('acceptsBinding'); |
125
|
|
|
|
126
|
2 |
|
$this->initializer->expects($this->never()) |
127
|
2 |
|
->method('initializeBinding'); |
128
|
|
|
|
129
|
2 |
|
$discovery = $this->createDiscovery(); |
130
|
2 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
131
|
2 |
|
$discovery->addBinding(new StringBinding('string1', Foo::clazz)); |
132
|
2 |
|
$discovery->addBinding(new StringBinding('string2', Foo::clazz)); |
133
|
|
|
|
134
|
2 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer)); |
135
|
2 |
|
$discovery->removeBindingType(Foo::clazz); |
136
|
2 |
|
} |
137
|
|
|
|
138
|
1 |
View Code Duplication |
public function testFindBindingsInitializesLoadedBindings() |
|
|
|
|
139
|
|
|
{ |
140
|
1 |
|
$binding1 = new StringBinding('string1', Foo::clazz); |
141
|
1 |
|
$binding2 = new StringBinding('string2', Foo::clazz); |
142
|
|
|
|
143
|
1 |
|
$this->initializer->expects($this->once()) |
144
|
1 |
|
->method('acceptsBinding') |
145
|
1 |
|
->willReturn(true); |
146
|
|
|
|
147
|
1 |
|
$this->initializer->expects($this->exactly(2)) |
148
|
1 |
|
->method('initializeBinding') |
149
|
1 |
|
->withConsecutive( |
150
|
1 |
|
array($binding1), |
151
|
1 |
|
array($binding2) |
152
|
|
|
); |
153
|
|
|
|
154
|
1 |
|
$discovery = $this->createDiscovery(); |
155
|
1 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
156
|
1 |
|
$discovery->addBinding($binding1); |
157
|
1 |
|
$discovery->addBinding($binding2); |
158
|
|
|
|
159
|
1 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer)); |
160
|
1 |
|
$discovery->findBindings(Foo::clazz); |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
View Code Duplication |
public function testGetBindingsInitializesLoadedBindings() |
|
|
|
|
164
|
|
|
{ |
165
|
1 |
|
$binding1 = new StringBinding('string1', Foo::clazz); |
166
|
1 |
|
$binding2 = new StringBinding('string2', Foo::clazz); |
167
|
|
|
|
168
|
1 |
|
$this->initializer->expects($this->once()) |
169
|
1 |
|
->method('acceptsBinding') |
170
|
1 |
|
->willReturn(true); |
171
|
|
|
|
172
|
1 |
|
$this->initializer->expects($this->exactly(2)) |
173
|
1 |
|
->method('initializeBinding') |
174
|
1 |
|
->withConsecutive( |
175
|
1 |
|
array($binding1), |
176
|
1 |
|
array($binding2) |
177
|
|
|
); |
178
|
|
|
|
179
|
1 |
|
$discovery = $this->createDiscovery(); |
180
|
1 |
|
$discovery->addBindingType(new BindingType(Foo::clazz, self::STRING_BINDING)); |
181
|
1 |
|
$discovery->addBinding($binding1); |
182
|
1 |
|
$discovery->addBinding($binding2); |
183
|
|
|
|
184
|
1 |
|
$discovery = $this->loadDiscoveryFromStorage($discovery, array($this->initializer)); |
185
|
1 |
|
$discovery->getBindings(); |
186
|
1 |
|
} |
187
|
|
|
} |
188
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.