UserSecurityIdentityTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  12. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  13. class UserSecurityIdentityTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testConstructor()
  16. {
  17. $id = new UserSecurityIdentity('foo', 'Foo');
  18. $this->assertEquals('foo', $id->getUsername());
  19. $this->assertEquals('Foo', $id->getClass());
  20. }
  21. // Test that constructor never changes the type, even for proxies
  22. public function testConstructorWithProxy()
  23. {
  24. $id = new UserSecurityIdentity('foo', 'Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\Foo');
  25. $this->assertEquals('foo', $id->getUsername());
  26. $this->assertEquals('Acme\DemoBundle\Proxy\__CG__\Symfony\Component\Security\Acl\Tests\Domain\Foo', $id->getClass());
  27. }
  28. /**
  29. * @dataProvider getCompareData
  30. */
  31. public function testEquals($id1, $id2, $equal)
  32. {
  33. $this->assertSame($equal, $id1->equals($id2));
  34. }
  35. public function getCompareData()
  36. {
  37. $account = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')
  38. ->setMockClassName('USI_AccountImpl')
  39. ->getMock();
  40. $account
  41. ->expects($this->any())
  42. ->method('getUsername')
  43. ->will($this->returnValue('foo'))
  44. ;
  45. $token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
  46. $token
  47. ->expects($this->any())
  48. ->method('getUser')
  49. ->will($this->returnValue($account))
  50. ;
  51. return array(
  52. array(new UserSecurityIdentity('foo', 'Foo'), new UserSecurityIdentity('foo', 'Foo'), true),
  53. array(new UserSecurityIdentity('foo', 'Bar'), new UserSecurityIdentity('foo', 'Foo'), false),
  54. array(new UserSecurityIdentity('foo', 'Foo'), new UserSecurityIdentity('bar', 'Foo'), false),
  55. array(new UserSecurityIdentity('foo', 'Foo'), UserSecurityIdentity::fromAccount($account), false),
  56. array(new UserSecurityIdentity('bla', 'Foo'), new UserSecurityIdentity('blub', 'Foo'), false),
  57. array(new UserSecurityIdentity('foo', 'Foo'), new RoleSecurityIdentity('foo'), false),
  58. array(new UserSecurityIdentity('foo', 'Foo'), UserSecurityIdentity::fromToken($token), false),
  59. array(new UserSecurityIdentity('foo', 'USI_AccountImpl'), UserSecurityIdentity::fromToken($token), true),
  60. );
  61. }
  62. }