1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\TDBM\Utils; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Schema\Index; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use TheCodingMachine\TDBM\ConfigurationInterface; |
9
|
|
|
use Zend\Code\Generator\ClassGenerator; |
10
|
|
|
use Zend\Code\Generator\FileGenerator; |
11
|
|
|
use Zend\Code\Generator\MethodGenerator; |
12
|
|
|
|
13
|
|
|
class CodeGeneratorEventDispatcherTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var CodeGeneratorEventDispatcher |
17
|
|
|
*/ |
18
|
|
|
private $dispatcher; |
19
|
|
|
private $method1; |
20
|
|
|
private $method2; |
21
|
|
|
private $method3; |
22
|
|
|
private $method4; |
23
|
|
|
private $method5; |
24
|
|
|
private $beanDescriptor; |
25
|
|
|
private $configuration; |
26
|
|
|
private $class; |
27
|
|
|
private $file; |
28
|
|
|
private $pivotTableMethodsDescriptor; |
29
|
|
|
private $beanPropertyDescriptor; |
30
|
|
|
private $nullDispatcher; |
31
|
|
|
|
32
|
|
|
public function setUp() |
33
|
|
|
{ |
34
|
|
|
$this->dispatcher = new CodeGeneratorEventDispatcher([new BaseCodeGeneratorListener()]); |
35
|
|
|
$this->nullDispatcher = new CodeGeneratorEventDispatcher([new class implements CodeGeneratorListenerInterface { |
36
|
|
|
public function onBaseBeanGenerated(?FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator |
37
|
|
|
{ |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function onBaseBeanConstructorGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
42
|
|
|
{ |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function onBaseBeanPropertyGenerated(?MethodGenerator $getter, ?MethodGenerator $setter, AbstractBeanPropertyDescriptor $propertyDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): array |
47
|
|
|
{ |
48
|
|
|
return [null, null]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function onBaseBeanOneToManyGenerated(MethodGenerator $getter, DirectForeignKeyMethodDescriptor $directForeignKeyMethodDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
52
|
|
|
{ |
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function onBaseBeanManyToManyGenerated(?MethodGenerator $getter, ?MethodGenerator $adder, ?MethodGenerator $remover, ?MethodGenerator $hasser, ?MethodGenerator $setter, PivotTableMethodsDescriptor $pivotTableMethodsDescriptor, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): array |
57
|
|
|
{ |
58
|
|
|
return [null, null, null, null, null]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function onBaseBeanJsonSerializeGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
62
|
|
|
{ |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function onBaseBeanCloneGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
67
|
|
|
{ |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function onBaseDaoGenerated(?FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator |
72
|
|
|
{ |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function onBaseDaoConstructorGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
77
|
|
|
{ |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function onBaseDaoSaveGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
82
|
|
|
{ |
83
|
|
|
return null; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function onBaseDaoFindAllGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
87
|
|
|
{ |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function onBaseDaoGetByIdGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
92
|
|
|
{ |
93
|
|
|
return null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function onBaseDaoDeleteGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
97
|
|
|
{ |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function onBaseDaoFindGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
102
|
|
|
{ |
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function onBaseDaoFindFromSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
107
|
|
|
{ |
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function onBaseDaoFindFromRawSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
112
|
|
|
{ |
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function onBaseDaoFindOneGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
117
|
|
|
{ |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function onBaseDaoFindOneFromSqlGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
122
|
|
|
{ |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function onBaseDaoSetDefaultSortGenerated(MethodGenerator $methodGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
127
|
|
|
{ |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function onBaseDaoFindByIndexGenerated(MethodGenerator $methodGenerator, Index $index, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration, ClassGenerator $classGenerator): ?MethodGenerator |
132
|
|
|
{ |
133
|
|
|
return null; |
134
|
|
|
} |
135
|
|
|
}]); |
136
|
|
|
$this->method1 = new MethodGenerator(); |
137
|
|
|
$this->method2 = new MethodGenerator(); |
138
|
|
|
$this->method3 = new MethodGenerator(); |
139
|
|
|
$this->method4 = new MethodGenerator(); |
140
|
|
|
$this->method5 = new MethodGenerator(); |
141
|
|
|
$this->beanDescriptor = $this->getMockBuilder(BeanDescriptor::class)->disableOriginalConstructor()->getMock(); |
142
|
|
|
$this->configuration = $this->getMockBuilder(ConfigurationInterface::class)->disableOriginalConstructor()->getMock(); |
143
|
|
|
$this->pivotTableMethodsDescriptor = $this->getMockBuilder(PivotTableMethodsDescriptor::class)->disableOriginalConstructor()->getMock(); |
144
|
|
|
$this->beanPropertyDescriptor = $this->getMockBuilder(ScalarBeanPropertyDescriptor::class)->disableOriginalConstructor()->getMock(); |
145
|
|
|
$this->class = new ClassGenerator(); |
146
|
|
|
$this->file = new FileGenerator(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testOnBaseDaoFindAllGenerated() |
150
|
|
|
{ |
151
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindAllGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
152
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindAllGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function testOnBaseBeanJsonSerializeGenerated() |
156
|
|
|
{ |
157
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseBeanJsonSerializeGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
158
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseBeanJsonSerializeGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function testOnBaseDaoConstructorGenerated() |
162
|
|
|
{ |
163
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
164
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testOnBaseDaoFindOneFromSqlGenerated() |
168
|
|
|
{ |
169
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindOneFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
170
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindOneFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testOnBaseDaoGenerated() |
174
|
|
|
{ |
175
|
|
|
$this->assertSame($this->file, $this->dispatcher->onBaseDaoGenerated($this->file, $this->beanDescriptor, $this->configuration)); |
176
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoGenerated($this->file, $this->beanDescriptor, $this->configuration)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function testOnBaseDaoSetDefaultSortGenerated() |
180
|
|
|
{ |
181
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoSetDefaultSortGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
182
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoSetDefaultSortGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testOnBaseDaoSaveGenerated() |
186
|
|
|
{ |
187
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoSaveGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
188
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoSaveGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testOnBaseDaoGetByIdGenerated() |
192
|
|
|
{ |
193
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoGetByIdGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
194
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoGetByIdGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testOnBaseBeanCloneGenerated() |
198
|
|
|
{ |
199
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseBeanCloneGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
200
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseBeanCloneGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testOnBaseDaoFindOneGenerated() |
204
|
|
|
{ |
205
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindOneGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
206
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindOneGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function testOnBaseBeanPropertyGenerated() |
210
|
|
|
{ |
211
|
|
|
$this->assertSame([$this->method1, $this->method2], $this->dispatcher->onBaseBeanPropertyGenerated($this->method1, $this->method2, $this->beanPropertyDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); |
212
|
|
|
$this->assertSame([null, null], $this->nullDispatcher->onBaseBeanPropertyGenerated(null, null, $this->beanPropertyDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function testOnBaseDaoFindGenerated() |
216
|
|
|
{ |
217
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
218
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testOnBaseBeanOneToManyGenerated() |
222
|
|
|
{ |
223
|
|
|
$directForeignKeyMethodDescriptor = $this->getMockBuilder(DirectForeignKeyMethodDescriptor::class)->disableOriginalConstructor()->getMock(); |
224
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseBeanOneToManyGenerated($this->method1, $directForeignKeyMethodDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); |
225
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseBeanOneToManyGenerated($this->method1, $directForeignKeyMethodDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function testOnBaseDaoFindFromSqlGenerated() |
229
|
|
|
{ |
230
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
231
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindFromSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function testOnBaseBeanConstructorGenerated() |
235
|
|
|
{ |
236
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseBeanConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
237
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseBeanConstructorGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function testOnBaseDaoFindFromRawSqlGenerated() |
241
|
|
|
{ |
242
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindFromRawSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
243
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindFromRawSqlGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testOnBaseDaoDeleteGenerated() |
247
|
|
|
{ |
248
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoDeleteGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
249
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoDeleteGenerated($this->method1, $this->beanDescriptor, $this->configuration, $this->class)); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
public function testOnBaseBeanGenerated() |
253
|
|
|
{ |
254
|
|
|
$this->assertSame($this->file, $this->dispatcher->onBaseBeanGenerated($this->file, $this->beanDescriptor, $this->configuration)); |
255
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseBeanGenerated($this->file, $this->beanDescriptor, $this->configuration)); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public function testOnBaseBeanManyToManyGenerated() |
259
|
|
|
{ |
260
|
|
|
$this->assertSame([$this->method1, $this->method2, $this->method3, $this->method4, $this->method5], $this->dispatcher->onBaseBeanManyToManyGenerated($this->method1, $this->method2, $this->method3, $this->method4, $this->method5, $this->pivotTableMethodsDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); |
261
|
|
|
$this->assertSame([null, null, null, null, null], $this->nullDispatcher->onBaseBeanManyToManyGenerated(null, null, null, null, null, $this->pivotTableMethodsDescriptor, $this->beanDescriptor, $this->configuration, $this->class)); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testOnBaseDaoFindByIndexGenerated() |
265
|
|
|
{ |
266
|
|
|
$index = $this->getMockBuilder(Index::class)->disableOriginalConstructor()->getMock(); |
267
|
|
|
$this->assertSame($this->method1, $this->dispatcher->onBaseDaoFindByIndexGenerated($this->method1, $index, $this->beanDescriptor, $this->configuration, $this->class)); |
268
|
|
|
$this->assertSame(null, $this->nullDispatcher->onBaseDaoFindByIndexGenerated($this->method1, $index, $this->beanDescriptor, $this->configuration, $this->class)); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|