GenerateAdminCommandTest.php 18 KB

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