123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?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\Admin\Pool;
- use Sonata\AdminBundle\Command\ExplainAdminCommand;
- use Sonata\AdminBundle\Route\RouteCollection;
- use Sonata\AdminBundle\Tests\Helpers\PHPUnit_Framework_TestCase;
- use Symfony\Component\Console\Application;
- use Symfony\Component\Console\Tester\CommandTester;
- use Symfony\Component\Validator\Constraints\Email;
- use Symfony\Component\Validator\Constraints\Length;
- use Symfony\Component\Validator\Constraints\NotNull;
- /**
- * @author Andrej Hudec <pulzarraider@gmail.com>
- */
- class ExplainAdminCommandTest extends PHPUnit_Framework_TestCase
- {
- /**
- * @var Application
- */
- private $application;
- /**
- * @var AdminInterface
- */
- private $admin;
- /**
- * @var Symfony\Component\Validator\MetadataFactoryInterface
- */
- private $validatorFactory;
- protected function setUp()
- {
- $this->application = new Application();
- $command = new ExplainAdminCommand();
- $container = $this->createMock('Symfony\Component\DependencyInjection\ContainerInterface');
- $this->admin = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $this->admin->expects($this->any())
- ->method('getCode')
- ->will($this->returnValue('foo'));
- $this->admin->expects($this->any())
- ->method('getClass')
- ->will($this->returnValue('Acme\Entity\Foo'));
- $this->admin->expects($this->any())
- ->method('getBaseControllerName')
- ->will($this->returnValue('SonataAdminBundle:CRUD'));
- $routeCollection = new RouteCollection('foo', 'fooBar', 'foo-bar', 'SonataAdminBundle:CRUD');
- $routeCollection->add('list');
- $routeCollection->add('edit');
- $this->admin->expects($this->any())
- ->method('getRoutes')
- ->will($this->returnValue($routeCollection));
- $fieldDescription1 = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription1->expects($this->any())
- ->method('getType')
- ->will($this->returnValue('text'));
- $fieldDescription1->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:foo_text.html.twig'));
- $fieldDescription2 = $this->createMock('Sonata\AdminBundle\Admin\FieldDescriptionInterface');
- $fieldDescription2->expects($this->any())
- ->method('getType')
- ->will($this->returnValue('datetime'));
- $fieldDescription2->expects($this->any())
- ->method('getTemplate')
- ->will($this->returnValue('SonataAdminBundle:CRUD:bar_datetime.html.twig'));
- $this->admin->expects($this->any())
- ->method('getListFieldDescriptions')
- ->will($this->returnValue(array(
- 'fooTextField' => $fieldDescription1,
- 'barDateTimeField' => $fieldDescription2,
- )));
- $this->admin->expects($this->any())
- ->method('getFilterFieldDescriptions')
- ->will($this->returnValue(array(
- 'fooTextField' => $fieldDescription1,
- 'barDateTimeField' => $fieldDescription2,
- )));
- $this->admin->expects($this->any())
- ->method('getFormTheme')
- ->will($this->returnValue(array('FooBundle::bar.html.twig')));
- $this->admin->expects($this->any())
- ->method('getFormFieldDescriptions')
- ->will($this->returnValue(array(
- 'fooTextField' => $fieldDescription1,
- 'barDateTimeField' => $fieldDescription2,
- )));
- $this->admin->expects($this->any())
- ->method('isChild')
- ->will($this->returnValue(true));
- // php 5.3 BC
- $adminParent = $this->createMock('Sonata\AdminBundle\Admin\AdminInterface');
- $adminParent->expects($this->any())
- ->method('getCode')
- ->will($this->returnValue('foo_child'));
- $this->admin->expects($this->any())
- ->method('getParent')
- ->will($this->returnCallback(function () use ($adminParent) {
- return $adminParent;
- }));
- if (interface_exists(
- 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'
- )) { // Prefer Symfony 2.5+ interfaces
- $this->validatorFactory = $this->createMock(
- 'Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface'
- );
- $validator = $this->createMock('Symfony\Component\Validator\Validator\ValidatorInterface');
- $validator->expects($this->any())->method('getMetadataFor')->will(
- $this->returnValue($this->validatorFactory)
- );
- } else {
- $this->validatorFactory = $this->createMock('Symfony\Component\Validator\MetadataFactoryInterface');
- $validator = $this->createMock('Symfony\Component\Validator\ValidatorInterface');
- $validator->expects($this->any())->method('getMetadataFactory')->will(
- $this->returnValue($this->validatorFactory)
- );
- }
- // php 5.3 BC
- $admin = $this->admin;
- $validatorFactory = $this->validatorFactory;
- $container->expects($this->any())
- ->method('get')
- ->will($this->returnCallback(function ($id) use ($container, $admin, $validator, $validatorFactory) {
- switch ($id) {
- case 'sonata.admin.pool':
- $pool = new Pool($container, '', '');
- $pool->setAdminServiceIds(array('acme.admin.foo', 'acme.admin.bar'));
- return $pool;
- case 'validator.validator_factory':
- return $validatorFactory;
- case 'validator':
- return $validator;
- case 'acme.admin.foo':
- return $admin;
- }
- return;
- }));
- $container->expects($this->any())->method('has')->will($this->returnValue(true));
- $command->setContainer($container);
- $this->application->add($command);
- }
- public function testExecute()
- {
- // NEXT_MAJOR: Remove check, when bumping requirements to SF 2.5+
- if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
- $metadata = $this->createMock('Symfony\Component\Validator\Mapping\MetadataInterface');
- } else {
- $metadata = $this->createMock('Symfony\Component\Validator\MetadataInterface');
- }
- $this->validatorFactory->expects($this->once())
- ->method('getMetadataFor')
- ->with($this->equalTo('Acme\Entity\Foo'))
- ->will($this->returnValue($metadata));
- // NEXT_MAJOR: Remove check, when bumping requirements to SF 2.5+
- if (class_exists('Symfony\Component\Validator\Mapping\GenericMetadata')) {
- $class = 'GenericMetadata';
- } else {
- // Symfony <2.5 compatibility
- $class = 'ElementMetadata';
- }
- $propertyMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
- $propertyMetadata->constraints = array(
- new NotNull(),
- new Length(array('min' => 2, 'max' => 50, 'groups' => array('create', 'edit'))),
- );
- $metadata->properties = array('firstName' => $propertyMetadata);
- $getterMetadata = $this->getMockForAbstractClass('Symfony\Component\Validator\Mapping\\'.$class);
- $getterMetadata->constraints = array(
- new NotNull(),
- new Email(array('groups' => array('registration', 'edit'))),
- );
- $metadata->getters = array('email' => $getterMetadata);
- $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->any())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
- $this->admin->expects($this->any())
- ->method('getFormBuilder')
- ->will($this->returnValue($formBuilder));
- $datagridBuilder = $this->createMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
- $this->admin->expects($this->any())
- ->method('getDatagridBuilder')
- ->will($this->returnValue($datagridBuilder));
- $listBuilder = $this->createMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
- $this->admin->expects($this->any())
- ->method('getListBuilder')
- ->will($this->returnValue($listBuilder));
- $command = $this->application->find('sonata:admin:explain');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
- $this->assertSame(sprintf(
- str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin.txt')),
- get_class($this->admin),
- get_class($modelManager),
- get_class($formBuilder),
- get_class($datagridBuilder),
- get_class($listBuilder)
- ), $commandTester->getDisplay());
- }
- public function testExecuteEmptyValidator()
- {
- if (interface_exists('Symfony\Component\Validator\Mapping\MetadataInterface')) { //sf2.5+
- $metadata = $this->createMock('Symfony\Component\Validator\Mapping\MetadataInterface');
- } else {
- $metadata = $this->createMock('Symfony\Component\Validator\MetadataInterface');
- }
- $this->validatorFactory->expects($this->once())
- ->method('getMetadataFor')
- ->with($this->equalTo('Acme\Entity\Foo'))
- ->will($this->returnValue($metadata));
- $metadata->properties = array();
- $metadata->getters = array();
- $modelManager = $this->createMock('Sonata\AdminBundle\Model\ModelManagerInterface');
- $this->admin->expects($this->any())
- ->method('getModelManager')
- ->will($this->returnValue($modelManager));
- $formBuilder = $this->createMock('Symfony\Component\Form\FormBuilderInterface');
- $this->admin->expects($this->any())
- ->method('getFormBuilder')
- ->will($this->returnValue($formBuilder));
- $datagridBuilder = $this->createMock('\Sonata\AdminBundle\Builder\DatagridBuilderInterface');
- $this->admin->expects($this->any())
- ->method('getDatagridBuilder')
- ->will($this->returnValue($datagridBuilder));
- $listBuilder = $this->createMock('Sonata\AdminBundle\Builder\ListBuilderInterface');
- $this->admin->expects($this->any())
- ->method('getListBuilder')
- ->will($this->returnValue($listBuilder));
- $command = $this->application->find('sonata:admin:explain');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array('command' => $command->getName(), 'admin' => 'acme.admin.foo'));
- $this->assertSame(sprintf(
- str_replace(
- "\n",
- PHP_EOL,
- file_get_contents(__DIR__.'/../Fixtures/Command/explain_admin_empty_validator.txt')
- ),
- get_class($this->admin),
- get_class($modelManager),
- get_class($formBuilder),
- get_class($datagridBuilder),
- get_class($listBuilder)
- ), $commandTester->getDisplay());
- }
- public function testExecuteNonAdminService()
- {
- try {
- $command = $this->application->find('sonata:admin:explain');
- $commandTester = new CommandTester($command);
- $commandTester->execute(array('command' => $command->getName(), 'admin' => 'nonexistent.service'));
- } catch (\RuntimeException $e) {
- $this->assertSame('Service "nonexistent.service" is not an admin class', $e->getMessage());
- return;
- }
- $this->fail('An expected exception has not been raised.');
- }
- }
|