Test Setup Failed
Pull Request — master (#39)
by
unknown
62:08
created

CookieControllerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

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

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 Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
10
use WMDE\BannerServer\Controller\AbstractCookieController;
11
use PHPUnit\Framework\TestCase;
12
13
class MockCookieController extends AbstractCookieController {
14
15
}
16
17
class CookieControllerTest extends TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
18
19
	public function test_given_no_category_then_no_cookie_is_set(): void {
20
		$controller = new MockCookieController( 'P1D' );
21
22
		$response = $controller->index( new Request() );
23
24
		$this->assertEmpty( $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT ) );
25
		$this->assertNotSame( Response::HTTP_OK, $response->getStatusCode() );
26
	}
27
28
	public function test_given_invalid_category_then_no_cookie_is_set(): void {
29
		$controller = new MockCookieController( 'P1D' );
30
31
		$response = $controller->index( new Request( ['c' => ':break-my-yaml!'] ) );
32
33
		$this->assertEmpty( $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT ) );
34
		$this->assertNotSame( Response::HTTP_OK, $response->getStatusCode() );
35
	}
36
37
	public function test_given_a_category_then_category_cookie_is_set(): void {
38
		$controller = new MockCookieController( 'P180D' );
39
40
		$response = $controller->index( new Request( ['c' => 'fundraising,fundraising_next'] ) );
41
42
		$this->assertSame( Response::HTTP_OK, $response->getStatusCode() );
43
		$firstCookie = $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT )[0];
44
		$expectedDate =( new \DateTime() )->add( new \DateInterval( 'P180D' ) );
45
46
		$this->assertSame( 'fundraising,fundraising_next', $firstCookie->getValue() );
47
		$this->assertEqualsWithDelta( $expectedDate->getTimestamp(), $firstCookie->getExpiresTime(), 5 );
48
		$this->assertSame( 'text/html', $response->headers->get( 'content-type', '' ) );
49
	}
50
51
	public function test_given_image_content_type_zero_byte_image_is_returned(): void {
52
		$controller = new MockCookieController( 'P180D' );
53
54
		$request = new Request( ['c' => 'fundraising,fundraising_next'] );
55
		$request->headers->set( 'accept', 'image/gif,image/png,image/*' );
56
		$response = $controller->index( $request );
57
58
		$this->assertSame( Response::HTTP_OK, $response->getStatusCode() );
59
		$this->assertNotEmpty( $response->headers->getCookies( ResponseHeaderBag::COOKIES_FLAT ) );
60
		$this->assertSame( 'image/png', $response->headers->get( 'content-type', '' ) );
61
		$this->assertEquals( '', $response->getContent() );
62
	}
63
64
}
65