GenerateAdminCommandTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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\Output\OutputInterface;
  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->assertEquals($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. $dialog = $this->getMock('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper', array('askConfirmation', 'askAndValidate'));
  162. $dialog->expects($this->any())
  163. ->method('askConfirmation')
  164. ->will($this->returnCallback(function(OutputInterface $output, $question, $default) {
  165. $questionClean = substr($question, 6, strpos($question, '</info>')-6);
  166. switch ($questionClean) {
  167. case 'Do you want to generate a controller':
  168. return 'yes';
  169. break;
  170. case 'Do you want to update the services YAML configuration file':
  171. return 'yes';
  172. break;
  173. }
  174. return $default;
  175. }));
  176. $dialog->expects($this->any())
  177. ->method('askAndValidate')
  178. ->will($this->returnCallback(function(OutputInterface $output, $question, $validator, $attempts = false, $default = null) use ($modelEntity) {
  179. $questionClean = substr($question, 6, strpos($question, '</info>')-6);
  180. switch ($questionClean) {
  181. case 'The fully qualified model class':
  182. return $modelEntity;;
  183. break;
  184. case 'The bundle name':
  185. return 'AcmeDemoBundle';
  186. break;
  187. case 'The admin class basename':
  188. return 'FooAdmin';
  189. break;
  190. case 'The controller class basename':
  191. return 'FooAdminController';
  192. break;
  193. case 'The services YAML configuration file':
  194. return 'admin.yml';
  195. break;
  196. case 'The admin service ID':
  197. return 'acme_demo_admin.admin.foo';
  198. break;
  199. case 'The manager type':
  200. return 'foo';
  201. break;
  202. }
  203. return $default;
  204. }));
  205. $command->getHelperSet()->set($dialog, 'dialog');
  206. $commandTester = new CommandTester($command);
  207. $commandTester->execute(array(
  208. 'command' => $command->getName(),
  209. 'model' => $modelEntity,
  210. ));
  211. $expectedOutput = PHP_EOL.str_pad('', 41, ' ').PHP_EOL.' Welcome to the Sonata admin generator '.PHP_EOL.str_pad('', 41, ' ').PHP_EOL.PHP_EOL;
  212. $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);
  213. $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);
  214. $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);
  215. $this->assertEquals($expectedOutput, str_replace("\n", PHP_EOL, str_replace(PHP_EOL, "\n", $commandTester->getDisplay())));
  216. $this->assertFileExists($this->tempDirectory.'/Admin/FooAdmin.php');
  217. $this->assertFileExists($this->tempDirectory.'/Controller/FooAdminController.php');
  218. $this->assertFileExists($this->tempDirectory.'/Resources/config/admin.yml');
  219. $adminContent = file_get_contents($this->tempDirectory.'/Admin/FooAdmin.php');
  220. $this->assertContains('class FooAdmin extends Admin', $adminContent);
  221. $this->assertContains('use Sonata\AdminBundle\Admin\Admin;', $adminContent);
  222. $this->assertContains('use Sonata\AdminBundle\Datagrid\DatagridMapper;', $adminContent);
  223. $this->assertContains('use Sonata\AdminBundle\Datagrid\ListMapper;', $adminContent);
  224. $this->assertContains('use Sonata\AdminBundle\Form\FormMapper;', $adminContent);
  225. $this->assertContains('use Sonata\AdminBundle\Show\ShowMapper;', $adminContent);
  226. $this->assertContains('protected function configureDatagridFilters(DatagridMapper $datagridMapper)', $adminContent);
  227. $this->assertContains('protected function configureListFields(ListMapper $listMapper)', $adminContent);
  228. $this->assertContains('protected function configureFormFields(FormMapper $formMapper)', $adminContent);
  229. $this->assertContains('protected function configureShowFields(ShowMapper $showMapper)', $adminContent);
  230. $controllerContent = file_get_contents($this->tempDirectory.'/Controller/FooAdminController.php');
  231. $this->assertContains('class FooAdminController extends CRUDController', $controllerContent);
  232. $this->assertContains('use Sonata\AdminBundle\Controller\CRUDController;', $controllerContent);
  233. $configServiceContent = file_get_contents($this->tempDirectory.'/Resources/config/admin.yml');
  234. $this->assertContains('services:'."\n".' acme_demo_admin.admin.foo', $configServiceContent);
  235. $this->assertContains(' - {name: sonata.admin, manager_type: foo, group: admin, label: Foo}', $configServiceContent);
  236. }
  237. public function getExecuteInteractiveTests()
  238. {
  239. return array(
  240. array('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'),
  241. array('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'),
  242. );
  243. }
  244. /**
  245. * @dataProvider getValidateManagerTypeTests
  246. */
  247. public function testValidateManagerType($expected, $managerType)
  248. {
  249. $this->command->setContainer($this->container);
  250. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  251. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  252. $this->assertEquals($expected, $this->command->validateManagerType($managerType));
  253. }
  254. public function getValidateManagerTypeTests()
  255. {
  256. return array(
  257. array('foo', 'foo'),
  258. array('bar', 'bar'),
  259. );
  260. }
  261. public function testValidateManagerTypeWithException1()
  262. {
  263. $this->command->setContainer($this->container);
  264. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "foo". Available manager types are "".');
  265. $this->command->validateManagerType('foo');
  266. }
  267. public function testValidateManagerTypeWithException2()
  268. {
  269. $this->command->setContainer($this->container);
  270. $this->container->set('sonata.admin.manager.foo', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  271. $this->container->set('sonata.admin.manager.bar', $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
  272. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "foo", "bar".');
  273. $this->command->validateManagerType('baz');
  274. }
  275. public function testValidateManagerTypeWithException3()
  276. {
  277. $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "".');
  278. $this->command->validateManagerType('baz');
  279. }
  280. }