ObjectIdentityTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Tests\Component\Security\Acl\Domain;
  11. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  12. class ObjectIdentityTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructor()
  15. {
  16. $id = new ObjectIdentity('fooid', 'footype');
  17. $this->assertEquals('fooid', $id->getIdentifier());
  18. $this->assertEquals('footype', $id->getType());
  19. }
  20. public function testFromDomainObjectPrefersInterfaceOverGetId()
  21. {
  22. $domainObject = $this->getMock('Symfony\Component\Security\Acl\Model\DomainObjectInterface');
  23. $domainObject
  24. ->expects($this->once())
  25. ->method('getObjectIdentifier')
  26. ->will($this->returnValue('getObjectIdentifier()'))
  27. ;
  28. $domainObject
  29. ->expects($this->never())
  30. ->method('getId')
  31. ->will($this->returnValue('getId()'))
  32. ;
  33. $id = ObjectIdentity::fromDomainObject($domainObject);
  34. $this->assertEquals('getObjectIdentifier()', $id->getIdentifier());
  35. }
  36. public function testFromDomainObjectWithoutInterface()
  37. {
  38. $id = ObjectIdentity::fromDomainObject(new TestDomainObject());
  39. $this->assertEquals('getId()', $id->getIdentifier());
  40. }
  41. /**
  42. * @dataProvider getCompareData
  43. */
  44. public function testEquals($oid1, $oid2, $equal)
  45. {
  46. if ($equal) {
  47. $this->assertTrue($oid1->equals($oid2));
  48. } else {
  49. $this->assertFalse($oid1->equals($oid2));
  50. }
  51. }
  52. public function getCompareData()
  53. {
  54. return array(
  55. array(new ObjectIdentity('123', 'foo'), new ObjectIdentity('123', 'foo'), true),
  56. array(new ObjectIdentity('123', 'foo'), new ObjectIdentity(123, 'foo'), true),
  57. array(new ObjectIdentity('1', 'foo'), new ObjectIdentity('2', 'foo'), false),
  58. array(new ObjectIdentity('1', 'bla'), new ObjectIdentity('1', 'blub'), false),
  59. );
  60. }
  61. public function setUp()
  62. {
  63. if (!class_exists('Doctrine\DBAL\DriverManager')) {
  64. $this->markTestSkipped('The Doctrine2 DBAL is required for this test');
  65. }
  66. }
  67. }
  68. class TestDomainObject
  69. {
  70. public function getObjectIdentifier()
  71. {
  72. return 'getObjectIdentifier()';
  73. }
  74. public function getId()
  75. {
  76. return 'getId()';
  77. }
  78. }