ClassMetadataTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Metadata;
  18. use Doctrine\Common\Annotations\AnnotationReader;
  19. use JMS\SecurityExtraBundle\Metadata\Driver\AnnotationDriver;
  20. use Metadata\MetadataFactory;
  21. class ClassMetadataTest extends \PHPUnit_Framework_TestCase
  22. {
  23. /**
  24. * @expectedException \RuntimeException
  25. * @expectedMessage You have overridden a secured method "differentMethodSignature" in "SubService". Please copy over the applicable security metadata, and also add @SatisfiesParentSecurityPolicy.
  26. */
  27. public function testAnalyzeThrowsExceptionWhenSecureMethodIsOverridden()
  28. {
  29. $this->getFactory()->getMetadataForClass('JMS\SecurityExtraBundle\Tests\Fixtures\SubService');
  30. }
  31. public function testAnalyzeThrowsNoExceptionWhenAbstractMethodIsNotOverridenInDirectChildClass()
  32. {
  33. $metadata = $this
  34. ->getFactory()
  35. ->getMetadataForClass('JMS\SecurityExtraBundle\Tests\Fixtures\AbstractMethodNotDirectlyOverwrittenInDirectChildService')
  36. ;
  37. $this->assertTrue(isset($metadata->methodMetadata['abstractMethod']));
  38. $metadata = $metadata->methodMetadata['abstractMethod'];
  39. $this->assertEquals(array('VIEW'), $metadata->returnPermissions);
  40. }
  41. public function testAnalyzeThrowsNoExceptionWhenSatisfiesParentSecurityPolicyIsDefined()
  42. {
  43. $metadata = $this
  44. ->getFactory()
  45. ->getMetadataForClass('JMS\SecurityExtraBundle\Tests\Fixtures\CorrectSubService')
  46. ;
  47. $methods = $metadata->methodMetadata;
  48. $this->assertTrue(isset($methods['differentMethodSignature']));
  49. $metadata = $methods['differentMethodSignature'];
  50. $this->assertEquals(array(), $metadata->roles);
  51. $this->assertEquals(array(), $metadata->paramPermissions);
  52. $this->assertEquals(array('VIEW'), $metadata->returnPermissions);
  53. }
  54. public function testAnalyzeWithComplexHierarchy()
  55. {
  56. $metadata = $this
  57. ->getFactory()
  58. ->getMetadataForClass('JMS\SecurityExtraBundle\Tests\Fixtures\ComplexService')
  59. ;
  60. $methods = $metadata->methodMetadata;
  61. $this->assertTrue(isset($methods['delete'], $methods['retrieve'], $methods['abstractMethod']));
  62. $metadata = $methods['delete'];
  63. $this->assertEquals(array(0 => array('MASTER', 'EDIT'), 2 => array('OWNER')), $metadata->paramPermissions);
  64. $this->assertEquals(array(), $metadata->returnPermissions);
  65. $this->assertEquals(array(), $metadata->roles);
  66. $metadata = $methods['retrieve'];
  67. $this->assertEquals(array('VIEW', 'UNDELETE'), $metadata->returnPermissions);
  68. $this->assertEquals(array(), $metadata->paramPermissions);
  69. $this->assertEquals(array(), $metadata->roles);
  70. $metadata = $methods['abstractMethod'];
  71. $this->assertEquals(array('ROLE_FOO', 'IS_AUTHENTICATED_FULLY'), $metadata->roles);
  72. $this->assertEquals(array(1 => array('FOO')), $metadata->paramPermissions);
  73. $this->assertEquals(array('WOW'), $metadata->returnPermissions);
  74. }
  75. public function testAnalyze()
  76. {
  77. $metadata = $this
  78. ->getFactory()
  79. ->getMetadataForClass('JMS\SecurityExtraBundle\Tests\Fixtures\MainService')
  80. ;
  81. $methods = $metadata->methodMetadata;
  82. $this->assertTrue(isset($methods['differentMethodSignature']));
  83. $metadata = $methods['differentMethodSignature'];
  84. $this->assertEquals(array(array('EDIT')), $metadata->paramPermissions);
  85. $this->assertEquals(array(), $metadata->returnPermissions);
  86. $this->assertEquals(array(), $metadata->roles);
  87. $this->assertFalse($metadata->isDeclaredOnInterface());
  88. }
  89. public function testSerializeUnserialize()
  90. {
  91. $metadata = $this
  92. ->getFactory()
  93. ->getMetadataForClass('JMS\SecurityExtraBundle\Tests\Fixtures\ComplexService')
  94. ;
  95. $this->assertEquals($metadata, unserialize(serialize($metadata)));
  96. }
  97. private function getFactory()
  98. {
  99. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  100. $factory->setIncludeInterfaces(true);
  101. return $factory;
  102. }
  103. }