1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Command\ResetPasswordHandler; |
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\ChangePasswordDataTransferObject; |
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidPasswordConfirmationException; |
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\InMemoryBaseUserRepository; |
10
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository; |
11
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection; |
12
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser; |
13
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactory; |
14
|
|
|
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder; |
15
|
|
|
|
16
|
|
|
class PasswordResetHandlerTest extends PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** @var UserRepository */ |
19
|
|
|
private $userRepository; |
20
|
|
|
|
21
|
|
|
/** @var BaseUserRepositoryCollection */ |
22
|
|
|
private $userRepositoryCollection; |
23
|
|
|
|
24
|
|
View Code Duplication |
public function setUp(): void |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$this->userRepository = new InMemoryBaseUserRepository( |
27
|
|
|
new EncoderFactory([BaseUser::class => new PlaintextPasswordEncoder()]) |
28
|
|
|
); |
29
|
|
|
$this->userRepositoryCollection = new BaseUserRepositoryCollection([$this->userRepository]); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Test if PAsswordResetHandler gets handled. |
34
|
|
|
* |
35
|
|
|
* @throws InvalidPasswordConfirmationException |
36
|
|
|
*/ |
37
|
|
|
public function testPasswordResetGetsHandled(): void |
38
|
|
|
{ |
39
|
|
|
$handler = new ResetPasswordHandler( |
40
|
|
|
$this->userRepositoryCollection, |
41
|
|
|
new EncoderFactory([BaseUser::class => new PlaintextPasswordEncoder()]) |
42
|
|
|
); |
43
|
|
|
|
44
|
|
|
$user = $this->userRepository->findByUsername('reset'); |
45
|
|
|
|
46
|
|
|
$changePasswordTransferObject = ChangePasswordDataTransferObject::forUser($user); |
47
|
|
|
$changePasswordTransferObject->newPassword = 'changedPassword'; |
48
|
|
|
|
49
|
|
|
$user = $this->userRepositoryCollection |
50
|
|
|
->findRepositoryByClassName(BaseUser::class) |
51
|
|
|
->findByUsername('reset'); |
52
|
|
|
$password = $user->getPassword(); |
53
|
|
|
$token = $user->getPasswordResetToken(); |
54
|
|
|
|
55
|
|
|
$handler->handle($changePasswordTransferObject); |
56
|
|
|
|
57
|
|
|
$updatedUser = $this->userRepositoryCollection |
58
|
|
|
->findRepositoryByClassName(BaseUser::class) |
59
|
|
|
->findByUsername('reset'); |
60
|
|
|
|
61
|
|
|
$this->assertEquals($user->getUsername(), $updatedUser->getUsername()); |
62
|
|
|
$this->assertNotEquals($password, $updatedUser->getPassword()); |
63
|
|
|
$this->assertNotNull($token); |
64
|
|
|
$this->assertNull($updatedUser->getPasswordResetToken()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.