ObjectIdentityTest.php 2.3 KB

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