|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare( strict_types = 1 ); |
|
4
|
|
|
|
|
5
|
|
|
namespace WMDE\Fundraising\Frontend\Tests\EdgeToEdge\Routes; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
8
|
|
|
use WMDE\Fundraising\Frontend\Tests\EdgeToEdge\WebRouteTestCase; |
|
9
|
|
|
|
|
10
|
|
|
class ValidateAmountRouteTest extends WebRouteTestCase { |
|
11
|
|
|
|
|
12
|
|
|
private const PATH = '/validate-donation-amount'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @dataProvider getPassingTestData |
|
16
|
|
|
*/ |
|
17
|
|
|
public function testGivenValidParameters_successResponseIsReturned( array $parameters ): void { |
|
18
|
|
|
$client = $this->createClient(); |
|
19
|
|
|
$client->request( Request::METHOD_POST, self::PATH, $parameters ); |
|
20
|
|
|
$this->assertJsonSuccessResponse( [ 'status' => 'OK' ], $client->getResponse() ); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function getPassingTestData(): array { |
|
24
|
|
|
return [ |
|
25
|
|
|
[ [ 'amount' => '1234' ] ], |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @dataProvider getFailingTestData |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testGivenInvalidParameters_matchingFailureResponseIsReturned( array $parameters, array $violations ): void { |
|
33
|
|
|
$client = $this->createClient(); |
|
34
|
|
|
$client->request( Request::METHOD_POST, self::PATH, $parameters ); |
|
35
|
|
|
$this->assertErrorJsonResponse( $client->getResponse() ); |
|
|
|
|
|
|
36
|
|
|
$this->assertEquals( $violations, $this->getJsonFromResponse( $client->getResponse() )['messages'] ); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getFailingTestData(): array { |
|
40
|
|
|
return [ |
|
41
|
|
|
[ [ 'amount' => '' ], [ 'amount' => [ 'This value should be of type digit.', 'This value should be a valid number.' ] ] ], |
|
42
|
|
|
[ [ 'amount' => 'fff' ], [ 'amount' => [ 'This value should be of type digit.', 'This value should be a valid number.' ] ] ], |
|
43
|
|
|
[ [ 'amount' => '12.34' ], [ 'amount' => [ 'This value should be of type digit.', 'This value should be 100 or more.' ] ] ], |
|
44
|
|
|
[ [ 'amount' => '1233.99' ], [ 'amount' => [ 'This value should be of type digit.' ] ] ], |
|
45
|
|
|
[ [ 'amount' => '12,34' ], [ 'amount' => [ 'This value should be of type digit.', 'This value should be a valid number.' ] ] ], |
|
46
|
|
|
[ [ 'amount' => '12' ], [ 'amount' => [ 'This value should be 100 or more.' ] ] ], |
|
47
|
|
|
[ [ 'amount' => '12879342897234879234' ], [ 'amount' => [ 'This value should be 10000000 or less.' ] ] ], |
|
48
|
|
|
[ [ 'amount' => '1234', 'something' => 'more' ], [ 'something' => [ 'This field was not expected.' ] ] ], |
|
49
|
|
|
[ [ 'no context' => 'indeed' ], [ 'amount' => [ 'This field is missing.' ], 'no context' => [ 'This field was not expected.' ] ] ] |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: