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