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

Commit

Permalink
Added cache tests for AnnotationLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Jun 18, 2016
1 parent 1178265 commit a0b6e33
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Resources/config/loaders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
</service>

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

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

<service id="cmf_seo.loader.annotation" class="Symfony\Cmf\Bundle\SeoBundle\Loader\AnnotationLoader" public="false">
<argument type="service" id="cmf_seo.annotation_reader"/>
<argument type="service" id="cache.cmf_seo"/>
</service>
</services>
</container>
7 changes: 5 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<parameters>
<parameter key="cmf_seo.config_values.class">Symfony\Cmf\Bundle\SeoBundle\DependencyInjection\ConfigValues</parameter>
<parameter key="cmf_seo.form.type.seo_metadata.class">Symfony\Cmf\Bundle\SeoBundle\Form\Type\SeoMetadataType</parameter>
<parameter key="cmf_seo.cache.file.class">Symfony\Cmf\Bundle\SeoBundle\Cache\FileCache</parameter>
<parameter key="cmf_seo.presentation.class">Symfony\Cmf\Bundle\SeoBundle\SeoPresentation</parameter>
<parameter key="cmf_seo.error.suggestion_provider.controller.class">Symfony\Cmf\Bundle\SeoBundle\Controller\SuggestionProviderController</parameter>
<parameter key="cmf_seo.twig.extension.class">Symfony\Cmf\Bundle\SeoBundle\Twig\Extension\CmfSeoExtension</parameter>
Expand Down Expand Up @@ -36,7 +35,11 @@
<tag name="kernel.cache_clearer" />
</service>

<service id="cmf_seo.cache" alias="cmf_seo.cache.file" />
<service id="cmf_seo.cache.file" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" public="false">
<argument>cmf_seo</argument>
</service>

<service id="cache.cmf_seo" alias="cmf_seo.cache.file" public="false"/>

<service id="cmf_seo.presentation" class="%cmf_seo.presentation.class%">
<argument type="service" id="sonata.seo.page" />
Expand Down
76 changes: 76 additions & 0 deletions Tests/Unit/Loader/BaseAnnotationLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,80 @@ public function testExtrasAnnotationWithScalar()

$this->loader->load($content);
}

public function testCaching()
{
// promises
$annotations = $this->getMockBuilder('Symfony\Cmf\Bundle\SeoBundle\Cache\CachedCollection')
->disableOriginalConstructor()
->getMock();
$annotations
->expects($this->any())
->method('isFresh')
->will($this->returnValue(true))
;
$annotations
->expects($this->any())
->method('getData')
->will($this->returnValue(['properties' => [], 'methods' => []]))
;
$cacheItemNoHit = $this->getMock('Psr\Cache\CacheItemInterface');
$cacheItemNoHit->expects($this->any())->method('isHit')->will($this->returnValue(false));
$cacheItemNoHit->expects($this->any())->method('get')->will($this->returnValue($annotations));
$cacheItemHit = $this->getMock('Psr\Cache\CacheItemInterface');
$cacheItemHit->expects($this->any())->method('isHit')->will($this->returnValue(true));
$cacheItemHit->expects($this->any())->method('get')->will($this->returnValue($annotations));
$cache = $this->getMock('Psr\Cache\CacheItemPoolInterface');
$cache
->expects($this->any())
->method('getItem')
->will($this->onConsecutiveCalls($cacheItemNoHit, $cacheItemHit))
;
$loader = new AnnotationLoader(new AnnotationReader(), $cache);

// predictions
$cache
->expects($this->once())
->method('save')
;

$loader->load($this->getContent());
$loader->load($this->getContent());
}

public function testCacheRefresh()
{
// promises
$annotations = $this->getMockBuilder('Symfony\Cmf\Bundle\SeoBundle\Cache\CachedCollection')
->disableOriginalConstructor()
->getMock();
$annotations
->expects($this->any())
->method('isFresh')
->will($this->returnValue(false))
;
$annotations
->expects($this->any())
->method('getData')
->will($this->returnValue(['properties' => [], 'methods' => []]))
;
$cacheItem = $this->getMock('Psr\Cache\CacheItemInterface');
$cacheItem->expects($this->any())->method('isHit')->will($this->returnValue(true));
$cacheItem->expects($this->any())->method('get')->will($this->returnValue($annotations));
$cache = $this->getMock('Psr\Cache\CacheItemPoolInterface');
$cache
->expects($this->any())
->method('getItem')
->will($this->returnValue($cacheItem))
;
$loader = new AnnotationLoader(new AnnotationReader(), $cache);

// predictions
$cache
->expects($this->once())
->method('save')
;

$loader->load($this->getContent());
}
}

0 comments on commit a0b6e33

Please sign in to comment.