JMSSecurityExtraExtensionTest.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SecurityExtraBundle\Tests\DependencyInjection;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use JMS\SecurityExtraBundle\DependencyInjection\JMSSecurityExtraExtension;
  20. class JMSSecurityExtraExtensionTest extends \PHPUnit_Framework_TestCase
  21. {
  22. private $extension;
  23. public function testConfigLoad()
  24. {
  25. $config = array();
  26. $this->extension->load(array($config), $container = $this->getContainer());
  27. $this->assertTrue($container->hasDefinition('security.access.method_interceptor'));
  28. $this->assertFalse($container->getParameter('security.access.secure_all_services'));
  29. $this->assertFalse($container->getDefinition('security.extra.iddqd_voter')->hasTag('security.voter'));
  30. }
  31. public function testConfigLoadSecureAll()
  32. {
  33. $this->extension->load(array(array('secure_all_services' => true)),
  34. $container = $this->getContainer());
  35. $this->assertTrue($container->getParameter('security.access.secure_all_services'));
  36. }
  37. public function testConfigLoadEnableIddqdAttribute()
  38. {
  39. $this->extension->load(array(array('enable_iddqd_attribute' => true)),
  40. $container = $this->getContainer());
  41. $this->assertTrue($container->getDefinition('security.extra.iddqd_voter')->hasTag('security.voter'));
  42. }
  43. public function testConfigLoadWithMethodAccessControl()
  44. {
  45. $this->extension->load(array(array(
  46. 'expressions' => true,
  47. 'method_access_control' => array(
  48. ':login$' => 'hasRole("FOO")',
  49. )
  50. )), $container = $this->getContainer());
  51. $this->assertEquals(array(':login$' => 'hasRole("FOO")'),
  52. $container->getParameter('security.access.method_access_control'));
  53. }
  54. /**
  55. * @expectedException \RuntimeException
  56. */
  57. public function testConfigLoadThrowsExceptionWhenMethodAccessControlWithoutExpressions()
  58. {
  59. $this->extension->load(array(array(
  60. 'expressions' => false,
  61. 'method_access_control' => array('foo' => 'bar'),
  62. )), $this->getContainer());
  63. }
  64. protected function setUp()
  65. {
  66. $this->extension = new JMSSecurityExtraExtension();
  67. }
  68. private function getContainer()
  69. {
  70. $container = new ContainerBuilder();
  71. $container->setParameter('kernel.cache_dir', sys_get_temp_dir());
  72. $container->setParameter('kernel.bundles', array('JMSAopBundle' => 'JMS\AopBundle\JMSAopBundle'));
  73. return $container;
  74. }
  75. }