-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathUserCreateProcessorTest.php
More file actions
129 lines (109 loc) · 4.16 KB
/
UserCreateProcessorTest.php
File metadata and controls
129 lines (109 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace App\Tests\State;
use ApiPlatform\Metadata\Post;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\User;
use App\Security\ReCaptcha\ReCaptchaWrapper;
use App\Service\MailService;
use App\State\UserCreateProcessor;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use ReCaptcha\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasher;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
/**
* @internal
*/
class UserCreateProcessorTest extends TestCase {
private UserCreateProcessor $processor;
private MockObject|Response $recaptchaResponse;
private MockObject|UserPasswordHasherInterface $userPasswordHasher;
private MailService|MockObject $mailService;
private User $user;
/**
* @throws \ReflectionException
*/
protected function setUp(): void {
$this->user = new User();
$this->recaptchaResponse = $this->createMock(Response::class);
$recaptcha = $this->createMock(ReCaptchaWrapper::class);
$recaptcha->method('verify')->willReturn($this->recaptchaResponse);
$this->userPasswordHasher = $this->createMock(UserPasswordHasher::class);
$this->mailService = $this->createMock(MailService::class);
$decoratedProcessor = $this->createStub(ProcessorInterface::class);
$this->processor = new UserCreateProcessor(
$decoratedProcessor,
$recaptcha,
$this->userPasswordHasher,
$this->mailService
);
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateRequiresReCaptcha() {
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(false)
;
$this->expectException(\Exception::class);
$this->processor->onBefore($this->user, new Post());
}
#[AllowMockObjectsWithoutExpectations]
public function testDoesNotHashWhenNoPasswordIsSet() {
// given
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$this->userPasswordHasher->expects($this->never())->method('hashPassword');
// when
/** @var User $data */
$data = $this->processor->onBefore($this->user, new Post());
// then
$this->assertNull($data->password);
$this->assertNull($data->plainPassword);
}
#[AllowMockObjectsWithoutExpectations]
public function testHashesPasswordWhenPlainPasswordIsSet() {
// given
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$this->user->plainPassword = 'test plain password';
$this->userPasswordHasher->expects($this->once())->method('hashPassword')->willReturn('test hash');
// when
/** @var User $data */
$data = $this->processor->onBefore($this->user, new Post());
// then
$this->assertEquals('test hash', $data->password);
$this->assertNull($data->plainPassword);
}
#[AllowMockObjectsWithoutExpectations]
public function testCreateAndSendActivationKey() {
// given
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
$this->mailService->expects($this->once())->method('sendUserActivationMail');
// when
/** @var User $data */
$data = $this->processor->onBefore($this->user, new Post());
$this->processor->onAfter($this->user, new Post());
// then
$this->assertNotNull($data->activationKeyHash);
}
#[AllowMockObjectsWithoutExpectations]
public function testSetsStateToRegisteredBeforeCreate() {
// when
$this->recaptchaResponse->expects(self::once())
->method('isSuccess')
->willReturn(true)
;
/** @var User $data */
$data = $this->processor->onBefore($this->user, new Post());
// then
self::assertThat($data->state, self::equalTo(User::STATE_REGISTERED));
}
}