Completed
Pull Request — master (#10)
by
unknown
24:49
created

BypassCacheListenerTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 8
dl 0
loc 87
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testOnKernelRequest() 0 19 1
A getApiClient() 0 7 1
A assertIsHit() 0 7 1
A dataProvider() 0 15 1
1
<?php
2
3
namespace MovingImage\Bundle\VMProApiBundle\Tests\EventListener;
4
5
use GuzzleHttp\ClientInterface;
6
use JMS\Serializer\Serializer;
7
use MovingImage\Bundle\VMProApiBundle\EventListener\BypassCacheListener;
8
use MovingImage\Client\VMPro\ApiClient;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Cache\CacheItemPoolInterface;
11
use Symfony\Component\Cache\Adapter\ArrayAdapter;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\HttpKernel\HttpKernel;
15
use Symfony\Component\HttpKernel\HttpKernelInterface;
16
17
class BypassCacheListenerTest extends TestCase
18
{
19
    /**
20
     * Tests onKernelRequest method.
21
     *
22
     * @dataProvider dataProvider
23
     *
24
     * @param string $bypassCacheArgument
25
     * @param array  $query
26
     * @param array  $request
27
     * @param array  $attributes
28
     * @param array  $cookies
29
     * @param bool   $isHit
30
     */
31
    public function testOnKernelRequest(
32
        $bypassCacheArgument,
33
        array $query,
34
        array $request,
35
        array $attributes,
36
        array $cookies,
37
        $isHit
38
    ) {
39
        $apiClient = $this->getApiClient();
40
        $listener = new BypassCacheListener($apiClient, $bypassCacheArgument);
41
        $request = new Request($query, $request, $attributes, $cookies);
42
        $kernel = $this->createMock(HttpKernel::class);
43
44
        $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
45
46
        $listener->onKernelRequest($event);
47
48
        $this->assertIsHit($isHit, $apiClient->getCacheItemPool());
0 ignored issues
show
Bug introduced by
The method getCacheItemPool() does not seem to exist on object<MovingImage\Client\VMPro\ApiClient>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
    }
50
51
    /**
52
     * Creates an ApiClient instance with mocked dependencies
53
     * and ArrayAdapter as cache pool.
54
     *
55
     * @return ApiClient
56
     */
57
    private function getApiClient()
58
    {
59
        $client = $this->createMock(ClientInterface::class);
60
        $serializer = $this->createMock(Serializer::class);
61
62
        return new ApiClient($client, $serializer, new ArrayAdapter());
63
    }
64
65
    /**
66
     * Asserts that storing an item to the provided pool followed by immediately
67
     * fetching it again from the pool will result in the specified "hit" status.
68
     * ($isHit = true -> expecting a cache hit; $isHit = false -> expecting a cache miss).
69
     *
70
     * @param $isHit
71
     * @param CacheItemPoolInterface $pool
72
     */
73
    private function assertIsHit($isHit, CacheItemPoolInterface $pool)
74
    {
75
        $item = $pool->getItem('test');
76
        $item->set('test');
77
        $pool->save($item);
78
        $this->assertSame($isHit, $pool->getItem('test')->isHit());
79
    }
80
81
    /**
82
     * Data provider for testOnKernelRequest.
83
     * Provides various combinations of request arguments and configuration,
84
     * as well as the expected behavior.
85
     *
86
     * @return array
87
     */
88
    public function dataProvider()
89
    {
90
        return [
91
            [null, [], [], [], [], true],
92
            [null, ['bypass_cache' => 1], [], [], [], true],
93
            [null, [], ['bypass_cache' => 1], [], [], true],
94
            [null, [], [], ['bypass_cache' => 1], [], true],
95
            [null, [], [], [], ['bypass_cache' => 1], true],
96
97
            ['bypass_cache', ['bypass_cache' => 1], [], [], [], false],
98
            ['bypass_cache', [], ['bypass_cache' => 1], [], [], false],
99
            ['bypass_cache', [], [], ['bypass_cache' => 1], [], false],
100
            ['bypass_cache', [], [], [], ['bypass_cache' => 1], false],
101
        ];
102
    }
103
}
104