Completed
Push — master ( 9ab4b9...065cdd )
by Alejandro
09:44
created

QrCodeCacheMiddlewareTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 3
lcom 1
cbo 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A noCachedPathFallbacksToNextMiddleware() 0 13 1
A cachedPathReturnsCacheContent() 0 20 1
1
<?php
2
namespace ShlinkioTest\Shlink\Core\Middleware;
3
4
use Doctrine\Common\Cache\ArrayCache;
5
use Doctrine\Common\Cache\Cache;
6
use PHPUnit_Framework_TestCase as TestCase;
7
use Shlinkio\Shlink\Core\Middleware\QrCodeCacheMiddleware;
8
use Zend\Diactoros\Response;
9
use Zend\Diactoros\ServerRequestFactory;
10
use Zend\Diactoros\Uri;
11
12
class QrCodeCacheMiddlewareTest extends TestCase
13
{
14
    /**
15
     * @var QrCodeCacheMiddleware
16
     */
17
    protected $middleware;
18
    /**
19
     * @var Cache
20
     */
21
    protected $cache;
22
23
    public function setUp()
24
    {
25
        $this->cache = new ArrayCache();
26
        $this->middleware = new QrCodeCacheMiddleware($this->cache);
27
    }
28
29
    /**
30
     * @test
31
     */
32
    public function noCachedPathFallbacksToNextMiddleware()
33
    {
34
        $isCalled = false;
35
        $this->middleware->__invoke(
36
            ServerRequestFactory::fromGlobals(),
37
            new Response(),
38
            function ($req, $resp) use (&$isCalled) {
39
                $isCalled = true;
40
                return $resp;
41
            }
42
        );
43
        $this->assertTrue($isCalled);
44
    }
45
46
    /**
47
     * @test
48
     */
49
    public function cachedPathReturnsCacheContent()
50
    {
51
        $isCalled = false;
52
        $uri = (new Uri())->withPath('/foo');
53
        $this->cache->save('/foo', ['body' => 'the body', 'content-type' => 'image/png']);
54
55
        $resp = $this->middleware->__invoke(
56
            ServerRequestFactory::fromGlobals()->withUri($uri),
57
            new Response(),
58
            function ($req, $resp) use (&$isCalled) {
59
                $isCalled = true;
60
                return $resp;
61
            }
62
        );
63
64
        $this->assertFalse($isCalled);
65
        $resp->getBody()->rewind();
66
        $this->assertEquals('the body', $resp->getBody()->getContents());
67
        $this->assertEquals('image/png', $resp->getHeaderLine('Content-Type'));
68
    }
69
}
70