vendor/bundles/FOS/UserBundle/Tests/Model/UserManagerTest.php
changeset 3 e54dfe4d0b2b
equal deleted inserted replaced
2:806e57d67020 3:e54dfe4d0b2b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the FOSUserBundle package.
       
     5  *
       
     6  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace FOS\UserBundle\Tests\Model;
       
    13 
       
    14 class UserManagerTest extends \PHPUnit_Framework_TestCase
       
    15 {
       
    16     private $manager;
       
    17     private $encoderFactory;
       
    18     private $algorithm;
       
    19     private $usernameCanonicalizer;
       
    20     private $emailCanonicalizer;
       
    21 
       
    22     protected function setUp()
       
    23     {
       
    24         $this->encoderFactory        = $this->getMockEncoderFactory();
       
    25         $this->algorithm             = 'sha512';
       
    26         $this->usernameCanonicalizer = $this->getMockCanonicalizer();
       
    27         $this->emailCanonicalizer    = $this->getMockCanonicalizer();
       
    28 
       
    29         $this->manager = $this->getUserManager(array(
       
    30             $this->encoderFactory,
       
    31             $this->algorithm,
       
    32             $this->usernameCanonicalizer,
       
    33             $this->emailCanonicalizer,
       
    34         ));
       
    35     }
       
    36 
       
    37     public function testUpdateCanonicalFields()
       
    38     {
       
    39         $user = $this->getUser();
       
    40         $user->setUsername('Username');
       
    41         $user->setEmail('User@Example.com');
       
    42 
       
    43         $this->usernameCanonicalizer->expects($this->once())
       
    44             ->method('canonicalize')
       
    45             ->with('Username')
       
    46             ->will($this->returnCallback('strtolower'));
       
    47 
       
    48         $this->emailCanonicalizer->expects($this->once())
       
    49             ->method('canonicalize')
       
    50             ->with('User@Example.com')
       
    51             ->will($this->returnCallback('strtolower'));
       
    52 
       
    53         $this->manager->updateCanonicalFields($user);
       
    54         $this->assertEquals('username', $user->getUsernameCanonical());
       
    55         $this->assertEquals('user@example.com', $user->getEmailCanonical());
       
    56     }
       
    57 
       
    58     public function testUpdatePassword()
       
    59     {
       
    60         $encoder = $this->getMockPasswordEncoder();
       
    61         $user = $this->getUser();
       
    62         $user->setPlainPassword('password');
       
    63 
       
    64         $this->encoderFactory->expects($this->once())
       
    65             ->method('getEncoder')
       
    66             ->will($this->returnValue($encoder));
       
    67 
       
    68         $encoder->expects($this->once())
       
    69             ->method('encodePassword')
       
    70             ->with('password', $user->getSalt())
       
    71             ->will($this->returnValue('encodedPassword'));
       
    72 
       
    73         $this->manager->updatePassword($user);
       
    74         $this->assertEquals($this->algorithm, $user->getAlgorithm(), '->updatePassword() sets algorithm');
       
    75         $this->assertEquals('encodedPassword', $user->getPassword(), '->updatePassword() sets encoded password');
       
    76         $this->assertNull($user->getPlainPassword(), '->updatePassword() erases credentials');
       
    77     }
       
    78 
       
    79     private function getMockCanonicalizer()
       
    80     {
       
    81         return $this->getMock('FOS\UserBundle\Util\CanonicalizerInterface');
       
    82     }
       
    83 
       
    84     private function getMockEncoderFactory()
       
    85     {
       
    86         return $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
       
    87     }
       
    88 
       
    89     private function getMockPasswordEncoder()
       
    90     {
       
    91         return $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
       
    92     }
       
    93 
       
    94     private function getUser()
       
    95     {
       
    96         return $this->getMockBuilder('FOS\UserBundle\Model\User')
       
    97             ->getMockForAbstractClass();
       
    98     }
       
    99 
       
   100     private function getUserManager(array $args)
       
   101     {
       
   102         return $this->getMockBuilder('FOS\UserBundle\Model\UserManager')
       
   103             ->setConstructorArgs($args)
       
   104             ->getMockForAbstractClass();
       
   105     }
       
   106 }