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