Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit a0b6e33

Browse files
committed
Added cache tests for AnnotationLoader
1 parent 1178265 commit a0b6e33

File tree

3 files changed

+83
-3
lines changed

3 files changed

+83
-3
lines changed

Resources/config/loaders.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
</service>
1919

2020
<service id="cmf_seo.loader.extractor" class="Symfony\Cmf\Bundle\SeoBundle\Loader\ExtractorLoader" public="false">
21-
<argument type="service" id="cmf_seo.cache"/>
21+
<argument type="service" id="cache.cmf_seo"/>
2222
</service>
2323

2424
<service id="cmf_seo.annotation_reader" class="Doctrine\Common\Annotations\AnnotationReader" public="false"/>
2525

2626
<service id="cmf_seo.loader.annotation" class="Symfony\Cmf\Bundle\SeoBundle\Loader\AnnotationLoader" public="false">
2727
<argument type="service" id="cmf_seo.annotation_reader"/>
28+
<argument type="service" id="cache.cmf_seo"/>
2829
</service>
2930
</services>
3031
</container>

Resources/config/services.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<parameters>
99
<parameter key="cmf_seo.config_values.class">Symfony\Cmf\Bundle\SeoBundle\DependencyInjection\ConfigValues</parameter>
1010
<parameter key="cmf_seo.form.type.seo_metadata.class">Symfony\Cmf\Bundle\SeoBundle\Form\Type\SeoMetadataType</parameter>
11-
<parameter key="cmf_seo.cache.file.class">Symfony\Cmf\Bundle\SeoBundle\Cache\FileCache</parameter>
1211
<parameter key="cmf_seo.presentation.class">Symfony\Cmf\Bundle\SeoBundle\SeoPresentation</parameter>
1312
<parameter key="cmf_seo.error.suggestion_provider.controller.class">Symfony\Cmf\Bundle\SeoBundle\Controller\SuggestionProviderController</parameter>
1413
<parameter key="cmf_seo.twig.extension.class">Symfony\Cmf\Bundle\SeoBundle\Twig\Extension\CmfSeoExtension</parameter>
@@ -36,7 +35,11 @@
3635
<tag name="kernel.cache_clearer" />
3736
</service>
3837

39-
<service id="cmf_seo.cache" alias="cmf_seo.cache.file" />
38+
<service id="cmf_seo.cache.file" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" public="false">
39+
<argument>cmf_seo</argument>
40+
</service>
41+
42+
<service id="cache.cmf_seo" alias="cmf_seo.cache.file" public="false"/>
4043

4144
<service id="cmf_seo.presentation" class="%cmf_seo.presentation.class%">
4245
<argument type="service" id="sonata.seo.page" />

Tests/Unit/Loader/BaseAnnotationLoaderTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,80 @@ public function testExtrasAnnotationWithScalar()
100100

101101
$this->loader->load($content);
102102
}
103+
104+
public function testCaching()
105+
{
106+
// promises
107+
$annotations = $this->getMockBuilder('Symfony\Cmf\Bundle\SeoBundle\Cache\CachedCollection')
108+
->disableOriginalConstructor()
109+
->getMock();
110+
$annotations
111+
->expects($this->any())
112+
->method('isFresh')
113+
->will($this->returnValue(true))
114+
;
115+
$annotations
116+
->expects($this->any())
117+
->method('getData')
118+
->will($this->returnValue(['properties' => [], 'methods' => []]))
119+
;
120+
$cacheItemNoHit = $this->getMock('Psr\Cache\CacheItemInterface');
121+
$cacheItemNoHit->expects($this->any())->method('isHit')->will($this->returnValue(false));
122+
$cacheItemNoHit->expects($this->any())->method('get')->will($this->returnValue($annotations));
123+
$cacheItemHit = $this->getMock('Psr\Cache\CacheItemInterface');
124+
$cacheItemHit->expects($this->any())->method('isHit')->will($this->returnValue(true));
125+
$cacheItemHit->expects($this->any())->method('get')->will($this->returnValue($annotations));
126+
$cache = $this->getMock('Psr\Cache\CacheItemPoolInterface');
127+
$cache
128+
->expects($this->any())
129+
->method('getItem')
130+
->will($this->onConsecutiveCalls($cacheItemNoHit, $cacheItemHit))
131+
;
132+
$loader = new AnnotationLoader(new AnnotationReader(), $cache);
133+
134+
// predictions
135+
$cache
136+
->expects($this->once())
137+
->method('save')
138+
;
139+
140+
$loader->load($this->getContent());
141+
$loader->load($this->getContent());
142+
}
143+
144+
public function testCacheRefresh()
145+
{
146+
// promises
147+
$annotations = $this->getMockBuilder('Symfony\Cmf\Bundle\SeoBundle\Cache\CachedCollection')
148+
->disableOriginalConstructor()
149+
->getMock();
150+
$annotations
151+
->expects($this->any())
152+
->method('isFresh')
153+
->will($this->returnValue(false))
154+
;
155+
$annotations
156+
->expects($this->any())
157+
->method('getData')
158+
->will($this->returnValue(['properties' => [], 'methods' => []]))
159+
;
160+
$cacheItem = $this->getMock('Psr\Cache\CacheItemInterface');
161+
$cacheItem->expects($this->any())->method('isHit')->will($this->returnValue(true));
162+
$cacheItem->expects($this->any())->method('get')->will($this->returnValue($annotations));
163+
$cache = $this->getMock('Psr\Cache\CacheItemPoolInterface');
164+
$cache
165+
->expects($this->any())
166+
->method('getItem')
167+
->will($this->returnValue($cacheItem))
168+
;
169+
$loader = new AnnotationLoader(new AnnotationReader(), $cache);
170+
171+
// predictions
172+
$cache
173+
->expects($this->once())
174+
->method('save')
175+
;
176+
177+
$loader->load($this->getContent());
178+
}
103179
}

0 commit comments

Comments
 (0)