/vendor/sensio/generator-bundle/Tests/Command/GenerateDoctrineCrudCommandTest.php

https://gitlab.com/pr0055/symfonypizza · PHP · 289 lines · 217 code · 57 blank · 15 comment · 0 complexity · 3792efacca2439caa98673fa4a4c8602 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\GeneratorBundle\Tests\Command;
  11. use Symfony\Component\Console\Tester\CommandTester;
  12. class GenerateDoctrineCrudCommandTest extends GenerateCommandTest
  13. {
  14. /**
  15. * @dataProvider getInteractiveCommandData
  16. */
  17. public function testInteractiveCommand($options, $input, $expected)
  18. {
  19. list($entity, $format, $prefix, $withWrite) = $expected;
  20. $generator = $this->getGenerator();
  21. $generator
  22. ->expects($this->once())
  23. ->method('generate')
  24. ->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
  25. ;
  26. $tester = new CommandTester($this->getCommand($generator, $input));
  27. $tester->execute($options);
  28. }
  29. public function getInteractiveCommandData()
  30. {
  31. return array(
  32. array(array(), "AcmeBlogBundle:Blog/Post\n", array('Blog\\Post', 'annotation', 'blog_post', false)),
  33. array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), '', array('Blog\\Post', 'annotation', 'blog_post', false)),
  34. array(array(), "AcmeBlogBundle:Blog/Post\ny\nyml\nfoobar\n", array('Blog\\Post', 'yml', 'foobar', true)),
  35. array(array(), "AcmeBlogBundle:Blog/Post\ny\nyml\n/foobar\n", array('Blog\\Post', 'yml', 'foobar', true)),
  36. array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--with-write' => true), '', array('Blog\\Post', 'yml', 'foo', true)),
  37. array(array('entity' => 'AcmeBlogBundle:Blog/Post'), "\ny\nyml\nfoobar\n", array('Blog\\Post', 'yml', 'foobar', true)),
  38. );
  39. }
  40. /**
  41. * @dataProvider getNonInteractiveCommandData
  42. */
  43. public function testNonInteractiveCommand($options, $expected)
  44. {
  45. list($entity, $format, $prefix, $withWrite) = $expected;
  46. $generator = $this->getGenerator();
  47. $generator
  48. ->expects($this->once())
  49. ->method('generate')
  50. ->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
  51. ;
  52. $tester = new CommandTester($this->getCommand($generator, ''));
  53. $tester->execute($options, array('interactive' => false));
  54. }
  55. public function getNonInteractiveCommandData()
  56. {
  57. return array(
  58. array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), array('Blog\\Post', 'annotation', 'blog_post', false)),
  59. array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--with-write' => true), array('Blog\\Post', 'yml', 'foo', true)),
  60. );
  61. }
  62. public function testCreateCrudWithAnnotationInNonAnnotationBundle()
  63. {
  64. $rootDir = $this->getContainer()->getParameter('kernel.root_dir');
  65. $routing = <<<DATA
  66. acme_blog:
  67. resource: "@AcmeBlogBundle/Resources/config/routing.xml"
  68. prefix: /
  69. DATA;
  70. file_put_contents($rootDir.'/config/routing.yml', $routing);
  71. $options = array();
  72. $input = "AcmeBlogBundle:Blog/Post\ny\nannotation\n/foobar\n";
  73. $expected = array('Blog\\Post', 'annotation', 'foobar', true);
  74. list($entity, $format, $prefix, $withWrite) = $expected;
  75. $generator = $this->getGenerator();
  76. $generator
  77. ->expects($this->once())
  78. ->method('generate')
  79. ->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
  80. ;
  81. $tester = new CommandTester($this->getCommand($generator, $input));
  82. $tester->execute($options);
  83. $expected = 'acme_blog_post:';
  84. $this->assertContains($expected, file_get_contents($rootDir.'/config/routing.yml'));
  85. }
  86. public function testCreateCrudWithAnnotationInAnnotationBundle()
  87. {
  88. $rootDir = $this->getContainer()->getParameter('kernel.root_dir');
  89. $routing = <<<DATA
  90. acme_blog:
  91. resource: "@AcmeBlogBundle/Controller/"
  92. type: annotation
  93. DATA;
  94. file_put_contents($rootDir.'/config/routing.yml', $routing);
  95. $options = array();
  96. $input = "AcmeBlogBundle:Blog/Post\ny\nyml\n/foobar\n";
  97. $expected = array('Blog\\Post', 'yml', 'foobar', true);
  98. list($entity, $format, $prefix, $withWrite) = $expected;
  99. $generator = $this->getGenerator();
  100. $generator
  101. ->expects($this->once())
  102. ->method('generate')
  103. ->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
  104. ;
  105. $tester = new CommandTester($this->getCommand($generator, $input));
  106. $tester->execute($options);
  107. $this->assertEquals($routing, file_get_contents($rootDir.'/config/routing.yml'));
  108. }
  109. public function testAddACrudWithOneAlreadyDefined()
  110. {
  111. $rootDir = $this->getContainer()->getParameter('kernel.root_dir');
  112. $routing = <<<DATA
  113. acme_blog:
  114. resource: "@AcmeBlogBundle/Controller/OtherController.php"
  115. type: annotation
  116. DATA;
  117. file_put_contents($rootDir.'/config/routing.yml', $routing);
  118. $options = array();
  119. $input = "AcmeBlogBundle:Blog/Post\ny\nannotation\n/foobar\n";
  120. $expected = array('Blog\\Post', 'annotation', 'foobar', true);
  121. list($entity, $format, $prefix, $withWrite) = $expected;
  122. $generator = $this->getGenerator();
  123. $generator
  124. ->expects($this->once())
  125. ->method('generate')
  126. ->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
  127. ;
  128. $tester = new CommandTester($this->getCommand($generator, $input));
  129. $tester->execute($options);
  130. $expected = '@AcmeBlogBundle/Controller/PostController.php';
  131. $this->assertContains($expected, file_get_contents($rootDir.'/config/routing.yml'));
  132. }
  133. protected function getCommand($generator, $input)
  134. {
  135. $command = $this
  136. ->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand')
  137. ->setMethods(array('getEntityMetadata'))
  138. ->getMock()
  139. ;
  140. $command
  141. ->expects($this->any())
  142. ->method('getEntityMetadata')
  143. ->will($this->returnValue(array($this->getDoctrineMetadata())))
  144. ;
  145. $command->setContainer($this->getContainer());
  146. $command->setHelperSet($this->getHelperSet($input));
  147. $command->setGenerator($generator);
  148. $command->setFormGenerator($this->getFormGenerator());
  149. return $command;
  150. }
  151. protected function getDoctrineMetadata()
  152. {
  153. return $this
  154. ->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')
  155. ->disableOriginalConstructor()
  156. ->getMock()
  157. ;
  158. }
  159. protected function getGenerator()
  160. {
  161. // get a noop generator
  162. return $this
  163. ->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator')
  164. ->disableOriginalConstructor()
  165. ->setMethods(array('generate'))
  166. ->getMock()
  167. ;
  168. }
  169. protected function getFormGenerator()
  170. {
  171. return $this
  172. ->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator')
  173. ->disableOriginalConstructor()
  174. ->setMethods(array('generate'))
  175. ->getMock()
  176. ;
  177. }
  178. protected function getBundle()
  179. {
  180. $bundle = parent::getBundle();
  181. $bundle
  182. ->expects($this->any())
  183. ->method('getName')
  184. ->will($this->returnValue('AcmeBlogBundle'))
  185. ;
  186. return $bundle;
  187. }
  188. protected function getContainer()
  189. {
  190. $container = parent::getContainer();
  191. $container->set('doctrine', $this->getDoctrine());
  192. return $container;
  193. }
  194. protected function getDoctrine()
  195. {
  196. $cache = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver')->getMock();
  197. $cache
  198. ->expects($this->any())
  199. ->method('getAllClassNames')
  200. ->will($this->returnValue(array('Acme\Bundle\BlogBundle\Entity\Post')))
  201. ;
  202. $configuration = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock();
  203. $configuration
  204. ->expects($this->any())
  205. ->method('getMetadataDriverImpl')
  206. ->will($this->returnValue($cache))
  207. ;
  208. $configuration
  209. ->expects($this->any())
  210. ->method('getEntityNamespaces')
  211. ->will($this->returnValue(array('AcmeBlogBundle' => 'Acme\Bundle\BlogBundle\Entity')))
  212. ;
  213. $manager = $this->getMockBuilder('Doctrine\ORM\EntityManagerInterface')->getMock();
  214. $manager
  215. ->expects($this->any())
  216. ->method('getConfiguration')
  217. ->will($this->returnValue($configuration))
  218. ;
  219. $registry = $this->getMockBuilder('Symfony\Bridge\Doctrine\RegistryInterface')->getMock();
  220. $registry
  221. ->expects($this->any())
  222. ->method('getAliasNamespace')
  223. ->will($this->returnValue('Acme\Bundle\BlogBundle\Entity\Blog\Post'))
  224. ;
  225. $registry
  226. ->expects($this->any())
  227. ->method('getManager')
  228. ->will($this->returnValue($manager))
  229. ;
  230. return $registry;
  231. }
  232. }