Completed
Pull Request — master (#1084)
by wiese
65:33
created

ValidateAmountRouteTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 43
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testGivenValidParameters_successResponseIsReturned() 0 5 1
A getPassingTestData() 0 5 1
A testGivenInvalidParameters_matchingFailureResponseIsReturned() 0 6 1
A getFailingTestData() 0 13 1
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() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertJsonSuccessResponse() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertErrorJsonResponse() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
36
		$this->assertEquals( $violations, $this->getJsonFromResponse( $client->getResponse() )['messages'] );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, getJsonFromResponse() does not accept null, maybe add an additional type check?

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:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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