BypassCacheListenerTest::dataProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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(): void
28
    {
29
        $this->serializer = SerializerBuilder::create()->build();
30
    }
31
32
    /**
33
     * Tests onKernelRequest method.
34
     *
35
     * @dataProvider dataProvider
36
     */
37
    public function testOnKernelRequest(
38
        ?string $bypassCacheArgument,
39
        array $query,
40
        array $request,
41
        array $attributes,
42
        array $cookies,
43
        bool $isHit
44
    ): void {
45
        $apiClient = $this->getApiClient();
46
        $listener = new BypassCacheListener($apiClient, $bypassCacheArgument);
47
        $request = new Request($query, $request, $attributes, $cookies);
48
        $kernel = $this->createMock(HttpKernel::class);
49
50
        $event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
0 ignored issues
show
Documentation introduced by
$kernel is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...el\HttpKernelInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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...
51
52
        $listener->onKernelRequest($event);
53
54
        $this->assertIsHit($isHit, $apiClient->getCacheItemPool());
55
    }
56
57
    /**
58
     * Creates an ApiClient instance with mocked dependencies
59
     * and ArrayAdapter as cache pool.
60
     */
61
    private function getApiClient(): ApiClient
62
    {
63
        $client = $this->createMock(ClientInterface::class);
64
65
        return new ApiClient($client, $this->serializer, new ArrayAdapter());
0 ignored issues
show
Documentation introduced by
$client is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<GuzzleHttp\ClientInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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...
66
    }
67
68
    /**
69
     * Asserts that storing an item to the provided pool followed by immediately
70
     * fetching it again from the pool will result in the specified "hit" status.
71
     * ($isHit = true -> expecting a cache hit; $isHit = false -> expecting a cache miss).
72
     */
73
    private function assertIsHit(bool $isHit, CacheItemPoolInterface $pool): void
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
    public function dataProvider(): array
87
    {
88
        return [
89
            [null, [], [], [], [], true],
90
            [null, ['bypass_cache' => 1], [], [], [], true],
91
            [null, [], ['bypass_cache' => 1], [], [], true],
92
            [null, [], [], ['bypass_cache' => 1], [], true],
93
            [null, [], [], [], ['bypass_cache' => 1], true],
94
95
            ['bypass_cache', ['bypass_cache' => 1], [], [], [], false],
96
            ['bypass_cache', [], ['bypass_cache' => 1], [], [], false],
97
            ['bypass_cache', [], [], ['bypass_cache' => 1], [], false],
98
            ['bypass_cache', [], [], [], ['bypass_cache' => 1], false],
99
        ];
100
    }
101
}
102