Completed
Pull Request — 2.x (#116)
by Rémi
02:07
created

VarnishCacheTest::testRunCommand()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 17
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 Sonata\CacheBundle\Adapter\VarnishCache;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
18
class VarnishCacheTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function testInitCache()
21
    {
22
        $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
23
        $router->expects($this->any())->method('generate')->will($this->returnValue('https://sonata-project.org/cache/esi/TOKEN?controller=asdsad'));
24
25
        $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
26
27
        $cache = new VarnishCache('token', array(), $router, 'ban', $resolver);
28
29
        $this->assertTrue($cache->flush(array()));
30
        $this->assertTrue($cache->flushAll());
31
32
        $cacheElement = $cache->set(array('id' => 7), 'data');
33
34
        $this->assertInstanceOf('Sonata\Cache\CacheElement', $cacheElement);
35
36
        $this->assertTrue($cache->has(array('id' => 7)));
37
38
        $cacheElement = $cache->get(array('id' => 7, 'controller' => 'foo.service::runAction', 'parameters' => array()));
39
40
        $this->assertInstanceOf('Sonata\Cache\CacheElement', $cacheElement);
41
42
        $this->assertEquals('<esi:include src="https://sonata-project.org/cache/esi/TOKEN?controller=asdsad"/>', $cacheElement->getData()->getContent());
43
    }
44
45
    /**
46
     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
47
     */
48
    public function testActionInvalidToken()
49
    {
50
        $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
51
        $router->expects($this->any())->method('generate')->will($this->returnValue('http://sonata-project.orf/cache/esi/TOKEN?controller=asdsad'));
52
53
        $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
54
55
        $request = Request::create('cache/esi/TOKEN?controller=asdsad', 'get', array(
56
            'token' => 'wrong',
57
        ));
58
59
        $cache = new VarnishCache('token', array(), $router, 'ban', $resolver);
60
        $cache->cacheAction($request);
61
    }
62
63
    public function testValidToken()
64
    {
65
        $router = $this->getMock('Symfony\Component\Routing\RouterInterface');
66
67
        $resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
68
        $resolver->expects($this->any())->method('getController')->will($this->returnValue(function () {
69
            return new Response();
70
        }));
71
        $resolver->expects($this->any())->method('getArguments')->will($this->returnValue(array()));
72
73
        $request = Request::create('cache/esi/TOKEN', 'get', array(
74
            'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664',
75
            'parameters' => array(
76
                'controller' => 'asfsat',
77
                'parameters' => array(),
78
            ),
79
        ));
80
81
        $cache = new VarnishCache('token', array(), $router, 'ban', $resolver);
82
        $cache->cacheAction($request);
83
    }
84
85
    public function testRunCommand()
86
    {
87
        $tmpFile = tempnam(sys_get_temp_dir(), 'sonata-cache');
88
89
        $cache = new VarnishCache(
90
            'token',
91
            array(
92
                sprintf("echo \"varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile),
93
                sprintf("echo \"varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret {{ COMMAND }} '{{ EXPRESSION }}'\" >> %s", $tmpFile),
94
            ),
95
            $this->getMock('Symfony\Component\Routing\RouterInterface'),
96
            'ban'
97
        );
98
99
        $method = new \ReflectionMethod($cache, 'runCommand');
100
        $method->setAccessible(true);
101
102
        $this->assertTrue($method->invoke($cache, 'ban', 'req.url ~ \'.*\''));
103
104
        $this->assertSame(<<<'CMD'
105
varnishadm -T 10.4.1.62:6082 -S /etc/varnish/secret ban 'req.url ~ '.*''
106
varnishadm -T 10.4.1.66:6082 -S /etc/varnish/secret ban 'req.url ~ '.*''
107
108
CMD
109
        , file_get_contents($tmpFile));
110
111
        unlink($tmpFile);
112
    }
113
}
114