-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathEventDispatchingHttpCacheTest.php
More file actions
88 lines (79 loc) · 3.33 KB
/
EventDispatchingHttpCacheTest.php
File metadata and controls
88 lines (79 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\HttpCache\Tests\Functional\Symfony;
use FOS\HttpCache\SymfonyCache\CacheInvalidation;
use FOS\HttpCache\SymfonyCache\CleanupCacheTagsListener;
use FOS\HttpCache\SymfonyCache\CustomTtlListener;
use FOS\HttpCache\SymfonyCache\DebugListener;
use FOS\HttpCache\SymfonyCache\EventDispatchingHttpCache;
use FOS\HttpCache\SymfonyCache\PurgeListener;
use FOS\HttpCache\SymfonyCache\RefreshListener;
use FOS\HttpCache\SymfonyCache\UserContextListener;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;
#[PHPUnit\Group('symfony')]
class EventDispatchingHttpCacheTest extends TestCase
{
use MockeryPHPUnitIntegration;
public function testEventListeners(): void
{
$request = new Request();
$expectedResponse = new Response();
$expectedResponse->headers->set('X-Reverse-Proxy-TTL', 60);
$expectedResponse->headers->set('X-Cache-Tags', 'foo, bar');
$httpKernel = \Mockery::mock(HttpKernelInterface::class)
->shouldReceive('handle')
->andReturn($expectedResponse)
->getMock();
$store = \Mockery::mock(StoreInterface::class)
->shouldReceive('lookup')->andReturn(null)->times(1)
// https://github.com/phpstan/phpstan-mockery/issues/8
/* @phpstan-ignore-next-line */
->shouldReceive('write')->times(1)
->shouldReceive('unlock')->times(1)
// need to declare the cleanup function explicitly to avoid issue between register_shutdown_function and mockery
->shouldReceive('cleanup')->atMost(1)
->getMock();
$kernel = new AppCache($httpKernel, $store);
$kernel->addSubscriber(new CustomTtlListener());
$kernel->addSubscriber(new DebugListener());
$kernel->addSubscriber(new PurgeListener());
$kernel->addSubscriber(new RefreshListener());
$kernel->addSubscriber(new UserContextListener([
// avoid having to provide mocking for the hash lookup
// we already test anonymous hash lookup in the UserContextListener unit test
'anonymous_hash' => 'abcdef',
]));
$kernel->addSubscriber(new CleanupCacheTagsListener());
$response = $kernel->handle($request);
$this->assertSame($expectedResponse, $response);
$this->assertFalse($response->headers->has('X-Reverse-Proxy-TTL'));
$this->assertFalse($response->headers->has('X-Cache-Tags'));
}
}
class AppCache extends HttpCache implements CacheInvalidation
{
use EventDispatchingHttpCache;
/**
* Made public to allow event listeners to do refresh operations.
*
* {@inheritdoc}
*/
public function fetch(Request $request, $catch = false): Response
{
return parent::fetch($request, $catch);
}
}