Completed
Pull Request — 2.x (#179)
by Grégoire
01:18
created

VarnishCacheTest::testActionInvalidToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\CacheBundle\Tests\Adapter\Cache;
13
14
use PHPUnit\Framework\TestCase;
15
use Sonata\CacheBundle\Adapter\VarnishCache;
16
use Sonata\Cache\CacheElement;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
20
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
21
use Symfony\Component\Routing\RouterInterface;
22
23
class VarnishCacheTest extends TestCase
24
{
25
    private $router;
26
    private $controllerResolver;
27
    private $argumentResolver;
28
    private $cache;
29
30
    protected function setUp()
31
    {
32
        $this->router = $this->createMock(RouterInterface::class);
33
        $this->controllerResolver = $this->createMock(ControllerResolverInterface::class);
34
        if (interface_exists(ArgumentResolverInterface::class)) {
35
            $this->argumentResolver = $this->createMock(ArgumentResolverInterface::class);
36
        }
37
        $this->cache = new VarnishCache(
38
            'token',
39
            [],
40
            $this->router,
41
            'ban',
42
            $this->controllerResolver,
43
            interface_exists(ArgumentResolverInterface::class) ?
44
            $this->argumentResolver :
45
            null
46
        );
47
    }
48
    public function testInitCache()
49
    {
50
        $this->router->expects($this->any())
51
            ->method('generate')
52
            ->will($this->returnValue(
53
                'https://sonata-project.org/cache/esi/TOKEN?controller=asdsad'
54
            ));
55
56
        $this->assertTrue($this->cache->flush([]));
57
        $this->assertTrue($this->cache->flushAll());
58
59
        $cacheElement = $this->cache->set(['id' => 7], 'data');
60
61
        $this->assertInstanceOf(CacheElement::class, $cacheElement);
62
63
        $this->assertTrue($this->cache->has(['id' => 7]));
64
65
        $cacheElement = $this->cache->get([
66
            'id' => 7,
67
            'controller' => 'foo.service::runAction',
68
            'parameters' => []
69
        ]);
70
71
        $this->assertInstanceOf(CacheElement::class, $cacheElement);
72
73
        $this->assertEquals(
74
            '<esi:include src="https://sonata-project.org/cache/esi/TOKEN?controller=asdsad"/>',
75
            $cacheElement->getData()->getContent()
76
        );
77
    }
78
79
    /**
80
     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
81
     */
82
    public function testActionInvalidToken()
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()
98
    {
99
        $this->controllerResolver->expects($this->any())
100
            ->method('getController')
101
            ->will($this->returnValue(function () {
102
                return new Response();
103
            }));
104
        $resolver = interface_exists(ArgumentResolverInterface::class) ?
105
            $this->argumentResolver :
106
            $this->controllerResolver;
107
108
        $resolver->expects($this->any())
109
            ->method('getArguments')
110
            ->will($this->returnValue([]));
111
112
        $request = Request::create('cache/esi/TOKEN', 'get', [
113
            'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664',
114
            'parameters' => [
115
                'controller' => 'asfsat',
116
                'parameters' => [],
117
            ],
118
        ]);
119
120
        $this->cache->cacheAction($request);
121
    }
122
123
    public function testRunCommand()
124
    {
125
        $tmpFile = tempnam(sys_get_temp_dir(), 'sonata-cache');
126
127
        $cache = new VarnishCache(
128
            'token',
129
            [
130
                sprintf("echo \"varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile),
131
                sprintf("echo \"varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile),
132
            ],
133
            $this->router,
134
            'ban',
135
            $this->controllerResolver,
136
            $this->argumentResolver
137
        );
138
139
        $method = new \ReflectionMethod($cache, 'runCommand');
140
        $method->setAccessible(true);
141
142
        $this->assertTrue($method->invoke($cache, 'ban', 'req.url ~ \'.*\''));
143
144
        $this->assertSame(<<<'CMD'
145
varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret ban 'req.url ~ '.*''
146
varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret ban 'req.url ~ '.*''
147
148
CMD
149
        , file_get_contents($tmpFile));
150
151
        unlink($tmpFile);
152
    }
153
154
    /**
155
     * @group legacy
156
     * @expectedDeprecation Not providing a "Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface" instance to "Sonata\CacheBundle\Adapter\VarnishCache::__construct" is deprecated since 3.x and will not be possible in 4.0
157
     */
158
    public function testConstructorLegacy()
159
    {
160
        new VarnishCache('token', [], $this->router, 'ban');
161
    }
162
}
163