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

Commit 8f4618d

Browse files
committed
Added cache tests for AnnotationLoader
1 parent 36c95e2 commit 8f4618d

File tree

5 files changed

+85
-28
lines changed

5 files changed

+85
-28
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,18 @@ cache:
1414

1515
env:
1616
matrix:
17-
- SYMFONY_VERSION=3.1.*
17+
- SYMFONY_VERSION=2.8.*
1818
global:
1919
- SYMFONY_DEPRECATIONS_HELPER=weak
2020

2121
matrix:
2222
include:
2323
- php: 7.0
24-
env: DEPS=dev SYMFONY_VERSION=3.2.*
24+
env: DEPS=dev SYMFONY_VERSION=3.1.*
2525
- php: 5.5
2626
env: COMPOSER_FLAGS="--prefer-lowest"
27-
- php: 7.0
28-
env: SYMFONY_VERSION=2.8.*
2927
- php: 7.0
3028
env: DEPS=dev COMPOSER_FLAGS="--prefer-stable" SYMFONY_VERSION=3.0.*
31-
- php: 7.0
32-
env: DEPS=dev COMPOSER_FLAGS="--prefer-stable" SYMFONY_VERSION=3.1.*
3329
fast_finish: true
3430

3531
before_install:

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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
<tag name="kernel.cache_clearer" />
2828
</service>
2929

30-
<service id="cmf_seo.cache" alias="cmf_seo.cache.file" />
30+
<service id="cmf_seo.cache.file" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" public="false">
31+
<argument>cmf_seo</argument>
32+
</service>
33+
34+
<service id="cache.cmf_seo" alias="cmf_seo.cache.file" public="false"/>
3135

3236
<service id="cmf_seo.presentation" class="Symfony\Cmf\Bundle\SeoBundle\SeoPresentation">
3337
<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
}

Tests/Unit/SeoPresentationTest.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,24 +197,4 @@ public function testRedirect()
197197
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $redirect);
198198
$this->assertEquals('/redirect/target', $redirect->getTargetUrl());
199199
}
200-
201-
public function testSeoAwareWithoutCurrentMetadata()
202-
{
203-
$content = $this->getMock('Symfony\Cmf\Bundle\SeoBundle\Tests\Resources\Document\SeoAwareContent');
204-
$content
205-
->expects($this->any())
206-
->method('getSeoMetadata')
207-
->will($this->returnValue(null))
208-
;
209-
210-
$content
211-
->expects($this->once())
212-
->method('setSeoMetadata')
213-
->with($this->callback(function ($c) {
214-
return $c instanceof SeoMetadataInterface;
215-
}))
216-
;
217-
218-
$this->seoPresentation->updateSeoPage($content);
219-
}
220200
}

0 commit comments

Comments
 (0)