PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Tests/Model/UserManagerTest.php

https://gitlab.com/freebird/WebApp
PHP | 180 lines | 142 code | 30 blank | 8 comment | 0 complexity | 86dfea373281248718bf4292d0a40641 MD5 | raw file
  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\Tests\Model;
  11. class UserManagerTest extends \PHPUnit_Framework_TestCase
  12. {
  13. private $manager;
  14. private $encoderFactory;
  15. private $usernameCanonicalizer;
  16. private $emailCanonicalizer;
  17. protected function setUp()
  18. {
  19. $this->encoderFactory = $this->getMockEncoderFactory();
  20. $this->usernameCanonicalizer = $this->getMockCanonicalizer();
  21. $this->emailCanonicalizer = $this->getMockCanonicalizer();
  22. $this->manager = $this->getUserManager(array(
  23. $this->encoderFactory,
  24. $this->usernameCanonicalizer,
  25. $this->emailCanonicalizer,
  26. ));
  27. }
  28. public function testUpdateCanonicalFields()
  29. {
  30. $user = $this->getUser();
  31. $user->setUsername('Username');
  32. $user->setEmail('User@Example.com');
  33. $this->usernameCanonicalizer->expects($this->once())
  34. ->method('canonicalize')
  35. ->with('Username')
  36. ->will($this->returnCallback('strtolower'));
  37. $this->emailCanonicalizer->expects($this->once())
  38. ->method('canonicalize')
  39. ->with('User@Example.com')
  40. ->will($this->returnCallback('strtolower'));
  41. $this->manager->updateCanonicalFields($user);
  42. $this->assertEquals('username', $user->getUsernameCanonical());
  43. $this->assertEquals('user@example.com', $user->getEmailCanonical());
  44. }
  45. public function testUpdatePassword()
  46. {
  47. $encoder = $this->getMockPasswordEncoder();
  48. $user = $this->getUser();
  49. $user->setPlainPassword('password');
  50. $this->encoderFactory->expects($this->once())
  51. ->method('getEncoder')
  52. ->will($this->returnValue($encoder));
  53. $encoder->expects($this->once())
  54. ->method('encodePassword')
  55. ->with('password', $user->getSalt())
  56. ->will($this->returnValue('encodedPassword'));
  57. $this->manager->updatePassword($user);
  58. $this->assertEquals('encodedPassword', $user->getPassword(), '->updatePassword() sets encoded password');
  59. $this->assertNull($user->getPlainPassword(), '->updatePassword() erases credentials');
  60. }
  61. public function testFindUserByUsername()
  62. {
  63. $this->manager->expects($this->once())
  64. ->method('findUserBy')
  65. ->with($this->equalTo(array('usernameCanonical' => 'jack')));
  66. $this->usernameCanonicalizer->expects($this->once())
  67. ->method('canonicalize')
  68. ->with('jack')
  69. ->will($this->returnValue('jack'));
  70. $this->manager->findUserByUsername('jack');
  71. }
  72. public function testFindUserByUsernameLowercasesTheUsername()
  73. {
  74. $this->manager->expects($this->once())
  75. ->method('findUserBy')
  76. ->with($this->equalTo(array('usernameCanonical' => 'jack')));
  77. $this->usernameCanonicalizer->expects($this->once())
  78. ->method('canonicalize')
  79. ->with('JaCk')
  80. ->will($this->returnValue('jack'));
  81. $this->manager->findUserByUsername('JaCk');
  82. }
  83. public function testFindUserByEmail()
  84. {
  85. $this->manager->expects($this->once())
  86. ->method('findUserBy')
  87. ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
  88. $this->emailCanonicalizer->expects($this->once())
  89. ->method('canonicalize')
  90. ->with('jack@email.org')
  91. ->will($this->returnValue('jack@email.org'));
  92. $this->manager->findUserByEmail('jack@email.org');
  93. }
  94. public function testFindUserByEmailLowercasesTheEmail()
  95. {
  96. $this->manager->expects($this->once())
  97. ->method('findUserBy')
  98. ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
  99. $this->emailCanonicalizer->expects($this->once())
  100. ->method('canonicalize')
  101. ->with('JaCk@EmAiL.oRg')
  102. ->will($this->returnValue('jack@email.org'));
  103. $this->manager->findUserByEmail('JaCk@EmAiL.oRg');
  104. }
  105. public function testFindUserByUsernameOrEmailWithUsername()
  106. {
  107. $this->manager->expects($this->once())
  108. ->method('findUserBy')
  109. ->with($this->equalTo(array('usernameCanonical' => 'jack')));
  110. $this->usernameCanonicalizer->expects($this->once())
  111. ->method('canonicalize')
  112. ->with('JaCk')
  113. ->will($this->returnValue('jack'));
  114. $this->manager->findUserByUsernameOrEmail('JaCk');
  115. }
  116. public function testFindUserByUsernameOrEmailWithEmail()
  117. {
  118. $this->manager->expects($this->once())
  119. ->method('findUserBy')
  120. ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
  121. $this->emailCanonicalizer->expects($this->once())
  122. ->method('canonicalize')
  123. ->with('JaCk@EmAiL.oRg')
  124. ->will($this->returnValue('jack@email.org'));
  125. $this->manager->findUserByUsernameOrEmail('JaCk@EmAiL.oRg');
  126. }
  127. private function getMockCanonicalizer()
  128. {
  129. return $this->getMock('FOS\UserBundle\Util\CanonicalizerInterface');
  130. }
  131. private function getMockEncoderFactory()
  132. {
  133. return $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
  134. }
  135. private function getMockPasswordEncoder()
  136. {
  137. return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
  138. }
  139. private function getUser()
  140. {
  141. return $this->getMockBuilder('FOS\UserBundle\Model\User')
  142. ->getMockForAbstractClass();
  143. }
  144. private function getUserManager(array $args)
  145. {
  146. return $this->getMockBuilder('FOS\UserBundle\Model\UserManager')
  147. ->setConstructorArgs($args)
  148. ->getMockForAbstractClass();
  149. }
  150. }