|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\Integration\Validation; |
|
6
|
|
|
|
|
7
|
|
|
use WMDE\Fundraising\Frontend\UseCases\GetInTouch\GetInTouchRequest; |
|
8
|
|
|
use WMDE\Fundraising\Frontend\Infrastructure\NullDomainNameValidator; |
|
9
|
|
|
use WMDE\Fundraising\Frontend\Validation\GetInTouchValidator; |
|
10
|
|
|
use WMDE\FunValidators\ConstraintViolation; |
|
11
|
|
|
use WMDE\FunValidators\ValidationResult; |
|
12
|
|
|
use WMDE\FunValidators\Validators\EmailValidator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @licence GNU GPL v2+ |
|
16
|
|
|
* @author Kai Nissen < [email protected] > |
|
17
|
|
|
*/ |
|
18
|
|
|
class GetInTouchValidatorTest extends \PHPUnit\Framework\TestCase { |
|
19
|
|
|
|
|
20
|
|
|
public function testNameFieldsAreOptional(): void { |
|
21
|
|
|
$mailValidator = new EmailValidator( new NullDomainNameValidator() ); |
|
22
|
|
|
$validator = new GetInTouchValidator( $mailValidator ); |
|
23
|
|
|
$request = new GetInTouchRequest( |
|
24
|
|
|
'', |
|
25
|
|
|
'', |
|
26
|
|
|
'[email protected]', |
|
27
|
|
|
'Hello there!', |
|
28
|
|
|
'I just wanted to say "Hi".' |
|
29
|
|
|
); |
|
30
|
|
|
$this->assertTrue( $validator->validate( $request )->isSuccessful() ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function testEmailAddressIsValidated(): void { |
|
34
|
|
|
$mailValidator = new EmailValidator( new NullDomainNameValidator() ); |
|
35
|
|
|
$validator = new GetInTouchValidator( $mailValidator ); |
|
36
|
|
|
$request = new GetInTouchRequest( |
|
37
|
|
|
'', |
|
38
|
|
|
'', |
|
39
|
|
|
'kh@meyer', |
|
40
|
|
|
'Hello there!', |
|
41
|
|
|
'I just wanted to say "Hi".' |
|
42
|
|
|
); |
|
43
|
|
|
$this->assertFalse( $validator->validate( $request )->isSuccessful() ); |
|
44
|
|
|
$this->assertConstraintWasViolated( $validator->validate( $request ), 'email' ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function assertConstraintWasViolated( ValidationResult $result, string $fieldName ): void { |
|
48
|
|
|
$this->assertContainsOnlyInstancesOf( ConstraintViolation::class, $result->getViolations() ); |
|
49
|
|
|
$this->assertTrue( $result->hasViolations() ); |
|
50
|
|
|
|
|
51
|
|
|
$violated = false; |
|
52
|
|
|
foreach ( $result->getViolations() as $violation ) { |
|
53
|
|
|
if ( $violation->getSource() === $fieldName ) { |
|
54
|
|
|
$violated = true; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->assertTrue( |
|
59
|
|
|
$violated, |
|
60
|
|
|
'Failed asserting that constraint for field "' . $fieldName . '"" was violated.' |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testSubjectIsValidated(): void { |
|
65
|
|
|
$mailValidator = new EmailValidator( new NullDomainNameValidator() ); |
|
66
|
|
|
$validator = new GetInTouchValidator( $mailValidator ); |
|
67
|
|
|
$request = new GetInTouchRequest( |
|
68
|
|
|
'', |
|
69
|
|
|
'', |
|
70
|
|
|
'[email protected]', |
|
71
|
|
|
'', |
|
72
|
|
|
'I just wanted to say "Hi".' |
|
73
|
|
|
); |
|
74
|
|
|
$this->assertFalse( $validator->validate( $request )->isSuccessful() ); |
|
75
|
|
|
$this->assertConstraintWasViolated( $validator->validate( $request ), 'subject' ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testMessageBodyIsValidated(): void { |
|
79
|
|
|
$mailValidator = new EmailValidator( new NullDomainNameValidator() ); |
|
80
|
|
|
$validator = new GetInTouchValidator( $mailValidator ); |
|
81
|
|
|
$request = new GetInTouchRequest( |
|
82
|
|
|
'', |
|
83
|
|
|
'', |
|
84
|
|
|
'[email protected]', |
|
85
|
|
|
'Hello there!', |
|
86
|
|
|
'' |
|
87
|
|
|
); |
|
88
|
|
|
$this->assertFalse( $validator->validate( $request )->isSuccessful() ); |
|
89
|
|
|
$this->assertConstraintWasViolated( $validator->validate( $request ), 'messageBody' ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|