|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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 Symfony\Component\Security\Core\Tests\Encoder; |
| 13 | + |
| 14 | +use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
| 15 | + |
| 16 | +class UserPasswordEncoderTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + public function testEncodePassword() |
| 19 | + { |
| 20 | + $userMock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
| 21 | + $userMock->expects($this->any()) |
| 22 | + ->method('getSalt') |
| 23 | + ->will($this->returnValue('userSalt')); |
| 24 | + |
| 25 | + $mockEncoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface'); |
| 26 | + $mockEncoder->expects($this->any()) |
| 27 | + ->method('encodePassword') |
| 28 | + ->with($this->equalTo('plainPassword'), $this->equalTo('userSalt')) |
| 29 | + ->will($this->returnValue('encodedPassword')); |
| 30 | + |
| 31 | + $mockEncoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); |
| 32 | + $mockEncoderFactory->expects($this->any()) |
| 33 | + ->method('getEncoder') |
| 34 | + ->with($this->equalTo($userMock)) |
| 35 | + ->will($this->returnValue($mockEncoder)); |
| 36 | + |
| 37 | + $passwordEncoder = new UserPasswordEncoder($mockEncoderFactory); |
| 38 | + |
| 39 | + $encoded = $passwordEncoder->encodePassword($userMock, 'plainPassword'); |
| 40 | + $this->assertEquals('encodedPassword', $encoded); |
| 41 | + } |
| 42 | + |
| 43 | + public function testIsPasswordValid() |
| 44 | + { |
| 45 | + $userMock = $this->getMock('Symfony\Component\Security\Core\User\UserInterface'); |
| 46 | + $userMock->expects($this->any()) |
| 47 | + ->method('getSalt') |
| 48 | + ->will($this->returnValue('userSalt')); |
| 49 | + $userMock->expects($this->any()) |
| 50 | + ->method('getPassword') |
| 51 | + ->will($this->returnValue('encodedPassword')); |
| 52 | + |
| 53 | + $mockEncoder = $this->getMock('Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface'); |
| 54 | + $mockEncoder->expects($this->any()) |
| 55 | + ->method('isPasswordValid') |
| 56 | + ->with($this->equalTo('encodedPassword'), $this->equalTo('plainPassword'), $this->equalTo('userSalt')) |
| 57 | + ->will($this->returnValue(true)); |
| 58 | + |
| 59 | + $mockEncoderFactory = $this->getMock('Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface'); |
| 60 | + $mockEncoderFactory->expects($this->any()) |
| 61 | + ->method('getEncoder') |
| 62 | + ->with($this->equalTo($userMock)) |
| 63 | + ->will($this->returnValue($mockEncoder)); |
| 64 | + |
| 65 | + $passwordEncoder = new UserPasswordEncoder($mockEncoderFactory); |
| 66 | + |
| 67 | + $isValid = $passwordEncoder->isPasswordValid($userMock, 'plainPassword'); |
| 68 | + $this->assertTrue($isValid); |
| 69 | + } |
| 70 | +} |
0 commit comments