GenerateAdminCommandTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. break;
  173. case 'Do you want to update the services YAML configuration file':
  174. return 'yes';
  175. break;
  176. }
  177. return $default;
  178. }));
  179. $dialog->expects($this->any())
  180. ->method('askAndValidate')
  181. ->will($this->returnCallback(function (OutputInterface $output, $question, $validator, $attempts = false, $default = null) use ($modelEntity) {
  182. $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
  183. switch ($questionClean) {
  184. case 'The fully qualified model class':
  185. return $modelEntity;
  186. break;
  187. case 'The bundle name':
  188. return 'AcmeDemoBundle';
  189. break;
  190. case 'The admin class basename':
  191. return 'FooAdmin';
  192. break;
  193. case 'The controller class basename':
  194. return 'FooAdminController';
  195. break;
  196. case 'The services YAML configuration file':
  197. return 'admin.yml';
  198. break;
  199. case 'The admin service ID':
  200. return 'acme_demo_admin.admin.foo';
  201. break;
  202. case 'The manager type':
  203. return 'foo';
  204. break;
  205. }
  206. return $default;
  207. }));
  208. $command->getHelperSet()->set($dialog, 'dialog');
  209. } else {
  210. $questionHelper = $this->getMock('Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper', array('ask'));
  211. $questionHelper->expects($this->any())
  212. ->method('ask')
  213. ->will($this->returnCallback(function (InputInterface $input, OutputInterface $output, Question $question) use ($modelEntity) {
  214. $questionClean = substr($question->getQuestion(), 6, strpos($question->getQuestion(), '</info>') - 6);
  215. switch ($questionClean) {
  216. // confirmations
  217. case 'Do you want to generate a controller':
  218. return 'yes';
  219. break;
  220. case 'Do you want to update the services YAML configuration file':
  221. return 'yes';
  222. break;
  223. // inputs
  224. case 'The fully qualified model class':
  225. return $modelEntity;
  226. break;
  227. case 'The bundle name':
  228. return 'AcmeDemoBundle';
  229. break;
  230. case 'The admin class basename':
  231. return 'FooAdmin';
  232. break;
  233. case 'The controller class basename':
  234. return 'FooAdminController';
  235. break;
  236. case 'The services YAML configuration file':
  237. return 'admin.yml';
  238. break;
  239. case 'The admin service ID':
  240. return 'acme_demo_admin.admin.foo';
  241. break;
  242. case 'The manager type':
  243. return 'foo';
  244. break;
  245. }
  246. return false;
  247. }));
  248. $command->getHelperSet()->set($questionHelper, 'question');
  249. }
  250. $commandTester = new CommandTester($command);
  251. $commandTester->execute(array(
  252. 'command' => $command->getName(),
  253. 'model' => $modelEntity,
  254. ));
  255. $expectedOutput = PHP_EOL.str_pad('', 41, ' ').PHP_EOL.' Welcome to the Sonata admin generator '.PHP_EOL.str_pad('', 41, ' ').PHP_EOL.PHP_EOL;
  256. $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);
  257. $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);
  258. $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);
  259. $this->assertSame($expectedOutput, str_replace("\n", PHP_EOL, str_replace(PHP_EOL, "\n", $commandTester->getDisplay())));
  260. $this->assertFileExists($this->tempDirectory.'/Admin/FooAdmin.php');
  261. $this->assertFileExists($this->tempDirectory.'/Controller/FooAdminController.php');
  262. $this->assertFileExists($this->tempDirectory.'/Resources/config/admin.yml');
  263. $adminContent = file_get_contents($this->tempDirectory.'/Admin/FooAdmin.php');
  264. $this->assertContains('class FooAdmin extends Admin', $adminContent);
  265. $this->assertContains('use Sonata\AdminBundle\Admin\Admin;', $adminContent);
  266. $this->assertContains('use Sonata\AdminBundle\Datagrid\DatagridMapper;', $adminContent);
  267. $this->assertContains('use Sonata\AdminBundle\Datagrid\ListMapper;', $adminContent);
  268. $this->assertContains('use Sonata\AdminBundle\Form\FormMapper;', $adminContent);
  269. $this->assertContains('use Sonata\AdminBundle\Show\ShowMapper;', $adminContent);
  270. $this->assertContains('protected function configureDatagridFilters(DatagridMapper $datagridMapper)', $adminContent);
  271. $this->assertContains('protected function configureListFields(ListMapper $listMapper)', $adminContent);
  272. $this->assertContains('protected function configureFormFields(FormMapper $formMapper)', $adminContent);
  273. $this->assertContains('protected function configureShowFields(ShowMapper $showMapper)', $adminContent);
  274. $controllerContent = file_get_contents($this->tempDirectory.'/Controller/FooAdminController.php');
  275. $this->assertContains('class FooAdminController extends CRUDController', $controllerContent);
  276. $this->assertContains('use Sonata\AdminBundle\Controller\CRUDController;', $controllerContent);
  277. $configServiceContent = file_get_contents($this->tempDirectory.'/Resources/config/admin.yml');
  278. $this->assertContains('services:'."\n".' acme_demo_admin.admin.foo', $configServiceContent);
  279. $this->assertContains(' - {name: sonata.admin, manager_type: foo, group: admin, label: Foo}', $configServiceContent);
  280. }
  281. public function getExecuteInteractiveTests()
  282. {
  283. return array(
  284. array('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'),
  285. array('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'),
  286. );
  287. }
  288. /**
  289. * @dataProvider getValidateManagerTypeTests
  290. */
  291. public function testValidateManagerType($expected, $managerType)
  292. {
  293. $this->command->setContainer($this->container);
  294. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  295. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  296. $this->assertSame($expected, $this->command->validateManagerType($managerType));
  297. }
  298. public function getValidateManagerTypeTests()
  299. {
  300. return array(
  301. array('foo', 'foo'),
  302. array('bar', 'bar'),
  303. );
  304. }
  305. public function testValidateManagerTypeWithException1()
  306. {
  307. $this->command->setContainer($this->container);
  308. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "foo". Available manager types are "".');
  309. $this->command->validateManagerType('foo');
  310. }
  311. public function testValidateManagerTypeWithException2()
  312. {
  313. $this->command->setContainer($this->container);
  314. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  315. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  316. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "foo", "bar".');
  317. $this->command->validateManagerType('baz');
  318. }
  319. public function testValidateManagerTypeWithException3()
  320. {
  321. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "".');
  322. $this->command->validateManagerType('baz');
  323. }
  324. }