Completed
Push — master ( 2858aa...73eec7 )
by Jeroen De
65:15
created

SkinTest::assertSkinResponseCookie()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 3
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\EdgeToEdge;
6
7
use Symfony\Component\BrowserKit\Cookie as RequestCookie;
8
use Symfony\Component\HttpFoundation\Cookie as ResponseCookie;
9
use Symfony\Component\HttpFoundation\Response;
10
use WMDE\Fundraising\Frontend\Presentation\SkinSettings;
11
12
class SkinTest extends WebRouteTestCase {
13
14
	private const SKIN_1 = '10h16';
15
	private const SKIN_2 = 'cat17';
16
	private const DEFAULT_SKIN = self::SKIN_1;
17
18
	public function testDefaultSkinGetsUsed(): void {
19
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
20
		$client->request( 'GET', '/' );
21
22
		$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() );
23
		$this->assertNoSkinResponseCookie( $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertNoSkinResponseCookie() 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...
24
	}
25
26
	public function testDefaultSkinNotSavedInCookie(): void {
27
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
28
		$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::DEFAULT_SKIN ] );
29
30
		$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() );
31
		$this->assertNoSkinResponseCookie( $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertNoSkinResponseCookie() 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...
32
	}
33
34
	public function testDefaultSkinUsedAndCookieRemovedWhenRequestedViaQueryWithOpposingCookie(): void {
35
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
36
		$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, self::SKIN_2 ) );
37
		$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::DEFAULT_SKIN ] );
38
39
		$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() );
40
		$this->assertSkinResponseCookie( '', false, $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertSkinResponseCookie() 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...
41
	}
42
43
	public function testSkinChoosableViaCookie(): void {
44
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
45
		$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, self::SKIN_2 ) );
46
		$client->request( 'GET', '/' );
47
48
		$this->assertContains( self::SKIN_2, $client->getResponse()->getContent() );
49
		$this->assertNoSkinResponseCookie( $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertNoSkinResponseCookie() 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...
50
	}
51
52
	public function testSkinChoosableViaQuery(): void {
53
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
54
		$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::SKIN_2 ] );
55
56
		$this->assertContains( self::SKIN_2, $client->getResponse()->getContent() );
57
		$this->assertSkinResponseCookie( self::SKIN_2, true, $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertSkinResponseCookie() 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...
58
	}
59
60
	public function testSkinViaQuerySupersedesCookie(): void {
61
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
62
		$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, self::SKIN_1 ) );
63
		$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => self::SKIN_2 ] );
64
65
		$this->assertContains( self::SKIN_2, $client->getResponse()->getContent() );
66
		$this->assertSkinResponseCookie( self::SKIN_2, true, $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertSkinResponseCookie() 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...
67
	}
68
69
	public function testInvalidQueryIgnored(): void {
70
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
71
		$client->request( 'GET', '/', [ SkinSettings::QUERY_PARAM_NAME => 'fff' ] );
72
73
		$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() );
74
		$this->assertNoSkinResponseCookie( $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertNoSkinResponseCookie() 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...
75
	}
76
77
	public function testInvalidCookieIgnored(): void {
78
		$client = $this->createClient( $this->getDummyConfig(), null, self::DISABLE_DEBUG );
79
		$client->getCookieJar()->set( new RequestCookie( SkinSettings::COOKIE_NAME, 'ggg' ) );
80
		$client->request( 'GET', '/' );
81
82
		$this->assertContains( self::DEFAULT_SKIN, $client->getResponse()->getContent() );
83
		$this->assertNoSkinResponseCookie( $client->getResponse() );
0 ignored issues
show
Bug introduced by
It seems like $client->getResponse() can be null; however, assertNoSkinResponseCookie() 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...
84
	}
85
86
	/**
87
	 * While this can return skin config as it pleases, the skin files are not faked but needed to generate response content
88
	 */
89
	private function getDummyConfig(): array {
90
		return [
91
			'skin' => [
92
				'options' => [ self::SKIN_1, self::SKIN_2 ],
93
				'default' => self::DEFAULT_SKIN,
94
				'cookie-lifetime' => 5
95
			]
96
		];
97
	}
98
99
	private function assertSkinResponseCookie( string $expectedValue, bool $positiveExpiresTime, Response $response ): void {
100
		$cookies = $response->headers->getCookies();
101
		foreach ( $cookies as $cookie ) {
102
			/**
103
			 * @var ResponseCookie $cookie
104
			 */
105
			if ( $cookie->getName() === SkinSettings::COOKIE_NAME ) {
106
				$this->assertSame( $expectedValue, $cookie->getValue() );
107
				if ( $positiveExpiresTime ) {
108
					$this->assertGreaterThan( time(), $cookie->getExpiresTime() );
109
				} else {
110
					$this->assertLessThan( time(), $cookie->getExpiresTime() );
111
				}
112
				return;
113
			}
114
		}
115
		$this->fail( 'Could not find the "skin" response cookie.' );
116
	}
117
118
	private function assertNoSkinResponseCookie( Response $response ): void {
119
		$cookies = $response->headers->getCookies();
120
		foreach ( $cookies as $cookie ) {
121
			/**
122
			 * @var ResponseCookie $cookie
123
			 */
124
			if ( $cookie->getName() === SkinSettings::COOKIE_NAME ) {
125
				$this->fail( 'Found an unexpected "skin" response cookie.' );
126
			}
127
		}
128
	}
129
}
130