1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Petkopara\CrudGeneratorBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use Petkopara\CrudGeneratorBundle\Configuration\Configuration; |
6
|
|
|
use Sensio\Bundle\GeneratorBundle\Tests\Command\GenerateCommandTest; |
7
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
/* |
11
|
|
|
* This file is part of the CrudGeneratorBundle |
12
|
|
|
* |
13
|
|
|
* It is based/extended from SensioGeneratorBundle |
14
|
|
|
* |
15
|
|
|
* (c) Petko Petkov <[email protected]> |
16
|
|
|
* |
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
18
|
|
|
* file that was distributed with this source code. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class CrudGeneratorCommandTest extends GenerateCommandTest |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @dataProvider getInteractiveCommandData |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function testInteractiveCommand($options, $input, $expected) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
list($entity, $withoutWrite, $filterType, $template, $format, $prefix, $withoutShow, $withoutBulk, $withoutSort, $withoutPageSize, $bundleViews, $overwrite) = $expected; |
30
|
|
|
$advConfig = new Configuration(); |
31
|
|
|
$advConfig->setWithoutWrite($withoutWrite); |
32
|
|
|
$advConfig->setRoutePrefix($prefix); |
33
|
|
|
$advConfig->setFormat($format); |
34
|
|
|
$advConfig->setFilterType($filterType); |
35
|
|
|
$advConfig->setBaseTemplate($template); |
36
|
|
|
$advConfig->setWithoutShow($withoutShow); |
37
|
|
|
$advConfig->setWithoutBulk($withoutBulk); |
38
|
|
|
$advConfig->setWithoutSorting($withoutSort); |
39
|
|
|
$advConfig->setWithoutPageSize($withoutPageSize); |
40
|
|
|
$advConfig->setOverwrite($overwrite); |
41
|
|
|
$advConfig->setBundleViews($bundleViews); |
42
|
|
|
|
43
|
|
|
$generator = $this->getGenerator(); |
44
|
|
|
$generator |
45
|
|
|
->expects($this->once()) |
46
|
|
|
->method('generateCrud') |
47
|
|
|
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $advConfig) |
48
|
|
|
; |
49
|
|
|
$tester = new CommandTester($this->getCommand($generator, $input)); |
50
|
|
|
$tester->execute($options); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getInteractiveCommandData() |
54
|
|
|
{ |
55
|
|
|
return array( |
56
|
|
|
array(array(), "AcmeBlogBundle:Blog/Post\nn\ninput\nbase.html.twig\nannotation\n/foobar\n\n", array('Blog\\Post', false, 'input', 'base.html.twig', 'annotation', 'foobar', false, false, false, false, false, false)), |
57
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--bundle-views'=> true), '', array('Blog\\Post', false, 'input', 'PetkoparaCrudGeneratorBundle::base.html.twig', 'annotation', 'blog_post', false, false, false, false, true,false)), |
58
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--template'=> 'base.html.twig', '--filter-type'=>'none'), '', array('Blog\\Post', false, 'none', 'base.html.twig', 'annotation', 'blog_post', false, false, false, false, false, false)), |
59
|
|
|
array(array(), "AcmeBlogBundle:Blog/Post\nn\nform\nbase.html.twig\nyml\n/foobar\n\n", array('Blog\\Post', false, 'form', 'base.html.twig', 'yml', 'foobar', false, false, false, false,false, false)), |
60
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--without-write' => true, '--filter-type' => 'input'), '', array('Blog\\Post', true, 'input', 'PetkoparaCrudGeneratorBundle::base.html.twig', 'yml', 'foo', false, false, false, false,false, false)), |
61
|
|
|
array(array('--without-show' => true, '--without-bulk' => true,), "AcmeBlogBundle:Blog/Post\nn\ninput\nbase.html.twig\nannotation\n/foobar\n\n", array('Blog\\Post', false, 'input', 'base.html.twig', 'annotation', 'foobar', true, true, false, false, false,false)), |
62
|
|
|
array(array('--without-sorting' => true, '--without-page-size' => true, '--bundle-views'=> true), "AcmeBlogBundle:Blog/Post\nn\ninput\nbase.html.twig\nannotation\n/foobar\n\n", array('Blog\\Post', false, 'input', 'base.html.twig', 'annotation', 'foobar', false, false, true, true, true,false)), |
63
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--overwrite'=> true,'--without-sorting' => true, '--without-page-size' => true, '--without-show' => true, '--without-bulk' => true,), '', array('Blog\\Post', false, 'input', 'PetkoparaCrudGeneratorBundle::base.html.twig', 'annotation', 'blog_post', true, true, true, true, false, true)), |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @dataProvider getNonInteractiveCommandData |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function testNonInteractiveCommand($options, $expected) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
list($entity, $withoutWrite, $filterType, $template, $format, $prefix, $withoutShow, $withoutBulk, $withoutSort, $withoutPageSize, $bundleViews, $overwrite) = $expected; |
73
|
|
|
$advConfig = new Configuration(); |
74
|
|
|
$advConfig->setWithoutWrite($withoutWrite); |
75
|
|
|
$advConfig->setRoutePrefix($prefix); |
76
|
|
|
$advConfig->setFormat($format); |
77
|
|
|
$advConfig->setFilterType($filterType); |
78
|
|
|
$advConfig->setBaseTemplate($template); |
79
|
|
|
$advConfig->setWithoutShow($withoutShow); |
80
|
|
|
$advConfig->setWithoutBulk($withoutBulk); |
81
|
|
|
$advConfig->setWithoutSorting($withoutSort); |
82
|
|
|
$advConfig->setWithoutPageSize($withoutPageSize); |
83
|
|
|
$advConfig->setBundleViews($bundleViews); |
84
|
|
|
$advConfig->setOverwrite($overwrite); |
85
|
|
|
$generator = $this->getGenerator(); |
86
|
|
|
|
87
|
|
|
$generator |
88
|
|
|
->expects($this->once()) |
89
|
|
|
->method('generateCrud') |
90
|
|
|
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $advConfig) |
91
|
|
|
; |
92
|
|
|
$tester = new CommandTester($this->getCommand($generator, '')); |
93
|
|
|
$tester->execute($options, array('interactive' => false)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getNonInteractiveCommandData() |
97
|
|
|
{ |
98
|
|
|
return array( |
99
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), array('Blog\\Post', false, 'input', 'PetkoparaCrudGeneratorBundle::base.html.twig', 'annotation', 'blog_post', false, false, false, false, false, false)), |
100
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--filter-type'=> 'form', '--bundle-views'=> true, '--template'=> 'base.html.twig'), array('Blog\\Post', false, 'form', 'base.html.twig', 'annotation', 'blog_post', false, false, false, false, true, false)), |
101
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--without-write' => true), array('Blog\\Post', true, 'input', 'PetkoparaCrudGeneratorBundle::base.html.twig', 'yml', 'foo', false, false, false, false, false, false)), |
102
|
|
|
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--without-show' => true, '--without-bulk' => true,'--without-sorting' => true, '--without-page-size' => true), array('Blog\\Post', false, 'input', 'PetkoparaCrudGeneratorBundle::base.html.twig', 'annotation', 'blog_post', true, true, true, true, false, false)), |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testCreateCrudWithAnnotationInNonAnnotationBundle() |
107
|
|
|
{ |
108
|
|
|
$rootDir = $this->getContainer()->getParameter('kernel.root_dir'); |
109
|
|
|
$routing = <<<DATA |
110
|
|
|
acme_blog: |
111
|
|
|
resource: "@AcmeBlogBundle/Resources/config/routing.xml" |
112
|
|
|
prefix: / |
113
|
|
|
DATA; |
114
|
|
|
file_put_contents($rootDir . '/config/routing.yml', $routing); |
115
|
|
|
$options = array(); |
116
|
|
|
$input = "AcmeBlogBundle:Blog/Post\nn\ninput\nPetkoparaCrudGeneratorBundle::base.html.twig\nannotation\n/foobar\n\n"; |
117
|
|
|
$expected = array('Blog\\Post', 'annotation', 'foobar', false); |
118
|
|
|
list($entity, $format, $prefix, $withoutWrite) = $expected; |
119
|
|
|
$generator = $this->getGenerator(); |
120
|
|
|
|
121
|
|
|
$advConfig = new Configuration(); |
122
|
|
|
$advConfig->setWithoutWrite($withoutWrite); |
123
|
|
|
$advConfig->setRoutePrefix($prefix); |
124
|
|
|
$advConfig->setFormat($format); |
125
|
|
|
$generator |
126
|
|
|
->expects($this->once()) |
127
|
|
|
->method('generateCrud') |
128
|
|
|
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $advConfig) |
129
|
|
|
; |
130
|
|
|
$tester = new CommandTester($this->getCommand($generator, $input)); |
131
|
|
|
$tester->execute($options); |
132
|
|
|
$expected = 'acme_blog_post:'; |
133
|
|
|
$this->assertContains($expected, file_get_contents($rootDir . '/config/routing.yml')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testCreateCrudWithAnnotationInAnnotationBundle() |
137
|
|
|
{ |
138
|
|
|
$rootDir = $this->getContainer()->getParameter('kernel.root_dir'); |
139
|
|
|
$routing = <<<DATA |
140
|
|
|
acme_blog: |
141
|
|
|
resource: "@AcmeBlogBundle/Controller/" |
142
|
|
|
type: annotation |
143
|
|
|
DATA; |
144
|
|
|
file_put_contents($rootDir . '/config/routing.yml', $routing); |
145
|
|
|
$options = array(); |
146
|
|
|
$input = "AcmeBlogBundle:Blog/Post\nn\ninput\nPetkoparaCrudGeneratorBundle::base.html.twig\nyml\n/foobar\n\n"; |
147
|
|
|
$expected = array('Blog\\Post', 'yml', 'foobar', false); |
148
|
|
|
list($entity, $format, $prefix, $withoutWrite) = $expected; |
|
|
|
|
149
|
|
|
$advConfig = new Configuration(); |
150
|
|
|
$advConfig->setRoutePrefix($prefix); |
151
|
|
|
$advConfig->setFormat($format); |
152
|
|
|
|
153
|
|
|
$generator = $this->getGenerator(); |
154
|
|
|
$generator |
155
|
|
|
->expects($this->once()) |
156
|
|
|
->method('generateCrud') |
157
|
|
|
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $advConfig) |
158
|
|
|
; |
159
|
|
|
$tester = new CommandTester($this->getCommand($generator, $input)); |
160
|
|
|
$tester->execute($options); |
161
|
|
|
$this->assertEquals($routing, file_get_contents($rootDir . '/config/routing.yml')); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testAddACrudWithOneAlreadyDefined() |
165
|
|
|
{ |
166
|
|
|
$rootDir = $this->getContainer()->getParameter('kernel.root_dir'); |
167
|
|
|
$routing = <<<DATA |
168
|
|
|
acme_blog: |
169
|
|
|
resource: "@AcmeBlogBundle/Controller/OtherController.php" |
170
|
|
|
type: annotation |
171
|
|
|
DATA; |
172
|
|
|
file_put_contents($rootDir . '/config/routing.yml', $routing); |
173
|
|
|
$options = array(); |
174
|
|
|
$input = "AcmeBlogBundle:Blog/Post\nn\ninput\nPetkoparaCrudGeneratorBundle::base.html.twig\nannotation\n/foobar\n\n"; |
175
|
|
|
$expected = array('Blog\\Post', 'annotation', 'foobar', false, false, false, false, false); |
176
|
|
|
list($entity, $format, $prefix, $withoutWrite, $withoutShow, $withoutBulk, $withoutSort, $withoutPageSize) = $expected; |
177
|
|
|
$advConfig = new Configuration(); |
178
|
|
|
|
179
|
|
|
$advConfig->setWithoutWrite($withoutWrite); |
180
|
|
|
$advConfig->setWithoutShow($withoutShow); |
181
|
|
|
$advConfig->setWithoutBulk($withoutBulk); |
182
|
|
|
$advConfig->setWithoutSorting($withoutSort); |
183
|
|
|
$advConfig->setWithoutPageSize($withoutPageSize); |
184
|
|
|
$advConfig->setRoutePrefix($prefix); |
185
|
|
|
$advConfig->setFormat($format); |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
$generator = $this->getGenerator(); |
189
|
|
|
$generator |
190
|
|
|
->expects($this->once()) |
191
|
|
|
->method('generateCrud') |
192
|
|
|
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $advConfig) |
193
|
|
|
; |
194
|
|
|
$tester = new CommandTester($this->getCommand($generator, $input)); |
195
|
|
|
$tester->execute($options); |
196
|
|
|
$expected = '@AcmeBlogBundle/Controller/PostController.php'; |
197
|
|
|
$this->assertContains($expected, file_get_contents($rootDir . '/config/routing.yml')); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
protected function getCommand($generator, $input) |
201
|
|
|
{ |
202
|
|
|
$command = $this |
203
|
|
|
->getMockBuilder('Petkopara\CrudGeneratorBundle\Command\CrudGeneratorCommand') |
204
|
|
|
->setMethods(array('getEntityMetadata')) |
205
|
|
|
->getMock() |
206
|
|
|
; |
207
|
|
|
$command |
208
|
|
|
->expects($this->any()) |
209
|
|
|
->method('getEntityMetadata') |
210
|
|
|
->will($this->returnValue(array($this->getDoctrineMetadata()))) |
211
|
|
|
; |
212
|
|
|
$command->setContainer($this->getContainer()); |
213
|
|
|
$command->setHelperSet($this->getHelperSet($input)); |
214
|
|
|
$command->setGenerator($generator); |
215
|
|
|
$command->setFormCrudGenerator($this->getFormGenerator()); |
216
|
|
|
$command->setFilterGenerator($this->getFilterGenerator()); |
217
|
|
|
return $command; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function getDoctrineMetadata() |
221
|
|
|
{ |
222
|
|
|
return $this |
223
|
|
|
->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo') |
224
|
|
|
->disableOriginalConstructor() |
225
|
|
|
->getMock() |
226
|
|
|
; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
protected function getGenerator() |
230
|
|
|
{ |
231
|
|
|
// get a noop generator |
232
|
|
|
return $this |
233
|
|
|
->getMockBuilder('Petkopara\CrudGeneratorBundle\Generator\PetkoparaCrudGenerator') |
234
|
|
|
->disableOriginalConstructor() |
235
|
|
|
->setMethods(array('generateCrud')) |
236
|
|
|
->getMock() |
237
|
|
|
; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
protected function getFormGenerator() |
241
|
|
|
{ |
242
|
|
|
return $this |
243
|
|
|
->getMockBuilder('Petkopara\CrudGeneratorBundle\Generator\PetkoparaFormGenerator') |
244
|
|
|
->disableOriginalConstructor() |
245
|
|
|
->setMethods(array('generate')) |
246
|
|
|
->getMock() |
247
|
|
|
; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
protected function getFilterGenerator() |
251
|
|
|
{ |
252
|
|
|
return $this |
253
|
|
|
->getMockBuilder('Petkopara\CrudGeneratorBundle\Generator\PetkoparaFilterGenerator') |
254
|
|
|
->disableOriginalConstructor() |
255
|
|
|
->setMethods(array('generate')) |
256
|
|
|
->getMock() |
257
|
|
|
; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
protected function getBundle() |
261
|
|
|
{ |
262
|
|
|
$bundle = parent::getBundle(); |
263
|
|
|
$bundle |
264
|
|
|
->expects($this->any()) |
265
|
|
|
->method('getName') |
266
|
|
|
->will($this->returnValue('AcmeBlogBundle')) |
267
|
|
|
; |
268
|
|
|
return $bundle; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
protected function getContainer() |
272
|
|
|
{ |
273
|
|
|
$container = parent::getContainer(); |
274
|
|
|
$container->set('doctrine', $this->getDoctrine()); |
275
|
|
|
return $container; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
protected function getDoctrine() |
279
|
|
|
{ |
280
|
|
|
$cache = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver')->getMock(); |
281
|
|
|
$cache |
282
|
|
|
->expects($this->any()) |
283
|
|
|
->method('getAllClassNames') |
284
|
|
|
->will($this->returnValue(array('Acme\Bundle\BlogBundle\Entity\Post'))) |
285
|
|
|
; |
286
|
|
|
$configuration = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock(); |
287
|
|
|
$configuration |
288
|
|
|
->expects($this->any()) |
289
|
|
|
->method('getMetadataDriverImpl') |
290
|
|
|
->will($this->returnValue($cache)) |
291
|
|
|
; |
292
|
|
|
$configuration |
293
|
|
|
->expects($this->any()) |
294
|
|
|
->method('getEntityNamespaces') |
295
|
|
|
->will($this->returnValue(array('AcmeBlogBundle' => 'Acme\Bundle\BlogBundle\Entity'))) |
296
|
|
|
; |
297
|
|
|
$manager = $this->getMockBuilder('Doctrine\ORM\EntityManagerInterface')->getMock(); |
298
|
|
|
$manager |
299
|
|
|
->expects($this->any()) |
300
|
|
|
->method('getConfiguration') |
301
|
|
|
->will($this->returnValue($configuration)) |
302
|
|
|
; |
303
|
|
|
$registry = $this->getMockBuilder('Symfony\Bridge\Doctrine\RegistryInterface')->getMock(); |
304
|
|
|
$registry |
305
|
|
|
->expects($this->any()) |
306
|
|
|
->method('getAliasNamespace') |
307
|
|
|
->will($this->returnValue('Acme\Bundle\BlogBundle\Entity\Blog\Post')) |
308
|
|
|
; |
309
|
|
|
$registry |
310
|
|
|
->expects($this->any()) |
311
|
|
|
->method('getManager') |
312
|
|
|
->will($this->returnValue($manager)) |
313
|
|
|
; |
314
|
|
|
return $registry; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
} |
318
|
|
|
|
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.