123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php
-
- /*
- * This file is part of the FOSUserBundle package.
- *
- * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
- namespace FOS\UserBundle\Tests\Model;
-
- class UserManagerTest extends \PHPUnit_Framework_TestCase
- {
- private $manager;
- private $encoderFactory;
- private $usernameCanonicalizer;
- private $emailCanonicalizer;
-
- protected function setUp()
- {
- $this->encoderFactory = $this->getMockEncoderFactory();
- $this->usernameCanonicalizer = $this->getMockCanonicalizer();
- $this->emailCanonicalizer = $this->getMockCanonicalizer();
-
- $this->manager = $this->getUserManager(array(
- $this->encoderFactory,
- $this->usernameCanonicalizer,
- $this->emailCanonicalizer,
- ));
- }
-
- public function testUpdateCanonicalFields()
- {
- $user = $this->getUser();
- $user->setUsername('Username');
- $user->setEmail('User@Example.com');
-
- $this->usernameCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('Username')
- ->will($this->returnCallback('strtolower'));
-
- $this->emailCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('User@Example.com')
- ->will($this->returnCallback('strtolower'));
-
- $this->manager->updateCanonicalFields($user);
- $this->assertEquals('username', $user->getUsernameCanonical());
- $this->assertEquals('user@example.com', $user->getEmailCanonical());
- }
-
- public function testUpdatePassword()
- {
- $encoder = $this->getMockPasswordEncoder();
- $user = $this->getUser();
- $user->setPlainPassword('password');
-
- $this->encoderFactory->expects($this->once())
- ->method('getEncoder')
- ->will($this->returnValue($encoder));
-
- $encoder->expects($this->once())
- ->method('encodePassword')
- ->with('password', $user->getSalt())
- ->will($this->returnValue('encodedPassword'));
-
- $this->manager->updatePassword($user);
- $this->assertEquals('encodedPassword', $user->getPassword(), '->updatePassword() sets encoded password');
- $this->assertNull($user->getPlainPassword(), '->updatePassword() erases credentials');
- }
-
- public function testFindUserByUsername()
- {
- $this->manager->expects($this->once())
- ->method('findUserBy')
- ->with($this->equalTo(array('usernameCanonical' => 'jack')));
- $this->usernameCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('jack')
- ->will($this->returnValue('jack'));
-
- $this->manager->findUserByUsername('jack');
- }
-
- public function testFindUserByUsernameLowercasesTheUsername()
- {
- $this->manager->expects($this->once())
- ->method('findUserBy')
- ->with($this->equalTo(array('usernameCanonical' => 'jack')));
- $this->usernameCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('JaCk')
- ->will($this->returnValue('jack'));
-
- $this->manager->findUserByUsername('JaCk');
- }
-
- public function testFindUserByEmail()
- {
- $this->manager->expects($this->once())
- ->method('findUserBy')
- ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
- $this->emailCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('jack@email.org')
- ->will($this->returnValue('jack@email.org'));
-
- $this->manager->findUserByEmail('jack@email.org');
- }
-
- public function testFindUserByEmailLowercasesTheEmail()
- {
- $this->manager->expects($this->once())
- ->method('findUserBy')
- ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
- $this->emailCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('JaCk@EmAiL.oRg')
- ->will($this->returnValue('jack@email.org'));
-
- $this->manager->findUserByEmail('JaCk@EmAiL.oRg');
- }
-
- public function testFindUserByUsernameOrEmailWithUsername()
- {
- $this->manager->expects($this->once())
- ->method('findUserBy')
- ->with($this->equalTo(array('usernameCanonical' => 'jack')));
- $this->usernameCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('JaCk')
- ->will($this->returnValue('jack'));
-
- $this->manager->findUserByUsernameOrEmail('JaCk');
- }
-
- public function testFindUserByUsernameOrEmailWithEmail()
- {
- $this->manager->expects($this->once())
- ->method('findUserBy')
- ->with($this->equalTo(array('emailCanonical' => 'jack@email.org')));
- $this->emailCanonicalizer->expects($this->once())
- ->method('canonicalize')
- ->with('JaCk@EmAiL.oRg')
- ->will($this->returnValue('jack@email.org'));
-
- $this->manager->findUserByUsernameOrEmail('JaCk@EmAiL.oRg');
- }
-
- private function getMockCanonicalizer()
- {
- return $this->getMock('FOS\UserBundle\Util\CanonicalizerInterface');
- }
-
- private function getMockEncoderFactory()
- {
- return $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
- }
-
- private function getMockPasswordEncoder()
- {
- return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
- }
-
- private function getUser()
- {
- return $this->getMockBuilder('FOS\UserBundle\Model\User')
- ->getMockForAbstractClass();
- }
-
- private function getUserManager(array $args)
- {
- return $this->getMockBuilder('FOS\UserBundle\Model\UserManager')
- ->setConstructorArgs($args)
- ->getMockForAbstractClass();
- }
- }
|