Completed
Push — master ( 3a5d30...efd5c6 )
by Jeroen De
16s
created

testGivenRequestWithUnknownDonationId_resultIsError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 19
rs 9.4285
cc 1
eloc 12
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\EdgeToEdge\Routes;
6
7
use Symfony\Component\HttpKernel\Client;
8
use WMDE\Fundraising\Frontend\DonationContext\Domain\Model\Donation;
9
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
10
use WMDE\Fundraising\Frontend\DonationContext\Tests\Data\ValidDonation;
11
use WMDE\Fundraising\Frontend\Tests\EdgeToEdge\WebRouteTestCase;
12
use WMDE\Fundraising\Frontend\Tests\Fixtures\FixedTokenGenerator;
13
14
/**
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class AddCommentPostRouteTest extends WebRouteTestCase {
19
20
	const CORRECT_UPDATE_TOKEN = 'b5b249c8beefb986faf8d186a3f16e86ef509ab2';
21
	const NON_EXISTING_DONATION_ID = 25502;
22
23
	public function testGivenRequestWithoutParameters_resultIsError(): void {
24
		$client = $this->createClient();
25
26
		$client->request(
27
			'POST',
28
			'add-comment',
29
			[]
30
		);
31
32
		$response = $client->getResponse();
33
34
		$this->assertTrue( $response->isSuccessful(), 'request is successful' );
35
		$this->assertErrorJsonResponse( $response );
0 ignored issues
show
Bug introduced by
It seems like $response defined by $client->getResponse() on line 32 can be null; however, WMDE\Fundraising\Fronten...sertErrorJsonResponse() 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
	}
37
38
	public function testGivenRequestWithoutTokens_resultIsError(): void {
39
		$this->createEnvironment( [], function( Client $client, FunFunFactory $factory ): void {
40
			$donation = $this->getNewlyStoredDonation( $factory );
41
42
			$client->request(
43
				'POST',
44
				'add-comment',
45
				[
46
					'comment' => 'Your programmers deserve a raise',
47
					'public' => '1',
48
					'displayName' => 'Uncle Bob',
49
					'donationId' => (string)$donation->getId(),
50
				]
51
			);
52
53
			$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...
54
		} );
55
	}
56
57
	private function getNewlyStoredDonation( FunFunFactory $factory ): Donation {
58
		$factory->setDonationTokenGenerator( new FixedTokenGenerator(
59
			self::CORRECT_UPDATE_TOKEN,
60
			new \DateTime( '9001-01-01' )
61
		) );
62
63
		$donation = ValidDonation::newDirectDebitDonation();
64
65
		$factory->getDonationRepository()->storeDonation( $donation );
66
67
		return $donation;
68
	}
69
70
	public function testGivenRequestWithValidParameters_resultIsSuccess(): void {
71
		$this->createEnvironment( [], function( Client $client, FunFunFactory $factory ): void {
72
			$donation = $this->getNewlyStoredDonation( $factory );
73
74
			$client->request(
75
				'POST',
76
				'add-comment',
77
				[
78
					'comment' => 'Your programmers deserve a raise',
79
					'public' => '1',
80
					'displayName' => 'Uncle Bob',
81
					'donationId' => (string)$donation->getId(),
82
					'updateToken' => self::CORRECT_UPDATE_TOKEN,
83
				]
84
			);
85
86
			$this->assertSuccessJsonResponse( $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertSuccessJsonResponse() 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...
87
		} );
88
	}
89
90
	public function testGivenRequestWithUnknownDonationId_resultIsError(): void {
91
		$this->createEnvironment( [], function( Client $client, FunFunFactory $factory ): void {
92
			$this->getNewlyStoredDonation( $factory );
93
94
			$client->request(
95
				'POST',
96
				'add-comment',
97
				[
98
					'comment' => 'Your programmers deserve a raise',
99
					'public' => '1',
100
					'displayName' => 'Uncle Bob',
101
					'donationId' => self::NON_EXISTING_DONATION_ID,
102
					'updateToken' => self::CORRECT_UPDATE_TOKEN,
103
				]
104
			);
105
106
			$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...
107
		} );
108
	}
109
110
	public function testGivenRequestWithInvalidUpdateToken_resultIsError(): void {
111
		$this->createEnvironment( [], function( Client $client, FunFunFactory $factory ): void {
112
			$donation = $this->getNewlyStoredDonation( $factory );
113
114
			$client->request(
115
				'POST',
116
				'add-comment',
117
				[
118
					'comment' => 'Your programmers deserve a raise',
119
					'public' => '1',
120
					'displayName' => 'Uncle Bob',
121
					'donationId' => (string)$donation->getId(),
122
					'updateToken' => 'Not the correct token',
123
				]
124
			);
125
126
			$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...
127
		} );
128
	}
129
130
}
131