SetupAclCommandTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Command\SetupAclCommand;
  12. use Symfony\Component\Console\Application;
  13. use Symfony\Component\Console\Tester\CommandTester;
  14. use Sonata\AdminBundle\Admin\Pool;
  15. use Sonata\AdminBundle\Util\AdminAclManipulatorInterface;
  16. /**
  17. * @author Andrej Hudec <pulzarraider@gmail.com>
  18. */
  19. class SetupAclCommandTest extends \PHPUnit_Framework_TestCase
  20. {
  21. public function testExecute()
  22. {
  23. $application = new Application();
  24. $command = new SetupAclCommand();
  25. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  26. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  27. $aclManipulator = $this->getMock('Sonata\AdminBundle\Util\AdminAclManipulatorInterface');
  28. $container->expects($this->any())
  29. ->method('get')
  30. ->will($this->returnCallback(function ($id) use ($container, $admin, $aclManipulator) {
  31. switch ($id) {
  32. case 'sonata.admin.pool':
  33. $pool = new Pool($container, '', '');
  34. $pool->setAdminServiceIds(array('acme.admin.foo'));
  35. return $pool;
  36. case 'sonata.admin.manipulator.acl.admin':
  37. return $aclManipulator;
  38. case 'acme.admin.foo':
  39. return $admin;
  40. }
  41. return null;
  42. }));
  43. $command->setContainer($container);
  44. $application->add($command);
  45. $command = $application->find('sonata:admin:setup-acl');
  46. $commandTester = new CommandTester($command);
  47. $commandTester->execute(array('command' => $command->getName()));
  48. $this->assertRegExp('/Starting ACL AdminBundle configuration/', $commandTester->getDisplay());
  49. }
  50. public function testExecuteWithException1()
  51. {
  52. $application = new Application();
  53. $command = new SetupAclCommand();
  54. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  55. $container->expects($this->any())
  56. ->method('get')
  57. ->will($this->returnCallback(function ($id) use ($container) {
  58. if ($id == 'sonata.admin.pool') {
  59. $pool = new Pool($container, '', '');
  60. $pool->setAdminServiceIds(array('acme.admin.foo'));
  61. return $pool;
  62. }
  63. throw new \Exception('Foo Exception');
  64. }));
  65. $command->setContainer($container);
  66. $application->add($command);
  67. $command = $application->find('sonata:admin:setup-acl');
  68. $commandTester = new CommandTester($command);
  69. $commandTester->execute(array('command' => $command->getName()));
  70. $this->assertRegExp('@Starting ACL AdminBundle configuration\s+Warning : The admin class cannot be initiated from the command line\s+Foo Exception@', $commandTester->getDisplay());
  71. }
  72. public function testExecuteWithException2()
  73. {
  74. $application = new Application();
  75. $command = new SetupAclCommand();
  76. $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
  77. $admin = $this->getMock('Sonata\AdminBundle\Admin\AdminInterface');
  78. $container->expects($this->any())
  79. ->method('get')
  80. ->will($this->returnCallback(function ($id) use ($container, $admin) {
  81. switch ($id) {
  82. case 'sonata.admin.pool':
  83. $pool = new Pool($container, '', '');
  84. $pool->setAdminServiceIds(array('acme.admin.foo'));
  85. return $pool;
  86. case 'sonata.admin.manipulator.acl.admin':
  87. return new \stdClass();
  88. case 'acme.admin.foo':
  89. return $admin;
  90. }
  91. return null;
  92. }));
  93. $command->setContainer($container);
  94. $application->add($command);
  95. $command = $application->find('sonata:admin:setup-acl');
  96. $commandTester = new CommandTester($command);
  97. $commandTester->execute(array('command' => $command->getName()));
  98. $this->assertRegExp('@Starting ACL AdminBundle configuration\s+The interface "AdminAclManipulatorInterface" is not implemented for stdClass: ignoring@', $commandTester->getDisplay());
  99. }
  100. }