CacheWareTest::testPrivateCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
 * CacheWare (https://github.com/juliangut/cacheware)
4
 * PSR7 cache headers management middleware
5
 *
6
 * @license BSD-3-Clause
7
 * @author Julián Gutiérrez <[email protected]>
8
 */
9
10
namespace Jgut\Middleware\Cacheware\Tests;
11
12
use Jgut\Middleware\CacheWare;
13
use Zend\Diactoros\Response;
14
use Zend\Diactoros\ServerRequestFactory;
15
16
/**
17
 * PHP cache headers management middleware test class.
18
 */
19
class CacheWareTest extends \PHPUnit_Framework_TestCase
20
{
21
    /**
22
     * @var \Zend\Diactoros\Request
23
     */
24
    protected $request;
25
26
    /**
27
     * @var Response
28
     */
29
    protected $response;
30
31
    /**
32
     * @var callable
33
     */
34
    protected $callback;
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function setUp()
40
    {
41
        $this->request = ServerRequestFactory::fromGlobals();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Zend\Diactoros\ServerRe...tFactory::fromGlobals() of type object<Zend\Diactoros\ServerRequest> is incompatible with the declared type object<Zend\Diactoros\Request> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
        $this->response = new Response;
43
        $this->callback = function ($request, $response) {
44
            return $response;
45
        };
46
    }
47
48
    public function testPublicCache()
49
    {
50
        $middleware = new CacheWare(['limiter' => CacheWare::CACHE_PUBLIC]);
51
52
        /* @var Response $response */
53
        $response = $middleware($this->request, $this->response, $this->callback);
54
55
        self::assertTrue($response->hasHeader('Expires'));
56
        self::assertTrue($response->hasHeader('Cache-Control'));
57
        self::assertSame(0, strpos($response->getHeaderLine('Cache-Control'), 'public'));
58
        self::assertTrue($response->hasHeader('Last-Modified'));
59
    }
60
61
    public function testPrivateCache()
62
    {
63
        $middleware = new CacheWare(['limiter' => CacheWare::CACHE_PRIVATE]);
64
65
        /* @var Response $response */
66
        $response = $middleware($this->request, $this->response, $this->callback);
67
68
        self::assertTrue($response->hasHeader('Expires'));
69
        self::assertEquals(CacheWare::CACHE_EXPIRED, $response->getHeaderLine('Expires'));
70
        self::assertTrue($response->hasHeader('Cache-Control'));
71
        self::assertSame(0, strpos($response->getHeaderLine('Cache-Control'), 'private'));
72
        self::assertTrue($response->hasHeader('Last-Modified'));
73
    }
74
75
    public function testPrivateNoExpireCache()
76
    {
77
        $middleware = new CacheWare(['limiter' => CacheWare::CACHE_PRIVATE_NO_EXPIRE]);
78
79
        /* @var Response $response */
80
        $response = $middleware($this->request, $this->response, $this->callback);
81
82
        self::assertFalse($response->hasHeader('Expires'));
83
        self::assertTrue($response->hasHeader('Cache-Control'));
84
        self::assertSame(0, strpos($response->getHeaderLine('Cache-Control'), 'private'));
85
        self::assertTrue($response->hasHeader('Last-Modified'));
86
    }
87
88
    public function testNoCacheCache()
89
    {
90
        $middleware = new CacheWare(['limiter' => CacheWare::CACHE_NOCACHE]);
91
92
        /* @var Response $response */
93
        $response = $middleware($this->request, $this->response, $this->callback);
94
95
        self::assertTrue($response->hasHeader('Expires'));
96
        self::assertEquals(CacheWare::CACHE_EXPIRED, $response->getHeaderLine('Expires'));
97
        self::assertTrue($response->hasHeader('Cache-Control'));
98
        self::assertEquals(
99
            'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
100
            $response->getHeaderLine('Cache-Control')
101
        );
102
        self::assertTrue($response->hasHeader('Pragma'));
103
        self::assertEquals('no-cache', $response->getHeaderLine('Pragma'));
104
    }
105
106
    public function testNoCache()
107
    {
108
        $middleware = new CacheWare(['limiter' => null]);
109
110
        /* @var Response $response */
111
        $response = $middleware($this->request, $this->response, $this->callback);
112
113
        self::assertFalse($response->hasHeader('Expires'));
114
        self::assertFalse($response->hasHeader('Cache-Control'));
115
        self::assertFalse($response->hasHeader('Last-Modified'));
116
        self::assertFalse($response->hasHeader('Pragma'));
117
    }
118
}
119