GenerateAdminCommandTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Command;
  11. use Sonata\AdminBundle\Command\GenerateAdminCommand;
  12. use Sonata\AdminBundle\Tests\Fixtures\Bundle\DemoAdminBundle;
  13. use Symfony\Bundle\FrameworkBundle\Console\Application;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Question\Question;
  17. use Symfony\Component\Console\Tester\CommandTester;
  18. use Symfony\Component\DependencyInjection\Container;
  19. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  20. /**
  21. * @author Andrej Hudec <pulzarraider@gmail.com>
  22. */
  23. class GenerateAdminCommandTest extends \PHPUnit_Framework_TestCase
  24. {
  25. /**
  26. * @var Application
  27. */
  28. private $application;
  29. /**
  30. * @var string
  31. */
  32. private $tempDirectory;
  33. /**
  34. * @var Container
  35. */
  36. private $container;
  37. /**
  38. * @var GenerateAdminCommand
  39. */
  40. private $command;
  41. protected function setUp()
  42. {
  43. // create temp dir
  44. $tempfile = tempnam(sys_get_temp_dir(), 'sonata_admin');
  45. if (file_exists($tempfile)) {
  46. unlink($tempfile);
  47. }
  48. mkdir($tempfile);
  49. $this->tempDirectory = $tempfile;
  50. $bundle = new DemoAdminBundle();
  51. $bundle->setPath($this->tempDirectory);
  52. $kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
  53. $kernel->expects($this->any())
  54. ->method('getBundles')
  55. ->will($this->returnValue(array($bundle)));
  56. $parameterBag = new ParameterBag();
  57. $this->container = new Container($parameterBag);
  58. $kernel->expects($this->any())
  59. ->method('getBundle')
  60. ->with($this->equalTo('AcmeDemoBundle'))
  61. ->will($this->returnValue($bundle));
  62. $this->application = new Application($kernel);
  63. $this->command = new GenerateAdminCommand();
  64. $this->application->add($this->command);
  65. }
  66. public function tearDown()
  67. {
  68. if ($this->tempDirectory) {
  69. if (file_exists($this->tempDirectory.'/Controller/FooAdminController.php')) {
  70. unlink($this->tempDirectory.'/Controller/FooAdminController.php');
  71. }
  72. if (file_exists($this->tempDirectory.'/Admin/FooAdmin.php')) {
  73. unlink($this->tempDirectory.'/Admin/FooAdmin.php');
  74. }
  75. if (file_exists($this->tempDirectory.'/Resources/config/admin.yml')) {
  76. unlink($this->tempDirectory.'/Resources/config/admin.yml');
  77. }
  78. if (is_dir($this->tempDirectory.'/Controller')) {
  79. rmdir($this->tempDirectory.'/Controller');
  80. }
  81. if (is_dir($this->tempDirectory.'/Admin')) {
  82. rmdir($this->tempDirectory.'/Admin');
  83. }
  84. if (is_dir($this->tempDirectory.'/Resources/config')) {
  85. rmdir($this->tempDirectory.'/Resources/config');
  86. }
  87. if (is_dir($this->tempDirectory.'/Resources')) {
  88. rmdir($this->tempDirectory.'/Resources');
  89. }
  90. if (file_exists($this->tempDirectory) && is_dir($this->tempDirectory)) {
  91. rmdir($this->tempDirectory);
  92. }
  93. }
  94. }
  95. public function testExecute()
  96. {
  97. $this->command->setContainer($this->container);
  98. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  99. $command = $this->application->find('sonata:admin:generate');
  100. $commandTester = new CommandTester($command);
  101. $commandTester->execute(array(
  102. 'command' => $command->getName(),
  103. 'model' => 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo',
  104. '--bundle' => 'AcmeDemoBundle',
  105. '--admin' => 'FooAdmin',
  106. '--controller' => 'FooAdminController',
  107. '--services' => 'admin.yml',
  108. '--id' => 'acme_demo_admin.admin.foo',
  109. ), array('interactive' => false));
  110. $expectedOutput = '';
  111. $expectedOutput .= sprintf('%3$sThe admin class "Sonata\AdminBundle\Tests\Fixtures\Bundle\Admin\FooAdmin" has been generated under the file "%1$s%2$sAdmin%2$sFooAdmin.php".%3$s', $this->tempDirectory, DIRECTORY_SEPARATOR, PHP_EOL);
  112. $expectedOutput .= sprintf('%3$sThe controller class "Sonata\AdminBundle\Tests\Fixtures\Bundle\Controller\FooAdminController" has been generated under the file "%1$s%2$sController%2$sFooAdminController.php".%3$s', $this->tempDirectory, DIRECTORY_SEPARATOR, PHP_EOL);
  113. $expectedOutput .= sprintf('%3$sThe service "acme_demo_admin.admin.foo" has been appended to the file "%1$s%2$sResources%2$sconfig%2$sadmin.yml".%3$s', $this->tempDirectory, DIRECTORY_SEPARATOR, PHP_EOL);
  114. $this->assertSame($expectedOutput, $commandTester->getDisplay());
  115. $this->assertFileExists($this->tempDirectory.'/Admin/FooAdmin.php');
  116. $this->assertFileExists($this->tempDirectory.'/Controller/FooAdminController.php');
  117. $this->assertFileExists($this->tempDirectory.'/Resources/config/admin.yml');
  118. $adminContent = file_get_contents($this->tempDirectory.'/Admin/FooAdmin.php');
  119. $this->assertContains('class FooAdmin extends Admin', $adminContent);
  120. $this->assertContains('use Sonata\AdminBundle\Admin\Admin;', $adminContent);
  121. $this->assertContains('use Sonata\AdminBundle\Datagrid\DatagridMapper;', $adminContent);
  122. $this->assertContains('use Sonata\AdminBundle\Datagrid\ListMapper;', $adminContent);
  123. $this->assertContains('use Sonata\AdminBundle\Form\FormMapper;', $adminContent);
  124. $this->assertContains('use Sonata\AdminBundle\Show\ShowMapper;', $adminContent);
  125. $this->assertContains('protected function configureDatagridFilters(DatagridMapper $datagridMapper)', $adminContent);
  126. $this->assertContains('protected function configureListFields(ListMapper $listMapper)', $adminContent);
  127. $this->assertContains('protected function configureFormFields(FormMapper $formMapper)', $adminContent);
  128. $this->assertContains('protected function configureShowFields(ShowMapper $showMapper)', $adminContent);
  129. $controllerContent = file_get_contents($this->tempDirectory.'/Controller/FooAdminController.php');
  130. $this->assertContains('class FooAdminController extends CRUDController', $controllerContent);
  131. $this->assertContains('use Sonata\AdminBundle\Controller\CRUDController;', $controllerContent);
  132. $configServiceContent = file_get_contents($this->tempDirectory.'/Resources/config/admin.yml');
  133. $this->assertContains('services:'."\n".' acme_demo_admin.admin.foo', $configServiceContent);
  134. $this->assertContains(' - { name: sonata.admin, manager_type: foo, group: admin, label: Foo }', $configServiceContent);
  135. }
  136. public function testExecuteWithExceptionNoModelManagers()
  137. {
  138. $this->setExpectedException('RuntimeException', 'There are no model managers registered.');
  139. $this->command->setContainer($this->container);
  140. $command = $this->application->find('sonata:admin:generate');
  141. $commandTester = new CommandTester($command);
  142. $commandTester->execute(array(
  143. 'command' => $command->getName(),
  144. 'model' => 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo',
  145. '--bundle' => 'AcmeDemoBundle',
  146. '--admin' => 'FooAdmin',
  147. '--controller' => 'FooAdminController',
  148. '--services' => 'admin.yml',
  149. '--id' => 'acme_demo_admin.admin.foo',
  150. ), array('interactive' => false));
  151. }
  152. /**
  153. * @dataProvider getExecuteInteractiveTests
  154. */
  155. public function testExecuteInteractive($modelEntity)
  156. {
  157. $this->command->setContainer($this->container);
  158. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  159. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  160. $command = $this->application->find('sonata:admin:generate');
  161. // @todo remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
  162. // DialogHelper does not exist in SensioGeneratorBundle 2.5+
  163. if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
  164. $dialog = $this->getMock('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper', array('askConfirmation', 'askAndValidate'));
  165. $dialog->expects($this->any())
  166. ->method('askConfirmation')
  167. ->will($this->returnCallback(function (OutputInterface $output, $question, $default) {
  168. $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
  169. switch ($questionClean) {
  170. case 'Do you want to generate a controller':
  171. return 'yes';
  172. case 'Do you want to update the services YAML configuration file':
  173. return 'yes';
  174. }
  175. return $default;
  176. }));
  177. $dialog->expects($this->any())
  178. ->method('askAndValidate')
  179. ->will($this->returnCallback(function (OutputInterface $output, $question, $validator, $attempts = false, $default = null) use ($modelEntity) {
  180. $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
  181. switch ($questionClean) {
  182. case 'The fully qualified model class':
  183. return $modelEntity;
  184. case 'The bundle name':
  185. return 'AcmeDemoBundle';
  186. case 'The admin class basename':
  187. return 'FooAdmin';
  188. case 'The controller class basename':
  189. return 'FooAdminController';
  190. case 'The services YAML configuration file':
  191. return 'admin.yml';
  192. case 'The admin service ID':
  193. return 'acme_demo_admin.admin.foo';
  194. case 'The manager type':
  195. return 'foo';
  196. }
  197. return $default;
  198. }));
  199. $command->getHelperSet()->set($dialog, 'dialog');
  200. } else {
  201. $questionHelper = $this->getMock('Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper', array('ask'));
  202. $questionHelper->expects($this->any())
  203. ->method('ask')
  204. ->will($this->returnCallback(function (InputInterface $input, OutputInterface $output, Question $question) use ($modelEntity) {
  205. $questionClean = substr($question->getQuestion(), 6, strpos($question->getQuestion(), '</info>') - 6);
  206. switch ($questionClean) {
  207. // confirmations
  208. case 'Do you want to generate a controller':
  209. return 'yes';
  210. case 'Do you want to update the services YAML configuration file':
  211. return 'yes';
  212. // inputs
  213. case 'The fully qualified model class':
  214. return $modelEntity;
  215. case 'The bundle name':
  216. return 'AcmeDemoBundle';
  217. case 'The admin class basename':
  218. return 'FooAdmin';
  219. case 'The controller class basename':
  220. return 'FooAdminController';
  221. case 'The services YAML configuration file':
  222. return 'admin.yml';
  223. case 'The admin service ID':
  224. return 'acme_demo_admin.admin.foo';
  225. case 'The manager type':
  226. return 'foo';
  227. }
  228. return false;
  229. }));
  230. $command->getHelperSet()->set($questionHelper, 'question');
  231. }
  232. $commandTester = new CommandTester($command);
  233. $commandTester->execute(array(
  234. 'command' => $command->getName(),
  235. 'model' => $modelEntity,
  236. ));
  237. $expectedOutput = PHP_EOL.str_pad('', 41, ' ').PHP_EOL.' Welcome to the Sonata admin generator '.PHP_EOL.str_pad('', 41, ' ').PHP_EOL.PHP_EOL;
  238. $expectedOutput .= sprintf('%3$sThe admin class "Sonata\AdminBundle\Tests\Fixtures\Bundle\Admin\FooAdmin" has been generated under the file "%1$s%2$sAdmin%2$sFooAdmin.php".%3$s', $this->tempDirectory, DIRECTORY_SEPARATOR, PHP_EOL);
  239. $expectedOutput .= sprintf('%3$sThe controller class "Sonata\AdminBundle\Tests\Fixtures\Bundle\Controller\FooAdminController" has been generated under the file "%1$s%2$sController%2$sFooAdminController.php".%3$s', $this->tempDirectory, DIRECTORY_SEPARATOR, PHP_EOL);
  240. $expectedOutput .= sprintf('%3$sThe service "acme_demo_admin.admin.foo" has been appended to the file "%1$s%2$sResources%2$sconfig%2$sadmin.yml".%3$s', $this->tempDirectory, DIRECTORY_SEPARATOR, PHP_EOL);
  241. $this->assertSame($expectedOutput, str_replace("\n", PHP_EOL, str_replace(PHP_EOL, "\n", $commandTester->getDisplay())));
  242. $this->assertFileExists($this->tempDirectory.'/Admin/FooAdmin.php');
  243. $this->assertFileExists($this->tempDirectory.'/Controller/FooAdminController.php');
  244. $this->assertFileExists($this->tempDirectory.'/Resources/config/admin.yml');
  245. $adminContent = file_get_contents($this->tempDirectory.'/Admin/FooAdmin.php');
  246. $this->assertContains('class FooAdmin extends Admin', $adminContent);
  247. $this->assertContains('use Sonata\AdminBundle\Admin\Admin;', $adminContent);
  248. $this->assertContains('use Sonata\AdminBundle\Datagrid\DatagridMapper;', $adminContent);
  249. $this->assertContains('use Sonata\AdminBundle\Datagrid\ListMapper;', $adminContent);
  250. $this->assertContains('use Sonata\AdminBundle\Form\FormMapper;', $adminContent);
  251. $this->assertContains('use Sonata\AdminBundle\Show\ShowMapper;', $adminContent);
  252. $this->assertContains('protected function configureDatagridFilters(DatagridMapper $datagridMapper)', $adminContent);
  253. $this->assertContains('protected function configureListFields(ListMapper $listMapper)', $adminContent);
  254. $this->assertContains('protected function configureFormFields(FormMapper $formMapper)', $adminContent);
  255. $this->assertContains('protected function configureShowFields(ShowMapper $showMapper)', $adminContent);
  256. $controllerContent = file_get_contents($this->tempDirectory.'/Controller/FooAdminController.php');
  257. $this->assertContains('class FooAdminController extends CRUDController', $controllerContent);
  258. $this->assertContains('use Sonata\AdminBundle\Controller\CRUDController;', $controllerContent);
  259. $configServiceContent = file_get_contents($this->tempDirectory.'/Resources/config/admin.yml');
  260. $this->assertContains('services:'."\n".' acme_demo_admin.admin.foo', $configServiceContent);
  261. $this->assertContains(' - { name: sonata.admin, manager_type: foo, group: admin, label: Foo }', $configServiceContent);
  262. }
  263. public function getExecuteInteractiveTests()
  264. {
  265. return array(
  266. array('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'),
  267. array('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'),
  268. );
  269. }
  270. /**
  271. * @dataProvider getValidateManagerTypeTests
  272. */
  273. public function testValidateManagerType($expected, $managerType)
  274. {
  275. $this->command->setContainer($this->container);
  276. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  277. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  278. $this->assertSame($expected, $this->command->validateManagerType($managerType));
  279. }
  280. public function getValidateManagerTypeTests()
  281. {
  282. return array(
  283. array('foo', 'foo'),
  284. array('bar', 'bar'),
  285. );
  286. }
  287. public function testValidateManagerTypeWithException1()
  288. {
  289. $this->command->setContainer($this->container);
  290. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "foo". Available manager types are "".');
  291. $this->command->validateManagerType('foo');
  292. }
  293. public function testValidateManagerTypeWithException2()
  294. {
  295. $this->command->setContainer($this->container);
  296. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  297. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  298. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "foo", "bar".');
  299. $this->command->validateManagerType('baz');
  300. }
  301. public function testValidateManagerTypeWithException3()
  302. {
  303. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "".');
  304. $this->command->validateManagerType('baz');
  305. }
  306. }