1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\CacheBundle\Tests\Adapter\Cache; |
15
|
|
|
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Sonata\Cache\CacheElement; |
18
|
|
|
use Sonata\CacheBundle\Adapter\SsiCache; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; |
22
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
23
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
24
|
|
|
use Symfony\Component\Routing\RouterInterface; |
25
|
|
|
|
26
|
|
|
class SsiCacheTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
private $router; |
29
|
|
|
private $controllerResolver; |
30
|
|
|
private $argumentResolver; |
31
|
|
|
private $cache; |
32
|
|
|
|
33
|
|
|
protected function setUp(): void |
34
|
|
|
{ |
35
|
|
|
$this->router = $this->createMock(RouterInterface::class); |
36
|
|
|
$this->controllerResolver = $this->createMock(ControllerResolverInterface::class); |
37
|
|
|
$this->argumentResolver = $this->createMock(ArgumentResolverInterface::class); |
38
|
|
|
|
39
|
|
|
$this->cache = new SsiCache( |
40
|
|
|
'token', |
41
|
|
|
$this->router, |
42
|
|
|
$this->controllerResolver, |
43
|
|
|
$this->argumentResolver |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testInitCache(): void |
48
|
|
|
{ |
49
|
|
|
$this->router->expects($this->any()) |
50
|
|
|
->method('generate') |
51
|
|
|
->will($this->returnValue('/cache/esi/TOKEN?controller=asdsad')); |
52
|
|
|
|
53
|
|
|
$this->assertTrue($this->cache->flush([])); |
54
|
|
|
$this->assertTrue($this->cache->flushAll()); |
55
|
|
|
|
56
|
|
|
$cacheElement = $this->cache->set(['id' => 7], 'data'); |
57
|
|
|
|
58
|
|
|
$this->assertInstanceOf(CacheElement::class, $cacheElement); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($this->cache->has(['id' => 7])); |
61
|
|
|
|
62
|
|
|
$cacheElement = $this->cache->get([ |
63
|
|
|
'id' => 7, |
64
|
|
|
'controller' => 'foo.service::runAction', |
65
|
|
|
'parameters' => [], |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf(CacheElement::class, $cacheElement); |
69
|
|
|
|
70
|
|
|
$this->assertEquals( |
71
|
|
|
'<!--# include virtual="/cache/esi/TOKEN?controller=asdsad" -->', |
72
|
|
|
$cacheElement->getData()->getContent() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function testActionInvalidToken(): void |
77
|
|
|
{ |
78
|
|
|
$this->expectException(AccessDeniedHttpException::class); |
79
|
|
|
|
80
|
|
|
$this->router->expects($this->any()) |
81
|
|
|
->method('generate') |
82
|
|
|
->will($this->returnValue( |
83
|
|
|
'http://sonata-project.orf/cache/esi/TOKEN?controller=asdsad' |
84
|
|
|
)); |
85
|
|
|
|
86
|
|
|
$request = Request::create('cache/esi/TOKEN?controller=asdsad', 'get', [ |
87
|
|
|
'token' => 'wrong', |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$this->cache->cacheAction($request); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testValidToken(): void |
94
|
|
|
{ |
95
|
|
|
$this->controllerResolver->expects($this->any()) |
96
|
|
|
->method('getController') |
97
|
|
|
->will($this->returnValue(function () { |
98
|
|
|
return new Response(); |
99
|
|
|
})); |
100
|
|
|
|
101
|
|
|
$this->argumentResolver->expects($this->any()) |
102
|
|
|
->method('getArguments') |
103
|
|
|
->will($this->returnValue([])); |
104
|
|
|
|
105
|
|
|
$request = Request::create('cache/esi/TOKEN', 'get', [ |
106
|
|
|
'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664', |
107
|
|
|
'parameters' => [ |
108
|
|
|
'controller' => 'asfsat', |
109
|
|
|
'parameters' => [], |
110
|
|
|
], |
111
|
|
|
]); |
112
|
|
|
|
113
|
|
|
$this->cache->cacheAction($request); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|