|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\CacheBundle\Adapter\SymfonyCache; |
|
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class SymfonyCacheTest. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Vincent Composieux <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class SymfonyCacheTest extends \PHPUnit_Framework_TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var SymfonyCache |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $cache; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var RouterInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $router; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var Filesystem |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $filesystem; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Sets up cache adapter. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setUp() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->router = $this->getMock('Symfony\Component\Routing\RouterInterface'); |
|
46
|
|
|
$this->filesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem'); |
|
47
|
|
|
|
|
48
|
|
|
$this->cache = new SymfonyCache($this->router, $this->filesystem, '/cache/dir', 'token', false, array('all', 'translations'), array()); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Tests cache initialization. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function testInitCache() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->assertTrue($this->cache->flush(array())); |
|
57
|
|
|
$this->assertTrue($this->cache->flushAll()); |
|
58
|
|
|
|
|
59
|
|
|
$this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache set() method does not exists'); |
|
60
|
|
|
$this->cache->set(array('id' => 5), 'data'); |
|
61
|
|
|
|
|
62
|
|
|
$this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache get() method does not exists'); |
|
63
|
|
|
$this->cache->get(array('id' => 5)); |
|
64
|
|
|
|
|
65
|
|
|
$this->setExpectedException('Sonata\Cache\Exception\UnsupportedException', 'Symfony cache has() method does not exists'); |
|
66
|
|
|
$this->cache->has(array('id' => 5)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Tests cacheAction() method. |
|
71
|
|
|
*/ |
|
72
|
|
|
public function testCacheAction() |
|
73
|
|
|
{ |
|
74
|
|
|
// Given |
|
75
|
|
|
$this->filesystem->expects($this->once())->method('exists')->will($this->returnValue(true)); |
|
|
|
|
|
|
76
|
|
|
$this->filesystem->expects($this->once())->method('remove'); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
// When |
|
79
|
|
|
$response = $this->cache->cacheAction('token', 'translations'); |
|
80
|
|
|
|
|
81
|
|
|
// Then |
|
82
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Response should be 200'); |
|
85
|
|
|
$this->assertEquals('ok', $response->getContent(), 'Response should return "OK"'); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals(2, $response->headers->get('Content-Length')); |
|
88
|
|
|
$this->assertEquals('must-revalidate, no-cache, private', $response->headers->get('Cache-Control')); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Tests cacheAction() method with an invalid token. |
|
93
|
|
|
*/ |
|
94
|
|
|
public function testCacheActionWithInvalidToken() |
|
95
|
|
|
{ |
|
96
|
|
|
// Given |
|
97
|
|
|
// When - Then expect exception |
|
98
|
|
|
$this->setExpectedException('Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException'); |
|
99
|
|
|
|
|
100
|
|
|
$this->cache->cacheAction('invalid-token', 'type'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Tests cacheAction() method with an invalid cache type. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testCacheActionWithInvalidType() |
|
107
|
|
|
{ |
|
108
|
|
|
// Given |
|
109
|
|
|
// When - Then expect exception |
|
110
|
|
|
$this->setExpectedException('\RuntimeException', 'Type "invalid-type" is not defined, allowed types are: "all, translations"'); |
|
111
|
|
|
|
|
112
|
|
|
$this->cache->cacheAction('token', 'invalid-type'); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.