/
CacheResolverTest.php
206 lines (167 loc) · 6.86 KB
/
CacheResolverTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Tests\Imagine\Cache\Resolver;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Liip\ImagineBundle\Imagine\Cache\Resolver\CacheResolver;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Tests\AbstractTest;
/**
* @covers \Liip\ImagineBundle\Imagine\Cache\Resolver\CacheResolver
*/
class CacheResolverTest extends AbstractTest
{
protected $filter = 'thumbnail';
protected $path = 'MadCat2.jpeg';
protected $webPath = '/media/cache/thumbnail/MadCat2.jpeg';
protected function setUp()
{
if (!class_exists(ArrayCache::class)) {
$this->markTestSkipped('Requires the doctrine/cache package.');
}
parent::setUp();
}
public function testResolveIsSavedToCache()
{
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->once())
->method('resolve')
->with($this->path, $this->filter)
->will($this->returnValue($this->webPath));
$cacheResolver = new CacheResolver(new ArrayCache(), $resolver);
$this->assertEquals($this->webPath, $cacheResolver->resolve($this->path, $this->filter));
// Call multiple times to verify the cache is used.
$this->assertEquals($this->webPath, $cacheResolver->resolve($this->path, $this->filter));
$this->assertEquals($this->webPath, $cacheResolver->resolve($this->path, $this->filter));
}
public function testNotCallInternalResolverIfCachedOnIsStored()
{
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->once())
->method('resolve')
->with($this->path, $this->filter)
->will($this->returnValue($this->webPath));
$resolver
->expects($this->never())
->method('isStored');
$cacheResolver = new CacheResolver(new ArrayCache(), $resolver);
$cacheResolver->resolve($this->path, $this->filter);
// Call multiple times to verify the cache is used.
$this->assertTrue($cacheResolver->isStored($this->path, $this->filter));
$this->assertTrue($cacheResolver->isStored($this->path, $this->filter));
}
public function testCallInternalResolverIfNotCachedOnIsStored()
{
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->exactly(2))
->method('isStored')
->will($this->returnValue(true));
$cacheResolver = new CacheResolver(new ArrayCache(), $resolver);
$this->assertTrue($cacheResolver->isStored($this->path, $this->filter));
$this->assertTrue($cacheResolver->isStored($this->path, $this->filter));
}
public function testStoreIsForwardedToResolver()
{
$binary = new Binary('aContent', 'image/jpeg', 'jpg');
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->exactly(2))
->method('store')
->with($this->identicalTo($binary), $this->webPath, $this->filter);
$cacheResolver = new CacheResolver(new ArrayCache(), $resolver);
// Call twice, as this method should not be cached.
$this->assertNull($cacheResolver->store($binary, $this->webPath, $this->filter));
$this->assertNull($cacheResolver->store($binary, $this->webPath, $this->filter));
}
public function testSavesToCacheIfInternalResolverReturnUrlOnResolve()
{
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->once())
->method('resolve')
->with($this->path, $this->filter)
->will($this->returnValue('/the/expected/browser'));
$cache = $this->getMockBuilder(Cache::class)->getMock();
$cache
->expects($this->exactly(1))
->method('save');
$cacheResolver = new CacheResolver($cache, $resolver);
$cacheResolver->resolve($this->path, $this->filter);
}
public function testRemoveSinglePathCacheOnRemove()
{
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->once())
->method('resolve')
->with($this->path, $this->filter)
->will($this->returnValue($this->webPath));
$resolver
->expects($this->once())
->method('remove');
$cache = new ArrayCache();
$cacheResolver = new CacheResolver($cache, $resolver);
$cacheResolver->resolve($this->path, $this->filter);
/*
* Checking 2 items:
* - The result of one resolve execution.
* - The index of entity.
*/
$this->assertCount(2, $this->getCacheEntries($cache));
$cacheResolver->remove(array($this->path), array($this->filter));
// Cache including index has been removed.
$this->assertCount(0, $this->getCacheEntries($cache));
}
public function testRemoveAllFilterCacheOnRemove()
{
$resolver = $this->createCacheResolverInterfaceMock();
$resolver
->expects($this->exactly(4))
->method('resolve')
->will($this->returnValue('aCachePath'));
$resolver
->expects($this->once())
->method('remove');
$cache = new ArrayCache();
$cacheResolver = new CacheResolver($cache, $resolver);
$cacheResolver->resolve('aPathFoo', 'thumbnail_233x233');
$cacheResolver->resolve('aPathBar', 'thumbnail_233x233');
$cacheResolver->resolve('aPathFoo', 'thumbnail_100x100');
$cacheResolver->resolve('aPathBar', 'thumbnail_100x100');
/*
* Checking 6 items:
* - The result of four resolve execution.
* - The index of two entities.
*/
$this->assertCount(6, $this->getCacheEntries($cache));
$cacheResolver->remove(array(), array('thumbnail_233x233'));
// Cache including index has been removed.
$this->assertCount(3, $this->getCacheEntries($cache));
}
/**
* There's an intermittent cache entry which is a cache namespace
* version, it may or may not be there depending on doctrine-cache
* version. There's no point in checking it anyway since it's a detail
* of doctrine cache implementation.
*
* @param ArrayCache $cache
*
* @return array
*/
private function getCacheEntries(ArrayCache $cache)
{
$cacheEntries = $this->readAttribute($cache, 'data');
unset($cacheEntries['DoctrineNamespaceCacheKey[]']);
return $cacheEntries;
}
}