GenerateAdminCommandTest.php 18 KB

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