|
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); |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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
|
|
|
|
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: