Last active
August 29, 2015 14:01
-
-
Save elnur/b5a73e6a2ee9cd412f78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| services: | |
| password_encoder: | |
| class: Symfony\Component\Security\Core\Tests\Encoder\PasswordEncoder | |
| factory_service: security.encoder_factory | |
| factory_method: getEncoder | |
| arguments: | |
| - Example\Model\User |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Example\Manager; | |
| use JMS\DiExtraBundle\Annotation\Inject; | |
| use JMS\DiExtraBundle\Annotation\InjectParams; | |
| use JMS\DiExtraBundle\Annotation\Service; | |
| use Example\Model\User; | |
| use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; | |
| /** | |
| * @Service("user_manager") | |
| */ | |
| class UserManager | |
| { | |
| /** | |
| * @var PasswordEncoderInterface | |
| */ | |
| private $passwordEncoder; | |
| /** | |
| * @InjectParams({ | |
| * "passwordEncoder" = @Inject("password_encoder") | |
| * }) | |
| * | |
| * @param PasswordEncoderInterface $passwordEncoder | |
| */ | |
| public function __construct(PasswordEncoderInterface $passwordEncoder) | |
| { | |
| $this->passwordEncoder = $passwordEncoder; | |
| } | |
| /** | |
| * @param User $user | |
| */ | |
| public function save(User $user) | |
| { | |
| $password = $this->passwordEncoder->encodePassword($user->getPlainPassword(), $user->getSalt()); | |
| $user->setPassword($password); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Example\Manager; | |
| use PHPUnit_Framework_TestCase; | |
| use Example\Model\User; | |
| use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; | |
| class UserManagerTest extends PHPUnit_Framework_TestCase | |
| { | |
| const PLAIN_PASSWORD = 'password'; | |
| const ENCODED_PASSWORD = 'encoded-password'; | |
| /** | |
| * @test | |
| */ | |
| public function shouldEncodePasswordIfPlainPasswordExists() | |
| { | |
| $passwordEncoder = $this->getMock(PasswordEncoderInterface::class); | |
| $userManager = new UserManager($passwordEncoder); | |
| $user = new User([ | |
| 'plainPassword' => self::PLAIN_PASSWORD, | |
| ]); | |
| $passwordEncoder | |
| ->expects($this->once()) | |
| ->method('encodePassword') | |
| ->with(self::PLAIN_PASSWORD, $user->getSalt()) | |
| ->willReturn(self::ENCODED_PASSWORD); | |
| $userManager->save($user); | |
| $this->assertEquals(self::ENCODED_PASSWORD, $user->getPassword()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment