PropelUserManagerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Propel;
  11. class PropelUserManagerTest extends \PHPUnit_Framework_TestCase
  12. {
  13. protected $userManager;
  14. public function testFindUserByUsername()
  15. {
  16. $this->userManager->expects($this->once())
  17. ->method('findUserBy')
  18. ->with($this->equalTo(array('usernameCanonical' => 'jack')));
  19. $this->userManager->expects($this->once())
  20. ->method('canonicalizeUsername')
  21. ->with($this->equalTo('jack'))
  22. ->will($this->returnValue('jack'));
  23. $this->userManager->findUserByUsername('jack');
  24. }
  25. public function testFindUserByUsernameLowercasesTheUsername()
  26. {
  27. $this->userManager->expects($this->once())
  28. ->method('findUserBy')
  29. ->with($this->equalTo(array('usernameCanonical' => 'jack')));
  30. $this->userManager->expects($this->once())
  31. ->method('canonicalizeUsername')
  32. ->with($this->equalTo('JaCk'))
  33. ->will($this->returnValue('jack'));
  34. $this->userManager->findUserByUsername('JaCk');
  35. }
  36. public function testFindUserByEmail()
  37. {
  38. $this->userManager->expects($this->once())
  39. ->method('findUserBy')
  40. ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
  41. $this->userManager->expects($this->once())
  42. ->method('canonicalizeEmail')
  43. ->with($this->equalTo('jack@email.org'))
  44. ->will($this->returnValue('jack@email.org'));
  45. $this->userManager->findUserByEmail('jack@email.org');
  46. }
  47. public function testFindUserByEmailLowercasesTheEmail()
  48. {
  49. $this->userManager->expects($this->once())
  50. ->method('findUserBy')
  51. ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
  52. $this->userManager->expects($this->once())
  53. ->method('canonicalizeEmail')
  54. ->with($this->equalTo('JaCk@EmAiL.oRg'))
  55. ->will($this->returnValue('jack@email.org'));
  56. $this->userManager->findUserByEmail('JaCk@EmAiL.oRg');
  57. }
  58. public function testFindUserByUsernameOrEmailWithUsername()
  59. {
  60. $this->userManager->expects($this->once())
  61. ->method('findUserBy')
  62. ->with($this->equalTo(array('usernameCanonical' => 'jack')));
  63. $this->userManager->expects($this->once())
  64. ->method('canonicalizeUsername')
  65. ->with($this->equalTo('JaCk'))
  66. ->will($this->returnValue('jack'));
  67. $this->userManager->findUserByUsernameOrEmail('JaCk');
  68. }
  69. public function testFindUserByUsernameOrEmailWithEmail()
  70. {
  71. $this->userManager->expects($this->once())
  72. ->method('findUserBy')
  73. ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
  74. $this->userManager->expects($this->once())
  75. ->method('canonicalizeEmail')
  76. ->with($this->equalTo('JaCk@EmAiL.oRg'))
  77. ->will($this->returnValue('jack@email.org'));
  78. $this->userManager->findUserByUsernameOrEmail('JaCk@EmAiL.oRg');
  79. }
  80. public function testLoadUserByUsernameWithExistingUser()
  81. {
  82. $userMock = $this->getMock('FOS\UserBundle\Propel\User', array(), array('sha1'));
  83. $manager = $this->getMockBuilder('FOS\UserBundle\Propel\UserManager')
  84. ->disableOriginalConstructor()
  85. ->setMethods(array('findUserByUsername'))
  86. ->getMock();
  87. $manager->expects($this->once())
  88. ->method('findUserByUsername')
  89. ->with($this->equalTo('jack'))
  90. ->will($this->returnValue($userMock));
  91. $manager->loadUserByUsername('jack');
  92. }
  93. /**
  94. * @expectedException Symfony\Component\Security\Core\Exception\UsernameNotFoundException
  95. */
  96. public function testLoadUserByUsernameWithMissingUser()
  97. {
  98. $manager = $this->getMockBuilder('FOS\UserBundle\Propel\UserManager')
  99. ->disableOriginalConstructor()
  100. ->setMethods(array('findUserByUsername'))
  101. ->getMock();
  102. $manager->expects($this->once())
  103. ->method('findUserByUsername')
  104. ->with($this->equalTo('jack'))
  105. ->will($this->returnValue(null));
  106. $manager->loadUserByUsername('jack');
  107. }
  108. protected function setUp()
  109. {
  110. if (!class_exists('Propel')) {
  111. $this->markTestSkipped('Propel not installed');
  112. }
  113. $this->userManager = $this->getManagerMock();
  114. }
  115. protected function tearDown()
  116. {
  117. unset($this->userManager);
  118. }
  119. protected function getManagerMock()
  120. {
  121. return $this->getMockBuilder('FOS\UserBundle\Propel\UserManager')
  122. ->disableOriginalConstructor()
  123. ->setMethods(array('findUserBy', 'canonicalizeUsername', 'canonicalizeEmail'))
  124. ->getMock();
  125. }
  126. }