ObjectIdentityTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Security\Acl\Tests\Domain
  11. {
  12. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  13. class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $id = new ObjectIdentity('fooid', 'footype');
  18. $this->assertEquals('fooid', $id->getIdentifier());
  19. $this->assertEquals('footype', $id->getType());
  20. }
  21. // Test that constructor never changes passed type, even with proxies
  22. public function testConstructorWithProxy()
  23. {
  24. $id = new ObjectIdentity('fooid', 'Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject');
  25. $this->assertEquals('fooid', $id->getIdentifier());
  26. $this->assertEquals('Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject', $id->getType());
  27. }
  28. public function testFromDomainObjectPrefersInterfaceOverGetId()
  29. {
  30. $domainObject = $this->getMock('Symfony\Component\Security\Acl\Model\DomainObjectInterface');
  31. $domainObject
  32. ->expects($this->once())
  33. ->method('getObjectIdentifier')
  34. ->will($this->returnValue('getObjectIdentifier()'))
  35. ;
  36. $domainObject
  37. ->expects($this->never())
  38. ->method('getId')
  39. ->will($this->returnValue('getId()'))
  40. ;
  41. $id = ObjectIdentity::fromDomainObject($domainObject);
  42. $this->assertEquals('getObjectIdentifier()', $id->getIdentifier());
  43. }
  44. public function testFromDomainObjectWithoutInterface()
  45. {
  46. $id = ObjectIdentity::fromDomainObject(new TestDomainObject());
  47. $this->assertEquals('getId()', $id->getIdentifier());
  48. $this->assertEquals('Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject', $id->getType());
  49. }
  50. public function testFromDomainObjectWithProxy()
  51. {
  52. $id = ObjectIdentity::fromDomainObject(new \Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject());
  53. $this->assertEquals('getId()', $id->getIdentifier());
  54. $this->assertEquals('Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject', $id->getType());
  55. }
  56. public function testFromDomainObjectWithoutInterfaceEnforcesStringIdentifier()
  57. {
  58. $domainObject = new TestDomainObject();
  59. $domainObject->id = 1;
  60. $id = ObjectIdentity::fromDomainObject($domainObject);
  61. $this->assertSame('1', $id->getIdentifier());
  62. $this->assertEquals('Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject', $id->getType());
  63. }
  64. public function testFromDomainObjectWithoutInterfaceAllowsZeroAsIdentifier()
  65. {
  66. $domainObject = new TestDomainObject();
  67. $domainObject->id = '0';
  68. $id = ObjectIdentity::fromDomainObject($domainObject);
  69. $this->assertSame('0', $id->getIdentifier());
  70. $this->assertEquals('Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject', $id->getType());
  71. }
  72. /**
  73. * @dataProvider getCompareData
  74. */
  75. public function testEquals($oid1, $oid2, $equal)
  76. {
  77. if ($equal) {
  78. $this->assertTrue($oid1->equals($oid2));
  79. } else {
  80. $this->assertFalse($oid1->equals($oid2));
  81. }
  82. }
  83. public function getCompareData()
  84. {
  85. return array(
  86. array(new ObjectIdentity('123', 'foo'), new ObjectIdentity('123', 'foo'), true),
  87. array(new ObjectIdentity('123', 'foo'), new ObjectIdentity(123, 'foo'), true),
  88. array(new ObjectIdentity('1', 'foo'), new ObjectIdentity('2', 'foo'), false),
  89. array(new ObjectIdentity('1', 'bla'), new ObjectIdentity('1', 'blub'), false),
  90. );
  91. }
  92. }
  93. class TestDomainObject
  94. {
  95. public $id = 'getId()';
  96. public function getObjectIdentifier()
  97. {
  98. return 'getObjectIdentifier()';
  99. }
  100. public function getId()
  101. {
  102. return $this->id;
  103. }
  104. }
  105. }
  106. namespace Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain
  107. {
  108. class TestDomainObject extends \Symfony\Component\Security\Acl\Tests\Domain\TestDomainObject
  109. {
  110. }
  111. }