CookieControllerTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 26
dl 0
loc 46
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_given_no_category_then_no_cookie_is_set() 0 7 1
A test_given_a_category_then_category_cookie_is_set() 0 12 1
A test_given_image_content_type_zero_byte_image_is_returned() 0 11 1
A test_given_invalid_category_then_no_cookie_is_set() 0 7 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Tests\Unit\Controller;
6
7
use DateInterval;
8
use DateTime;
9
use PHPUnit\Framework\Attributes\CoversClass;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\Attributes\CoversClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
14
use WMDE\BannerServer\Controller\AbstractCookieController;
15
use WMDE\BannerServer\Tests\Utils\TestCookieController;
16
17
#[CoversClass( AbstractCookieController::class )]
18
class CookieControllerTest extends TestCase {
19
20
	public function test_given_no_category_then_no_cookie_is_set(): void {
21
		$controller = new TestCookieController( 'P1D' );
22
23
		$response = $controller->index( new Request() );
24
25
		$this->assertCount( 0, $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT ) );
26
		$this->assertNotSame( Response::HTTP_OK, $response->getStatusCode() );
27
	}
28
29
	public function test_given_invalid_category_then_no_cookie_is_set(): void {
30
		$controller = new TestCookieController( 'P1D' );
31
32
		$response = $controller->index( new Request( [ 'c' => ':break-my-yaml!' ] ) );
33
34
		$this->assertCount( 0, $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT ) );
35
		$this->assertNotSame( Response::HTTP_OK, $response->getStatusCode() );
36
	}
37
38
	public function test_given_a_category_then_category_cookie_is_set(): void {
39
		$controller = new TestCookieController( 'P180D' );
40
41
		$response = $controller->index( new Request( [ 'c' => 'fundraising,fundraising_next' ] ) );
42
43
		$this->assertSame( Response::HTTP_OK, $response->getStatusCode() );
44
		$firstCookie = $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT )[0];
45
		$expectedDate = ( new DateTime() )->add( new DateInterval( 'P180D' ) );
46
47
		$this->assertSame( 'fundraising,fundraising_next', $firstCookie->getValue() );
48
		$this->assertEqualsWithDelta( $expectedDate->getTimestamp(), $firstCookie->getExpiresTime(), 5 );
49
		$this->assertSame( 'text/html', $response->headers->get( 'content-type', '' ) );
50
	}
51
52
	public function test_given_image_content_type_zero_byte_image_is_returned(): void {
53
		$controller = new TestCookieController( 'P180D' );
54
55
		$request = new Request( [ 'c' => 'fundraising,fundraising_next' ] );
56
		$request->headers->set( 'accept', 'image/gif,image/png,image/*' );
57
		$response = $controller->index( $request );
58
59
		$this->assertSame( Response::HTTP_OK, $response->getStatusCode() );
60
		$this->assertNotEmpty( $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT ) );
61
		$this->assertSame( 'image/png', $response->headers->get( 'content-type', '' ) );
62
		$this->assertSame( '', $response->getContent() );
63
	}
64
65
}
66