vendor/bundles/FOS/UserBundle/Tests/Validation/PasswordValidatorTest.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\Validation;
       
    13 
       
    14 use FOS\UserBundle\Validator\PasswordValidator;
       
    15 use FOS\UserBundle\Validator\Password;
       
    16 use FOS\UserBundle\Form\Model\ChangePassword;
       
    17 
       
    18 class PasswordValidatorTest extends \PHPUnit_Framework_TestCase
       
    19 {
       
    20     private $validator;
       
    21     private $constraint;
       
    22     private $encoderFactory;
       
    23     private $changePasswordObject;
       
    24     private $encoder;
       
    25 
       
    26     public function setUp()
       
    27     {
       
    28         $this->constraint = new Password();
       
    29         $this->constraint->passwordProperty = 'current';
       
    30         $this->constraint->userProperty = 'user';
       
    31 
       
    32         $this->changePasswordObject = new ChangePassword($this->getMock('FOS\UserBundle\Model\UserInterface'));
       
    33         $this->encoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface');
       
    34         $this->encoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface');
       
    35 
       
    36         $this->validator = new PasswordValidator();
       
    37         $this->validator->setEncoderFactory($this->encoderFactory);
       
    38     }
       
    39 
       
    40     /**
       
    41      * @expectedException \RuntimeException
       
    42      */
       
    43     public function testExceptionForNonObject()
       
    44     {
       
    45         $this->validator->isValid('propertyValue', $this->constraint);
       
    46     }
       
    47 
       
    48     public function testFalseOnInvalidPassword()
       
    49     {
       
    50         $this->encoderFactory->expects($this->once())
       
    51                 ->method('getEncoder')
       
    52                 ->will($this->returnValue($this->encoder));
       
    53 
       
    54         $this->encoder->expects($this->once())
       
    55                 ->method('isPasswordValid')
       
    56                 ->will($this->returnValue(false));
       
    57 
       
    58         $this->assertFalse($this->validator->isValid($this->changePasswordObject, $this->constraint));
       
    59     }
       
    60 
       
    61     public function testTrueOnValidPassword()
       
    62     {
       
    63         $this->encoderFactory->expects($this->once())
       
    64                 ->method('getEncoder')
       
    65                 ->will($this->returnValue($this->encoder));
       
    66 
       
    67         $this->encoder->expects($this->once())
       
    68                 ->method('isPasswordValid')
       
    69                 ->will($this->returnValue(true));
       
    70 
       
    71         $this->assertTrue($this->validator->isValid($this->changePasswordObject, $this->constraint));
       
    72     }
       
    73 }