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