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 Jul 5, 2016
1 parent 36c95e2 commit 8f4618d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 28 deletions.
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ cache:

env:
matrix:
- SYMFONY_VERSION=3.1.*
- SYMFONY_VERSION=2.8.*
global:
- SYMFONY_DEPRECATIONS_HELPER=weak

matrix:
include:
- php: 7.0
env: DEPS=dev SYMFONY_VERSION=3.2.*
env: DEPS=dev SYMFONY_VERSION=3.1.*
- php: 5.5
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.0
env: SYMFONY_VERSION=2.8.*
- php: 7.0
env: DEPS=dev COMPOSER_FLAGS="--prefer-stable" SYMFONY_VERSION=3.0.*
- php: 7.0
env: DEPS=dev COMPOSER_FLAGS="--prefer-stable" SYMFONY_VERSION=3.1.*
fast_finish: true

before_install:
Expand Down
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>
6 changes: 5 additions & 1 deletion Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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="Symfony\Cmf\Bundle\SeoBundle\SeoPresentation">
<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());
}
}
20 changes: 0 additions & 20 deletions Tests/Unit/SeoPresentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,4 @@ public function testRedirect()
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $redirect);
$this->assertEquals('/redirect/target', $redirect->getTargetUrl());
}

public function testSeoAwareWithoutCurrentMetadata()
{
$content = $this->getMock('Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SeoAwareContent');
$content
->expects($this->any())
->method('getSeoMetadata')
->will($this->returnValue(null))
;

$content
->expects($this->once())
->method('setSeoMetadata')
->with($this->callback(function ($c) {
return $c instanceof SeoMetadataInterface;
}))
;

$this->seoPresentation->updateSeoPage($content);
}
}

0 comments on commit 8f4618d

Please sign in to comment.