1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Tests\Command; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Command\CreateUserHandler; |
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\BaseUserDataTransferObject; |
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\InMemoryBaseUserRepository; |
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository; |
10
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection; |
11
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser; |
12
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactory; |
13
|
|
|
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder; |
14
|
|
|
|
15
|
|
|
class CreateUserHandlerTest extends PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var UserRepository */ |
18
|
|
|
private $userRepository; |
19
|
|
|
|
20
|
|
|
/** @var BaseUserRepositoryCollection */ |
21
|
|
|
private $userRepositoryCollection; |
22
|
|
|
|
23
|
|
View Code Duplication |
public function setUp(): void |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->userRepository = new InMemoryBaseUserRepository( |
26
|
|
|
new EncoderFactory([BaseUser::class => new PlaintextPasswordEncoder()]) |
27
|
|
|
); |
28
|
|
|
$this->userRepositoryCollection = new BaseUserRepositoryCollection([$this->userRepository]); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Test if CreateUserHandler gets handled. |
33
|
|
|
*/ |
34
|
|
|
public function testCreateUserGetsHandled(): void |
35
|
|
|
{ |
36
|
|
|
$handler = new CreateUserHandler( |
37
|
|
|
new EncoderFactory([BaseUser::class => new PlaintextPasswordEncoder()]), |
38
|
|
|
$this->userRepositoryCollection |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$userDataTransferObject = new BaseUserDataTransferObject(); |
42
|
|
|
$userDataTransferObject->userName = 'sumo'; |
43
|
|
|
$userDataTransferObject->plainPassword = 'randomPassword'; |
44
|
|
|
$userDataTransferObject->displayName = 'sumocoders'; |
45
|
|
|
$userDataTransferObject->email = '[email protected]'; |
46
|
|
|
|
47
|
|
|
$handler->handle($userDataTransferObject); |
48
|
|
|
|
49
|
|
|
$this->assertEquals( |
50
|
|
|
'sumo', |
51
|
|
|
$this->userRepository->findByUsername('sumo')->getUsername() |
52
|
|
|
); |
53
|
|
|
$this->assertEquals( |
54
|
|
|
'sumocoders', |
55
|
|
|
$this->userRepository->findByUsername('sumo')->getDisplayName() |
56
|
|
|
); |
57
|
|
|
$this->assertEquals( |
58
|
|
|
'randomPassword{' . $this->userRepository->findByUsername('sumo')->getSalt() . '}', |
59
|
|
|
$this->userRepository->findByUsername('sumo')->getPassword() |
60
|
|
|
); |
61
|
|
|
$this->assertEquals( |
62
|
|
|
'[email protected]', |
63
|
|
|
$this->userRepository->findByUsername('sumo')->getEmail() |
64
|
|
|
); |
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.