Completed
Push — master ( 3724e0...87fc1b )
by Jordi Sala
01:29
created

VarnishCacheTest::testValidToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 14
nc 1
nop 0
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\VarnishCache;
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 VarnishCacheTest 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 VarnishCache(
40
            'token',
41
            [],
42
            $this->router,
43
            'ban',
44
            $this->controllerResolver,
45
            $this->argumentResolver
46
        );
47
    }
48
49
    public function testInitCache(): void
50
    {
51
        $this->router->expects($this->any())
52
            ->method('generate')
53
            ->will($this->returnValue(
54
                'https://sonata-project.org/cache/esi/TOKEN?controller=asdsad'
55
            ));
56
57
        $this->assertTrue($this->cache->flush([]));
58
        $this->assertTrue($this->cache->flushAll());
59
60
        $cacheElement = $this->cache->set(['id' => 7], 'data');
61
62
        $this->assertInstanceOf(CacheElement::class, $cacheElement);
63
64
        $this->assertTrue($this->cache->has(['id' => 7]));
65
66
        $cacheElement = $this->cache->get([
67
            'id' => 7,
68
            'controller' => 'foo.service::runAction',
69
            'parameters' => [],
70
        ]);
71
72
        $this->assertInstanceOf(CacheElement::class, $cacheElement);
73
74
        $this->assertEquals(
75
            '<esi:include src="https://sonata-project.org/cache/esi/TOKEN?controller=asdsad"/>',
76
            $cacheElement->getData()->getContent()
77
        );
78
    }
79
80
    public function testActionInvalidToken(): void
81
    {
82
        $this->expectException(AccessDeniedHttpException::class);
83
84
        $this->router->expects($this->any())
85
            ->method('generate')
86
            ->will($this->returnValue(
87
                'http://sonata-project.orf/cache/esi/TOKEN?controller=asdsad'
88
            ));
89
90
        $request = Request::create('cache/esi/TOKEN?controller=asdsad', 'get', [
91
            'token' => 'wrong',
92
        ]);
93
94
        $this->cache->cacheAction($request);
95
    }
96
97
    public function testValidToken(): void
98
    {
99
        $this->controllerResolver->expects($this->any())
100
            ->method('getController')
101
            ->will($this->returnValue(function () {
102
                return new Response();
103
            }));
104
105
        $this->argumentResolver->expects($this->any())
106
            ->method('getArguments')
107
            ->will($this->returnValue([]));
108
109
        $request = Request::create('cache/esi/TOKEN', 'get', [
110
            'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664',
111
            'parameters' => [
112
                'controller' => 'asfsat',
113
                'parameters' => [],
114
            ],
115
        ]);
116
117
        $this->cache->cacheAction($request);
118
    }
119
120
    public function testRunCommand(): void
121
    {
122
        $tmpFile = tempnam(sys_get_temp_dir(), 'sonata-cache');
123
124
        $cache = new VarnishCache(
125
            'token',
126
            [
127
                sprintf("echo \"varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile),
128
                sprintf("echo \"varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile),
129
            ],
130
            $this->router,
131
            'ban',
132
            $this->controllerResolver,
133
            $this->argumentResolver
134
        );
135
136
        $method = new \ReflectionMethod($cache, 'runCommand');
137
        $method->setAccessible(true);
138
139
        $this->assertTrue($method->invoke($cache, 'ban', 'req.url ~ \'.*\''));
140
141
        $this->assertSame(<<<'CMD'
142
varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret ban 'req.url ~ '.*''
143
varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret ban 'req.url ~ '.*''
144
145
CMD
146
        , file_get_contents($tmpFile));
147
148
        unlink($tmpFile);
149
    }
150
}
151