Completed
Push — master ( 18de21...1ab94f )
by Grégoire
12s
created

SsiCacheTest::testValidToken()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 17
nc 2
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\Cache\CacheElement;
16
use Sonata\CacheBundle\Adapter\SsiCache;
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
/**
24
 * NEXT_MAJOR: remove interface_exists conditions when dropping sf < 3.1.
25
 */
26
class SsiCacheTest extends TestCase
27
{
28
    private $router;
29
    private $controllerResolver;
30
    private $argumentResolver;
31
    private $cache;
32
33
    protected function setUp()
34
    {
35
        $this->router = $this->createMock(RouterInterface::class);
36
        $this->controllerResolver = $this->createMock(ControllerResolverInterface::class);
37
        if (interface_exists(ArgumentResolverInterface::class)) {
38
            $this->argumentResolver = $this->createMock(ArgumentResolverInterface::class);
39
        }
40
        $this->cache = new SsiCache(
41
            'token',
42
            $this->router,
43
            $this->controllerResolver,
44
            interface_exists(ArgumentResolverInterface::class) ?
45
            $this->argumentResolver :
46
            null
47
        );
48
    }
49
50
    public function testInitCache()
51
    {
52
        $this->router->expects($this->any())
53
            ->method('generate')
54
            ->will($this->returnValue('/cache/esi/TOKEN?controller=asdsad'));
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
            '<!--# include virtual="/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(): void
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
        $resolver = interface_exists(ArgumentResolverInterface::class) ?
106
            $this->argumentResolver :
107
            $this->controllerResolver;
108
109
        $resolver->expects($this->any())
110
            ->method('getArguments')
111
            ->will($this->returnValue([]));
112
113
        $request = Request::create('cache/esi/TOKEN', 'get', [
114
            'token' => '44befdbd93f304ea693023aa6587729bed76a206ecdacfd9bbd9b43fcf2e1664',
115
            'parameters' => [
116
                'controller' => 'asfsat',
117
                'parameters' => [],
118
            ],
119
        ]);
120
121
        $this->cache->cacheAction($request);
122
    }
123
124
    /**
125
     * @group legacy
126
     * @expectedDeprecation Not providing a "Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface" instance to "Sonata\CacheBundle\Adapter\SsiCache::__construct" is deprecated since 3.x and will not be possible in 4.0
127
     */
128
    public function testConstructorLegacy()
129
    {
130
        if (!interface_exists(ArgumentResolverInterface::class)) {
131
            $this->markTestSkipped(
132
                'Running Symfony < 3.1'
133
            );
134
        }
135
136
        new SsiCache('token', $this->router, $this->controllerResolver);
137
    }
138
}
139