123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Tests\Command;
- use Sonata\AdminBundle\Command\GenerateAdminCommand;
- use Sonata\AdminBundle\Tests\Fixtures\Bundle\DemoAdminBundle;
- use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
- use Symfony\Bundle\FrameworkBundle\Console\Application;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Console\Question\Question;
- use Symfony\Component\Console\Tester\CommandTester;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
- /**
- * @author Andrej Hudec <pulzarraider@gmail.com>
- */
- class GenerateAdminCommandTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var Application
- */
- private $application;
- /**
- * @var string
- */
- private $tempDirectory;
- /**
- * @var Container
- */
- private $container;
- /**
- * @var GenerateAdminCommand
- */
- private $command;
- protected function setUp()
- {
- // create temp dir
- $tempfile = tempnam(sys_get_temp_dir(), 'sonata_admin');
- if (file_exists($tempfile)) {
- unlink($tempfile);
- }
- mkdir($tempfile);
- $this->tempDirectory = $tempfile;
- $bundle = new DemoAdminBundle();
- $bundle->setPath($this->tempDirectory);
- $kernel = $this->createMock('Symfony\Component\HttpKernel\KernelInterface');
- $kernel->expects($this->any())
- ->method('getBundles')
- ->will($this->returnValue(array($bundle)));
- $parameterBag = new ParameterBag();
- $this->container = new Container($parameterBag);
- $kernel->expects($this->any())
- ->method('getBundle')
- ->with($this->equalTo('AcmeDemoBundle'))
- ->will($this->returnValue($bundle));
- $kernel->expects($this->any())
- ->method('getContainer')
- ->will($this->returnValue($this->container));
- $this->application = new Application($kernel);
- $this->command = new GenerateAdminCommand();
- $this->application->add($this->command);
- }
- public function tearDown()
- {
- if ($this->tempDirectory) {
- if (file_exists($this->tempDirectory.'/Controller/FooAdminController.php')) {
- unlink($this->tempDirectory.'/Controller/FooAdminController.php');
- }
- if (file_exists($this->tempDirectory.'/Admin/FooAdmin.php')) {
- unlink($this->tempDirectory.'/Admin/FooAdmin.php');
- }
- if (file_exists($this->tempDirectory.'/Resources/config/admin.yml')) {
- unlink($this->tempDirectory.'/Resources/config/admin.yml');
- }
- if (is_dir($this->tempDirectory.'/Controller')) {
- rmdir($this->tempDirectory.'/Controller');
- }
- if (is_dir($this->tempDirectory.'/Admin')) {
- rmdir($this->tempDirectory.'/Admin');
- }
- if (is_dir($this->tempDirectory.'/Resources/config')) {
- rmdir($this->tempDirectory.'/Resources/config');
- }
- if (is_dir($this->tempDirectory.'/Resources')) {
- rmdir($this->tempDirectory.'/Resources');
- }
- if (file_exists($this->tempDirectory) && is_dir($this->tempDirectory)) {
- rmdir($this->tempDirectory);
- }
- }
- }
- public function testExecute()
- {
- $this->command->setContainer($this->container);
- $this->container->set('sonata.admin.manager.foo', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $command = $this->application->find('sonata:admin:generate');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array(
- 'command' => $command->getName(),
- 'model' => 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo',
- '--bundle' => 'AcmeDemoBundle',
- '--admin' => 'FooAdmin',
- '--controller' => 'FooAdminController',
- '--services' => 'admin.yml',
- '--id' => 'acme_demo_admin.admin.foo',
- ), array('interactive' => false));
- $expectedOutput = '';
- $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);
- $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);
- $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);
- $this->assertSame($expectedOutput, $commandTester->getDisplay());
- $this->assertFileExists($this->tempDirectory.'/Admin/FooAdmin.php');
- $this->assertFileExists($this->tempDirectory.'/Controller/FooAdminController.php');
- $this->assertFileExists($this->tempDirectory.'/Resources/config/admin.yml');
- $adminContent = file_get_contents($this->tempDirectory.'/Admin/FooAdmin.php');
- $this->assertContains('class FooAdmin extends AbstractAdmin', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Admin\AbstractAdmin;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Datagrid\DatagridMapper;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Datagrid\ListMapper;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Form\FormMapper;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Show\ShowMapper;', $adminContent);
- $this->assertContains('protected function configureDatagridFilters(DatagridMapper $datagridMapper)', $adminContent);
- $this->assertContains('protected function configureListFields(ListMapper $listMapper)', $adminContent);
- $this->assertContains('protected function configureFormFields(FormMapper $formMapper)', $adminContent);
- $this->assertContains('protected function configureShowFields(ShowMapper $showMapper)', $adminContent);
- $controllerContent = file_get_contents($this->tempDirectory.'/Controller/FooAdminController.php');
- $this->assertContains('class FooAdminController extends CRUDController', $controllerContent);
- $this->assertContains('use Sonata\AdminBundle\Controller\CRUDController;', $controllerContent);
- $configServiceContent = file_get_contents($this->tempDirectory.'/Resources/config/admin.yml');
- $this->assertContains('services:'."\n".' acme_demo_admin.admin.foo', $configServiceContent);
- $this->assertContains(' - { name: sonata.admin, manager_type: foo, group: admin, label: Foo }', $configServiceContent);
- }
- public function testExecuteWithExceptionNoModelManagers()
- {
- $this->expectException('RuntimeException', 'There are no model managers registered.');
- $this->command->setContainer($this->container);
- $command = $this->application->find('sonata:admin:generate');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array(
- 'command' => $command->getName(),
- 'model' => 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo',
- '--bundle' => 'AcmeDemoBundle',
- '--admin' => 'FooAdmin',
- '--controller' => 'FooAdminController',
- '--services' => 'admin.yml',
- '--id' => 'acme_demo_admin.admin.foo',
- ), array('interactive' => false));
- }
- /**
- * @dataProvider getExecuteInteractiveTests
- */
- public function testExecuteInteractive($modelEntity)
- {
- $this->command->setContainer($this->container);
- $this->container->set('sonata.admin.manager.foo', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $this->container->set('sonata.admin.manager.bar', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $command = $this->application->find('sonata:admin:generate');
- // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
- // DialogHelper does not exist in SensioGeneratorBundle 2.5+
- if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
- $dialog = $this->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')
- ->setMethods(array('askConfirmation', 'askAndValidate'))
- ->getMock();
- $dialog->expects($this->any())
- ->method('askConfirmation')
- ->will($this->returnCallback(function (OutputInterface $output, $question, $default) {
- $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
- switch ($questionClean) {
- case 'Do you want to generate a controller':
- return 'yes';
- case 'Do you want to update the services YAML configuration file':
- return 'yes';
- }
- return $default;
- }));
- $dialog->expects($this->any())
- ->method('askAndValidate')
- ->will($this->returnCallback(function (OutputInterface $output, $question, $validator, $attempts = false, $default = null) use ($modelEntity) {
- $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
- switch ($questionClean) {
- case 'The fully qualified model class':
- return $modelEntity;
- case 'The bundle name':
- return 'AcmeDemoBundle';
- case 'The admin class basename':
- return 'FooAdmin';
- case 'The controller class basename':
- return 'FooAdminController';
- case 'The services YAML configuration file':
- return 'admin.yml';
- case 'The admin service ID':
- return 'acme_demo_admin.admin.foo';
- case 'The manager type':
- return 'foo';
- }
- return $default;
- }));
- $command->getHelperSet()->set($dialog, 'dialog');
- } else {
- $questionHelper = $this->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper')
- ->setMethods(array('ask'))
- ->getMock();
- $questionHelper->expects($this->any())
- ->method('ask')
- ->will($this->returnCallback(function (InputInterface $input, OutputInterface $output, Question $question) use ($modelEntity) {
- $questionClean = substr($question->getQuestion(), 6, strpos($question->getQuestion(), '</info>') - 6);
- switch ($questionClean) {
- // confirmations
- case 'Do you want to generate a controller':
- return 'yes';
- case 'Do you want to update the services YAML configuration file':
- return 'yes';
- // inputs
- case 'The fully qualified model class':
- return $modelEntity;
- case 'The bundle name':
- return 'AcmeDemoBundle';
- case 'The admin class basename':
- return 'FooAdmin';
- case 'The controller class basename':
- return 'FooAdminController';
- case 'The services YAML configuration file':
- return 'admin.yml';
- case 'The admin service ID':
- return 'acme_demo_admin.admin.foo';
- case 'The manager type':
- return 'foo';
- }
- return false;
- }));
- $command->getHelperSet()->set($questionHelper, 'question');
- }
- $commandTester = new CommandTester($command);
- $commandTester->execute(array(
- 'command' => $command->getName(),
- 'model' => $modelEntity,
- ));
- $expectedOutput = PHP_EOL.str_pad('', 41, ' ').PHP_EOL.' Welcome to the Sonata admin generator '.PHP_EOL.str_pad('', 41, ' ').PHP_EOL.PHP_EOL;
- $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);
- $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);
- $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);
- $this->assertSame($expectedOutput, str_replace("\n", PHP_EOL, str_replace(PHP_EOL, "\n", $commandTester->getDisplay())));
- $this->assertFileExists($this->tempDirectory.'/Admin/FooAdmin.php');
- $this->assertFileExists($this->tempDirectory.'/Controller/FooAdminController.php');
- $this->assertFileExists($this->tempDirectory.'/Resources/config/admin.yml');
- $adminContent = file_get_contents($this->tempDirectory.'/Admin/FooAdmin.php');
- $this->assertContains('class FooAdmin extends AbstractAdmin', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Admin\AbstractAdmin;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Datagrid\DatagridMapper;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Datagrid\ListMapper;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Form\FormMapper;', $adminContent);
- $this->assertContains('use Sonata\AdminBundle\Show\ShowMapper;', $adminContent);
- $this->assertContains('protected function configureDatagridFilters(DatagridMapper $datagridMapper)', $adminContent);
- $this->assertContains('protected function configureListFields(ListMapper $listMapper)', $adminContent);
- $this->assertContains('protected function configureFormFields(FormMapper $formMapper)', $adminContent);
- $this->assertContains('protected function configureShowFields(ShowMapper $showMapper)', $adminContent);
- $controllerContent = file_get_contents($this->tempDirectory.'/Controller/FooAdminController.php');
- $this->assertContains('class FooAdminController extends CRUDController', $controllerContent);
- $this->assertContains('use Sonata\AdminBundle\Controller\CRUDController;', $controllerContent);
- $configServiceContent = file_get_contents($this->tempDirectory.'/Resources/config/admin.yml');
- $this->assertContains('services:'."\n".' acme_demo_admin.admin.foo', $configServiceContent);
- $this->assertContains(' - { name: sonata.admin, manager_type: foo, group: admin, label: Foo }', $configServiceContent);
- }
- public function getExecuteInteractiveTests()
- {
- return array(
- array('Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo'),
- array('Sonata\AdminBundle\Tests\Fixtures\Entity\Foo'),
- );
- }
- /**
- * @dataProvider getValidateManagerTypeTests
- */
- public function testValidateManagerType($expected, $managerType)
- {
- $this->command->setContainer($this->container);
- $this->container->set('sonata.admin.manager.foo', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $this->container->set('sonata.admin.manager.bar', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $this->assertSame($expected, $this->command->validateManagerType($managerType));
- }
- public function getValidateManagerTypeTests()
- {
- return array(
- array('foo', 'foo'),
- array('bar', 'bar'),
- );
- }
- public function testValidateManagerTypeWithException1()
- {
- $this->command->setContainer($this->container);
- $this->setExpectedException('InvalidArgumentException', 'Invalid manager type "foo". Available manager types are "".');
- $this->command->validateManagerType('foo');
- }
- public function testValidateManagerTypeWithException2()
- {
- $this->command->setContainer($this->container);
- $this->container->set('sonata.admin.manager.foo', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $this->container->set('sonata.admin.manager.bar', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $this->expectException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "foo", "bar".');
- $this->command->validateManagerType('baz');
- }
- public function testValidateManagerTypeWithException3()
- {
- $this->expectException('InvalidArgumentException', 'Invalid manager type "baz". Available manager types are "".');
- $this->command->validateManagerType('baz');
- }
- public function testAnswerUpdateServicesWithNo()
- {
- $this->container->set('sonata.admin.manager.foo', $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface'));
- $modelEntity = 'Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Foo';
- $command = $this->application->find('sonata:admin:generate');
- // NEXT_MAJOR: Remove this BC code for SensioGeneratorBundle 2.3/2.4 after dropping support for Symfony 2.3
- // DialogHelper does not exist in SensioGeneratorBundle 2.5+
- if (class_exists('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')) {
- $dialog = $this->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper')
- ->setMethods(array('askConfirmation', 'askAndValidate'))
- ->getMock();
- $dialog->expects($this->any())
- ->method('askConfirmation')
- ->will($this->returnCallback(function (OutputInterface $output, $question, $default) {
- $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
- switch ($questionClean) {
- case 'Do you want to generate a controller':
- return false;
- case 'Do you want to update the services YAML configuration file':
- return false;
- }
- return $default;
- }));
- $dialog->expects($this->any())
- ->method('askAndValidate')
- ->will($this->returnCallback(function (OutputInterface $output, $question, $validator, $attempts = false, $default = null) use ($modelEntity) {
- $questionClean = substr($question, 6, strpos($question, '</info>') - 6);
- switch ($questionClean) {
- case 'The fully qualified model class':
- return $modelEntity;
- case 'The bundle name':
- return 'AcmeDemoBundle';
- case 'The admin class basename':
- return 'FooAdmin';
- case 'The controller class basename':
- return 'FooAdminController';
- case 'The services YAML configuration file':
- return 'admin.yml';
- case 'The admin service ID':
- return 'acme_demo_admin.admin.foo';
- case 'The manager type':
- return 'foo';
- }
- return $default;
- }));
- $command->getHelperSet()->set($dialog, 'dialog');
- } else {
- $questionHelper = $this->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper')
- ->setMethods(array('ask'))
- ->getMock();
- $questionHelper->expects($this->any())
- ->method('ask')
- ->will($this->returnCallback(function (InputInterface $input, OutputInterface $output, Question $question) use ($modelEntity) {
- $questionClean = substr($question->getQuestion(), 6, strpos($question->getQuestion(), '</info>') - 6);
- switch ($questionClean) {
- // confirmations
- case 'Do you want to generate a controller':
- return false;
- case 'Do you want to update the services YAML configuration file':
- return false;
- // inputs
- case 'The fully qualified model class':
- return $modelEntity;
- case 'The bundle name':
- return 'AcmeDemoBundle';
- case 'The admin class basename':
- return 'FooAdmin';
- }
- return false;
- }));
- $command->getHelperSet()->set($questionHelper, 'question');
- }
- $commandTester = new CommandTester($command);
- $commandTester->execute(array(
- 'command' => $command->getName(),
- 'model' => $modelEntity,
- ));
- $this->assertFalse(file_exists($this->tempDirectory.'/Resources/config/services.yml'));
- }
- }
|