|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
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 Sensio\Bundle\GeneratorBundle\Tests\Command; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Helper\HelperSet; |
|
|
15 |
use Symfony\Component\Console\Helper\FormatterHelper; |
|
|
16 |
use Symfony\Component\Console\Output\StreamOutput; |
|
|
17 |
use Symfony\Component\Console\Tester\CommandTester; |
|
|
18 |
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand; |
|
|
19 |
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper; |
|
|
20 |
use Symfony\Component\DependencyInjection\Container; |
|
|
21 |
|
|
|
22 |
class GenerateDoctrineCrudCommandTest extends GenerateCommandTest |
|
|
23 |
{ |
|
|
24 |
/** |
|
|
25 |
* @dataProvider getInteractiveCommandData |
|
|
26 |
*/ |
|
|
27 |
public function testInteractiveCommand($options, $input, $expected) |
|
|
28 |
{ |
|
|
29 |
list($entity, $format, $prefix, $withWrite) = $expected; |
|
|
30 |
|
|
|
31 |
$generator = $this->getGenerator(); |
|
|
32 |
$generator |
|
|
33 |
->expects($this->once()) |
|
|
34 |
->method('generate') |
|
|
35 |
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite) |
|
|
36 |
; |
|
|
37 |
|
|
|
38 |
$tester = new CommandTester($this->getCommand($generator, $input)); |
|
|
39 |
$tester->execute($options); |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
public function getInteractiveCommandData() |
|
|
43 |
{ |
|
|
44 |
return array( |
|
|
45 |
array(array(), "AcmeBlogBundle:Blog/Post\n", array('Blog\\Post', 'annotation', 'blog_post', false)), |
|
|
46 |
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), '', array('Blog\\Post', 'annotation', 'blog_post', false)), |
|
|
47 |
array(array(), "AcmeBlogBundle:Blog/Post\ny\nyml\nfoobar\n", array('Blog\\Post', 'yml', 'foobar', true)), |
|
|
48 |
array(array(), "AcmeBlogBundle:Blog/Post\ny\nyml\n/foobar\n", array('Blog\\Post', 'yml', 'foobar', true)), |
|
|
49 |
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--with-write' => true), '', array('Blog\\Post', 'yml', 'foo', true)), |
|
|
50 |
); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
/** |
|
|
54 |
* @dataProvider getNonInteractiveCommandData |
|
|
55 |
*/ |
|
|
56 |
public function testNonInteractiveCommand($options, $expected) |
|
|
57 |
{ |
|
|
58 |
list($entity, $format, $prefix, $withWrite) = $expected; |
|
|
59 |
|
|
|
60 |
$generator = $this->getGenerator(); |
|
|
61 |
$generator |
|
|
62 |
->expects($this->once()) |
|
|
63 |
->method('generate') |
|
|
64 |
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite) |
|
|
65 |
; |
|
|
66 |
|
|
|
67 |
$tester = new CommandTester($this->getCommand($generator, '')); |
|
|
68 |
$tester->execute($options, array('interactive' => false)); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
public function getNonInteractiveCommandData() |
|
|
72 |
{ |
|
|
73 |
return array( |
|
|
74 |
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), array('Blog\\Post', 'annotation', 'blog_post', false)), |
|
|
75 |
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--with-write' => true), array('Blog\\Post', 'yml', 'foo', true)), |
|
|
76 |
); |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
protected function getCommand($generator, $input) |
|
|
80 |
{ |
|
|
81 |
$command = $this |
|
|
82 |
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand') |
|
|
83 |
->setMethods(array('getEntityMetadata')) |
|
|
84 |
->getMock() |
|
|
85 |
; |
|
|
86 |
|
|
|
87 |
$command |
|
|
88 |
->expects($this->any()) |
|
|
89 |
->method('getEntityMetadata') |
|
|
90 |
->will($this->returnValue(array($this->getDoctrineMetadata()))) |
|
|
91 |
; |
|
|
92 |
|
|
|
93 |
$command->setContainer($this->getContainer()); |
|
|
94 |
$command->setHelperSet($this->getHelperSet($input)); |
|
|
95 |
$command->setGenerator($generator); |
|
|
96 |
$command->setFormGenerator($this->getFormGenerator()); |
|
|
97 |
|
|
|
98 |
return $command; |
|
|
99 |
} |
|
|
100 |
|
|
|
101 |
protected function getDoctrineMetadata() |
|
|
102 |
{ |
|
|
103 |
return $this |
|
|
104 |
->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo') |
|
|
105 |
->disableOriginalConstructor() |
|
|
106 |
->getMock() |
|
|
107 |
; |
|
|
108 |
} |
|
|
109 |
|
|
|
110 |
protected function getGenerator() |
|
|
111 |
{ |
|
|
112 |
// get a noop generator |
|
|
113 |
return $this |
|
|
114 |
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator') |
|
|
115 |
->disableOriginalConstructor() |
|
|
116 |
->setMethods(array('generate')) |
|
|
117 |
->getMock() |
|
|
118 |
; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
protected function getFormGenerator() |
|
|
122 |
{ |
|
|
123 |
return $this |
|
|
124 |
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator') |
|
|
125 |
->disableOriginalConstructor() |
|
|
126 |
->setMethods(array('generate')) |
|
|
127 |
->getMock() |
|
|
128 |
; |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
protected function getContainer() |
|
|
132 |
{ |
|
|
133 |
$container = parent::getContainer(); |
|
|
134 |
|
|
|
135 |
$registry = $this->getMock('Symfony\Bridge\Doctrine\RegistryInterface'); |
|
|
136 |
$registry |
|
|
137 |
->expects($this->any()) |
|
|
138 |
->method('getEntityNamespace') |
|
|
139 |
->will($this->returnValue('Foo\\FooBundle\\Entity')) |
|
|
140 |
; |
|
|
141 |
|
|
|
142 |
$container->set('doctrine', $registry); |
|
|
143 |
|
|
|
144 |
return $container; |
|
|
145 |
} |
|
|
146 |
} |