Completed
Push — master ( acfdda...fdd7ce )
by
unknown
61:19 queued 36:44
created

BypassCacheListenerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 9
dl 0
loc 96
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testOnKernelRequest() 0 19 1
A getApiClient() 0 6 1
A assertIsHit() 0 7 1
A dataProvider() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MovingImage\Bundle\VMProApiBundle\Tests\EventListener;
6
7
use GuzzleHttp\ClientInterface;
8
use JMS\Serializer\SerializerBuilder;
9
use JMS\Serializer\SerializerInterface;
10
use MovingImage\Bundle\VMProApiBundle\EventListener\BypassCacheListener;
11
use MovingImage\Client\VMPro\ApiClient;
12
use PHPUnit\Framework\TestCase;
13
use Psr\Cache\CacheItemPoolInterface;
14
use Symfony\Component\Cache\Adapter\ArrayAdapter;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17
use Symfony\Component\HttpKernel\HttpKernel;
18
use Symfony\Component\HttpKernel\HttpKernelInterface;
19
20
class BypassCacheListenerTest extends TestCase
21
{
22
    /**
23
     * @var SerializerInterface
24
     */
25
    private $serializer;
26
27
    public function setUp()
28
    {
29
        $this->serializer = SerializerBuilder::create()->build();
30
    }
31
32
    /**
33
     * Tests onKernelRequest method.
34
     *
35
     * @dataProvider dataProvider
36
     *
37
     * @param string $bypassCacheArgument
38
     * @param array  $query
39
     * @param array  $request
40
     * @param array  $attributes
41
     * @param array  $cookies
42
     * @param bool   $isHit
43
     */
44
    public function testOnKernelRequest(
45
        $bypassCacheArgument,
46
        array $query,
47
        array $request,
48
        array $attributes,
49
        array $cookies,
50
        $isHit
51
    ) {
52
        $apiClient = $this->getApiClient();
53
        $listener = new BypassCacheListener($apiClient, $bypassCacheArgument);
54
        $request = new Request($query, $request, $attributes, $cookies);
55
        $kernel = $this->createMock(HttpKernel::class);
56
57
        $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\HttpKe...\Event\GetResponseEvent has been deprecated with message: since Symfony 4.3, use RequestEvent instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
58
59
        $listener->onKernelRequest($event);
60
61
        $this->assertIsHit($isHit, $apiClient->getCacheItemPool());
62
    }
63
64
    /**
65
     * Creates an ApiClient instance with mocked dependencies
66
     * and ArrayAdapter as cache pool.
67
     *
68
     * @return ApiClient
69
     */
70
    private function getApiClient()
71
    {
72
        $client = $this->createMock(ClientInterface::class);
73
74
        return new ApiClient($client, $this->serializer, new ArrayAdapter());
0 ignored issues
show
Compatibility introduced by
$this->serializer of type object<JMS\Serializer\SerializerInterface> is not a sub-type of object<JMS\Serializer\Serializer>. It seems like you assume a concrete implementation of the interface JMS\Serializer\SerializerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75
    }
76
77
    /**
78
     * Asserts that storing an item to the provided pool followed by immediately
79
     * fetching it again from the pool will result in the specified "hit" status.
80
     * ($isHit = true -> expecting a cache hit; $isHit = false -> expecting a cache miss).
81
     *
82
     * @param $isHit
83
     * @param CacheItemPoolInterface $pool
84
     */
85
    private function assertIsHit($isHit, CacheItemPoolInterface $pool)
86
    {
87
        $item = $pool->getItem('test');
88
        $item->set('test');
89
        $pool->save($item);
90
        $this->assertSame($isHit, $pool->getItem('test')->isHit());
91
    }
92
93
    /**
94
     * Data provider for testOnKernelRequest.
95
     * Provides various combinations of request arguments and configuration,
96
     * as well as the expected behavior.
97
     *
98
     * @return array
99
     */
100
    public function dataProvider()
101
    {
102
        return [
103
            [null, [], [], [], [], true],
104
            [null, ['bypass_cache' => 1], [], [], [], true],
105
            [null, [], ['bypass_cache' => 1], [], [], true],
106
            [null, [], [], ['bypass_cache' => 1], [], true],
107
            [null, [], [], [], ['bypass_cache' => 1], true],
108
109
            ['bypass_cache', ['bypass_cache' => 1], [], [], [], false],
110
            ['bypass_cache', [], ['bypass_cache' => 1], [], [], false],
111
            ['bypass_cache', [], [], ['bypass_cache' => 1], [], false],
112
            ['bypass_cache', [], [], [], ['bypass_cache' => 1], false],
113
        ];
114
    }
115
}
116