1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CrudGeneratorBundle |
5
|
|
|
* |
6
|
|
|
* It is based/extended from SensioGeneratorBundle |
7
|
|
|
* |
8
|
|
|
* (c) Petko Petkov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Petkopara\CrudGeneratorBundle\Tests\Generator; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
17
|
|
|
use Petkopara\CrudGeneratorBundle\Configuration\Configuration; |
18
|
|
|
use Petkopara\CrudGeneratorBundle\Generator\PetkoparaCrudGenerator; |
19
|
|
|
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator; |
20
|
|
|
use Sensio\Bundle\GeneratorBundle\Tests\Generator\GeneratorTest; |
21
|
|
|
use Symfony\Component\HttpKernel\Bundle\BundleInterface; |
22
|
|
|
|
23
|
|
|
class CrudGeneratorGeneratorTest extends GeneratorTest |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testGenerateYamlFull() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$advancedConfig = new Configuration(); |
29
|
|
|
$advancedConfig->setRoutePrefix('post'); |
30
|
|
|
$advancedConfig->setFormat('yml'); |
31
|
|
|
$advancedConfig->setOverwrite(true); |
32
|
|
|
|
33
|
|
|
$this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
34
|
|
|
$files = array( |
35
|
|
|
'Controller/PostController.php', |
36
|
|
|
'Tests/Controller/PostControllerTest.php', |
37
|
|
|
'Resources/config/routing/post.yml', |
38
|
|
|
'Resources/views/post/index.html.twig', |
39
|
|
|
'Resources/views/post/show.html.twig', |
40
|
|
|
'Resources/views/post/new.html.twig', |
41
|
|
|
'Resources/views/post/edit.html.twig', |
42
|
|
|
); |
43
|
|
|
foreach ($files as $file) { |
44
|
|
|
$this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
45
|
|
|
} |
46
|
|
|
$files = array( |
47
|
|
|
'Resources/config/routing/post.xml', |
48
|
|
|
); |
49
|
|
|
foreach ($files as $file) { |
50
|
|
|
$this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
51
|
|
|
} |
52
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
53
|
|
|
$strings = array( |
54
|
|
|
'namespace Foo\BarBundle\Controller;', |
55
|
|
|
'public function indexAction', |
56
|
|
|
'public function showAction', |
57
|
|
|
'public function newAction', |
58
|
|
|
'public function editAction', |
59
|
|
|
); |
60
|
|
|
foreach ($strings as $string) { |
61
|
|
|
$this->assertContains($string, $content); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
$this->assertBulk(); |
66
|
|
|
$this->assertPagination(); |
67
|
|
|
$this->assertSorting(); |
68
|
|
|
$this->assertPageSize(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGenerateXml() |
72
|
|
|
{ |
73
|
|
|
$advancedConfig = new Configuration(); |
74
|
|
|
$advancedConfig->setRoutePrefix('post'); |
75
|
|
|
$advancedConfig->setFormat('xml'); |
76
|
|
|
|
77
|
|
|
$this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
78
|
|
|
$files = array( |
79
|
|
|
'Controller/PostController.php', |
80
|
|
|
'Tests/Controller/PostControllerTest.php', |
81
|
|
|
'Resources/config/routing/post.xml', |
82
|
|
|
'Resources/views/post/index.html.twig', |
83
|
|
|
'Resources/views/post/show.html.twig', |
84
|
|
|
'Resources/views/post/new.html.twig', |
85
|
|
|
'Resources/views/post/edit.html.twig', |
86
|
|
|
); |
87
|
|
|
foreach ($files as $file) { |
88
|
|
|
$this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
89
|
|
|
} |
90
|
|
|
$files = array( |
91
|
|
|
'Resources/config/routing/post.yml', |
92
|
|
|
); |
93
|
|
|
foreach ($files as $file) { |
94
|
|
|
$this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
95
|
|
|
} |
96
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
97
|
|
|
$strings = array( |
98
|
|
|
'namespace Foo\BarBundle\Controller;', |
99
|
|
|
'public function indexAction', |
100
|
|
|
'public function showAction', |
101
|
|
|
); |
102
|
|
|
foreach ($strings as $string) { |
103
|
|
|
$this->assertContains($string, $content); |
104
|
|
|
} |
105
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
106
|
|
|
$strings = array( |
107
|
|
|
'@Route', |
108
|
|
|
); |
109
|
|
|
foreach ($strings as $string) { |
110
|
|
|
$this->assertNotContains($string, $content); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->assertBulk(); |
114
|
|
|
$this->assertPagination(); |
115
|
|
|
$this->assertSorting(); |
116
|
|
|
$this->assertPageSize(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
public function testGenerateAnnotationWrite() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$advancedConfig = new Configuration(); |
122
|
|
|
$advancedConfig->setFormat('annotation'); |
123
|
|
|
$advancedConfig->setRoutePrefix('/post'); |
124
|
|
|
$this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
125
|
|
|
$files = array( |
126
|
|
|
'Controller/PostController.php', |
127
|
|
|
'Tests/Controller/PostControllerTest.php', |
128
|
|
|
'Resources/views/post/index.html.twig', |
129
|
|
|
'Resources/views/post/show.html.twig', |
130
|
|
|
'Resources/views/post/new.html.twig', |
131
|
|
|
'Resources/views/post/edit.html.twig', |
132
|
|
|
); |
133
|
|
|
foreach ($files as $file) { |
134
|
|
|
$this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
135
|
|
|
} |
136
|
|
|
$files = array( |
137
|
|
|
'Resources/config/routing/post.yml', |
138
|
|
|
'Resources/config/routing/post.xml', |
139
|
|
|
); |
140
|
|
|
foreach ($files as $file) { |
141
|
|
|
$this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
142
|
|
|
} |
143
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
144
|
|
|
$strings = array( |
145
|
|
|
'namespace Foo\BarBundle\Controller;', |
146
|
|
|
'public function indexAction', |
147
|
|
|
'public function showAction', |
148
|
|
|
'public function newAction', |
149
|
|
|
'public function editAction', |
150
|
|
|
'@Route', |
151
|
|
|
); |
152
|
|
|
foreach ($strings as $string) { |
153
|
|
|
$this->assertContains($string, $content); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
$this->assertBulk(); |
158
|
|
|
$this->assertPagination(); |
159
|
|
|
$this->assertSorting(); |
160
|
|
|
$this->assertPageSize(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
View Code Duplication |
public function testGenerateAnnotation() |
|
|
|
|
164
|
|
|
{ |
165
|
|
|
$advancedConfig = new Configuration(); |
166
|
|
|
$advancedConfig->setRoutePrefix('post'); |
167
|
|
|
$advancedConfig->setFormat('annotation'); |
168
|
|
|
$advancedConfig->setOverwrite(true); |
169
|
|
|
|
170
|
|
|
$this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
171
|
|
|
$files = array( |
172
|
|
|
'Controller/PostController.php', |
173
|
|
|
'Tests/Controller/PostControllerTest.php', |
174
|
|
|
'Resources/views/post/index.html.twig', |
175
|
|
|
'Resources/views/post/show.html.twig', |
176
|
|
|
'Resources/views/post/new.html.twig', |
177
|
|
|
'Resources/views/post/edit.html.twig', |
178
|
|
|
); |
179
|
|
|
foreach ($files as $file) { |
180
|
|
|
$this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
181
|
|
|
} |
182
|
|
|
$files = array( |
183
|
|
|
'Resources/config/routing/post.yml', |
184
|
|
|
'Resources/config/routing/post.xml', |
185
|
|
|
); |
186
|
|
|
foreach ($files as $file) { |
187
|
|
|
$this->assertFalse(file_exists($this->tmpDir . '/' . $file), sprintf('%s has not been generated', $file)); |
188
|
|
|
} |
189
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
190
|
|
|
$strings = array( |
191
|
|
|
'namespace Foo\BarBundle\Controller;', |
192
|
|
|
'public function indexAction', |
193
|
|
|
'public function showAction', |
194
|
|
|
'@Route', |
195
|
|
|
); |
196
|
|
|
foreach ($strings as $string) { |
197
|
|
|
$this->assertContains($string, $content); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$this->assertBulk(); |
201
|
|
|
$this->assertPagination(); |
202
|
|
|
$this->assertSorting(); |
203
|
|
|
$this->assertPageSize(); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testGenerateWithBaseTemplate() |
207
|
|
|
{ |
208
|
|
|
$template = 'base.html.twig'; |
209
|
|
|
$advancedConfig = new Configuration(); |
210
|
|
|
$advancedConfig->setRoutePrefix('post'); |
211
|
|
|
$advancedConfig->setFormat('annotation'); |
212
|
|
|
$advancedConfig->setOverwrite(true); |
213
|
|
|
|
214
|
|
|
$advancedConfig->setBaseTemplate($template); |
215
|
|
|
|
216
|
|
|
$this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
217
|
|
|
$content = file_get_contents($this->tmpDir . '/Resources/views/post/index.html.twig'); |
218
|
|
|
$strings = array( |
219
|
|
|
"{% extends '$template' %}", |
220
|
|
|
); |
221
|
|
|
foreach ($strings as $string) { |
222
|
|
|
$this->assertContains($string, $content); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$this->assertBulk(); |
226
|
|
|
$this->assertPagination(); |
227
|
|
|
$this->assertSorting(); |
228
|
|
|
$this->assertPageSize(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testGenerateWithBundleViews() |
232
|
|
|
{ |
233
|
|
|
$advancedConfig = new Configuration(); |
234
|
|
|
$advancedConfig->setRoutePrefix('post'); |
235
|
|
|
$advancedConfig->setFormat('annotation'); |
236
|
|
|
$advancedConfig->setBundleViews(false); |
237
|
|
|
|
238
|
|
|
$this->getGenerator()->generateCrud($this->getBundle(), 'Post', $this->getMetadata(), $advancedConfig); |
239
|
|
|
$files = array( |
240
|
|
|
'Controller/PostController.php', |
241
|
|
|
'Resources/views/post/index.html.twig', |
242
|
|
|
'Resources/views/post/show.html.twig', |
243
|
|
|
'Resources/views/post/new.html.twig', |
244
|
|
|
'Resources/views/post/edit.html.twig', |
245
|
|
|
); |
246
|
|
|
foreach ($files as $file) { |
247
|
|
|
$this->assertTrue(file_exists($this->tmpDir . '/' . $file), sprintf('%s has been generated', $file)); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @dataProvider getRoutePrefixes |
254
|
|
|
*/ |
255
|
|
|
public function testGetRouteNamePrefix($original, $expected) |
256
|
|
|
{ |
257
|
|
|
$prefix = DoctrineCrudGenerator::getRouteNamePrefix($original); |
258
|
|
|
$this->assertEquals($expected, $prefix); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function getRoutePrefixes() |
262
|
|
|
{ |
263
|
|
|
return array( |
264
|
|
|
array('', ''), |
265
|
|
|
array('/', ''), |
266
|
|
|
array('//', ''), |
267
|
|
|
array('/{foo}', ''), |
268
|
|
|
array('/{_foo}', ''), |
269
|
|
|
array('/{/foo}', ''), |
270
|
|
|
array('/{/foo/}', ''), |
271
|
|
|
array('/{_locale}', ''), |
272
|
|
|
array('/{_locale}/foo', 'foo'), |
273
|
|
|
array('/{_locale}/foo/', 'foo'), |
274
|
|
|
array('/{_locale}/foo/{_format}', 'foo'), |
275
|
|
|
array('/{_locale}/foo/{_format}/', 'foo'), |
276
|
|
|
array('/{_locale}/foo/{_format}/bar', 'foo_bar'), |
277
|
|
|
array('/{_locale}/foo/{_format}/bar/', 'foo_bar'), |
278
|
|
|
array('/{_locale}/foo/{_format}/bar//', 'foo_bar'), |
279
|
|
|
array('/{foo}/foo/{bar}/bar', 'foo_bar'), |
280
|
|
|
array('/{foo}/foo/{bar}/bar/', 'foo_bar'), |
281
|
|
|
array('/{foo}/foo/{bar}/bar//', 'foo_bar'), |
282
|
|
|
); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* |
287
|
|
|
* @return PetkoparaCrudGenerator |
288
|
|
|
*/ |
289
|
|
|
protected function getGenerator() |
290
|
|
|
{ |
291
|
|
|
$generator = new PetkoparaCrudGenerator($this->filesystem, $this->tmpDir); |
292
|
|
|
$generator->setSkeletonDirs(__DIR__ . '/../../Resources/skeleton'); |
|
|
|
|
293
|
|
|
return $generator; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return BundleInterface |
298
|
|
|
*/ |
299
|
|
|
protected function getBundle() |
300
|
|
|
{ |
301
|
|
|
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock(); |
302
|
|
|
$bundle->expects($this->any())->method('getPath')->will($this->returnValue($this->tmpDir)); |
303
|
|
|
$bundle->expects($this->any())->method('getName')->will($this->returnValue('FooBarBundle')); |
304
|
|
|
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue('Foo\BarBundle')); |
305
|
|
|
return $bundle; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @return ClassMetadataInfo |
310
|
|
|
*/ |
311
|
|
|
public function getMetadata() |
312
|
|
|
{ |
313
|
|
|
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')->disableOriginalConstructor()->getMock(); |
314
|
|
|
$metadata->identifier = array('id'); |
315
|
|
|
$metadata->fieldMappings = array('title' => array('type' => 'string')); |
316
|
|
|
return $metadata; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
protected function assertPagination() |
320
|
|
|
{ |
321
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
322
|
|
|
$strings = array( |
323
|
|
|
'protected function paginator', |
324
|
|
|
); |
325
|
|
|
foreach ($strings as $string) { |
326
|
|
|
$this->assertContains($string, $content); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
View Code Duplication |
protected function assertBulk() |
|
|
|
|
331
|
|
|
{ |
332
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
333
|
|
|
$strings = array( |
334
|
|
|
'public function deleteById', |
335
|
|
|
); |
336
|
|
|
foreach ($strings as $string) { |
337
|
|
|
$this->assertContains($string, $content); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
|
341
|
|
|
$content = file_get_contents($this->tmpDir . '/Resources/views/post/index.html.twig'); |
342
|
|
|
$strings = array( |
343
|
|
|
'<select class = "form-control" name="bulk_action" >', |
344
|
|
|
); |
345
|
|
|
foreach ($strings as $string) { |
346
|
|
|
$this->assertContains($string, $content); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
View Code Duplication |
protected function assertSorting() |
|
|
|
|
351
|
|
|
{ |
352
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
353
|
|
|
$strings = array( |
354
|
|
|
'$queryBuilder->orderBy($sortCol, $request->get(\'pcg_sort_order\', \'desc\'));', |
355
|
|
|
); |
356
|
|
|
foreach ($strings as $string) { |
357
|
|
|
$this->assertContains($string, $content); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
$content = file_get_contents($this->tmpDir . '/Resources/views/post/index.html.twig'); |
362
|
|
|
$strings = array( |
363
|
|
|
'{% import "PetkoparaCrudGeneratorBundle::macros/th_sortable.html.twig" as macros %}', |
364
|
|
|
'macros.th_sortable', |
365
|
|
|
); |
366
|
|
|
foreach ($strings as $string) { |
367
|
|
|
$this->assertContains($string, $content); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
View Code Duplication |
protected function assertPageSize() |
|
|
|
|
372
|
|
|
{ |
373
|
|
|
$content = file_get_contents($this->tmpDir . '/Controller/PostController.php'); |
374
|
|
|
$strings = array( |
375
|
|
|
'$pagerfanta->setCurrentPage($request->get(\'pcg_page\', 1));', |
376
|
|
|
); |
377
|
|
|
foreach ($strings as $string) { |
378
|
|
|
$this->assertContains($string, $content); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
$content = file_get_contents($this->tmpDir . '/Resources/views/post/index.html.twig'); |
383
|
|
|
$strings = array( |
384
|
|
|
'<select class = "form-control" onchange="window.location = this.value" >', |
385
|
|
|
'<option value=\'{{ path(\'post\', app.request.query.all|merge({\'pcg_show\': \'10\'})) }}\' {% if app.request.get(\'pcg_show\') == 10 %} selected {% endif %}>10</option>', |
386
|
|
|
); |
387
|
|
|
foreach ($strings as $string) { |
388
|
|
|
$this->assertContains($string, $content); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
} |
393
|
|
|
|
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.