Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd97471

Browse filesBrowse files
ornicarfabpot
authored andcommitted
[HttpKernel] Add test coverage for cache warming
1 parent 562ebd5 commit bd97471
Copy full SHA for bd97471

File tree

Expand file treeCollapse file tree

2 files changed

+165
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+165
-0
lines changed
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\HttpKernel\CacheWarmer;
13+
14+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
15+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
16+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
17+
18+
class CacheWarmerAggregateTest extends \PHPUnit_Framework_TestCase
19+
{
20+
protected static $cacheDir;
21+
22+
public static function setUpBeforeClass()
23+
{
24+
self::$cacheDir = tempnam(sys_get_temp_dir(), 'sf2_cache_warmer_dir');
25+
}
26+
27+
public function testInjectWarmersUsingConstructor()
28+
{
29+
$warmer = $this->getCacheWarmerMock();
30+
$warmer
31+
->expects($this->once())
32+
->method('warmUp');
33+
$aggregate = new CacheWarmerAggregate(array($warmer));
34+
$aggregate->warmUp(self::$cacheDir);
35+
}
36+
37+
public function testInjectWarmersUsingAdd()
38+
{
39+
$warmer = $this->getCacheWarmerMock();
40+
$warmer
41+
->expects($this->once())
42+
->method('warmUp');
43+
$aggregate = new CacheWarmerAggregate();
44+
$aggregate->add($warmer);
45+
$aggregate->warmUp(self::$cacheDir);
46+
}
47+
48+
public function testInjectWarmersUsingSetWarmers()
49+
{
50+
$warmer = $this->getCacheWarmerMock();
51+
$warmer
52+
->expects($this->once())
53+
->method('warmUp');
54+
$aggregate = new CacheWarmerAggregate();
55+
$aggregate->setWarmers(array($warmer));
56+
$aggregate->warmUp(self::$cacheDir);
57+
}
58+
59+
public function testWarmupDoesCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsEnabled()
60+
{
61+
$warmer = $this->getCacheWarmerMock();
62+
$warmer
63+
->expects($this->never())
64+
->method('isOptional');
65+
$warmer
66+
->expects($this->once())
67+
->method('warmUp');
68+
69+
$aggregate = new CacheWarmerAggregate(array($warmer));
70+
$aggregate->enableOptionalWarmers();
71+
$aggregate->warmUp(self::$cacheDir);
72+
}
73+
74+
public function testWarmupDoesNotCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsNotEnabled()
75+
{
76+
$warmer = $this->getCacheWarmerMock();
77+
$warmer
78+
->expects($this->once())
79+
->method('isOptional')
80+
->will($this->returnValue(true));
81+
$warmer
82+
->expects($this->never())
83+
->method('warmUp');
84+
85+
$aggregate = new CacheWarmerAggregate(array($warmer));
86+
$aggregate->warmUp(self::$cacheDir);
87+
}
88+
89+
protected function getCacheWarmerMock()
90+
{
91+
$warmer = $this->getMockBuilder('Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface')
92+
->disableOriginalConstructor()
93+
->getMock();
94+
95+
return $warmer;
96+
}
97+
}
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\HttpKernel\CacheWarmer;
13+
14+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
15+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
16+
17+
class CacheWarmerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
protected static $cacheFile;
20+
21+
public static function setUpBeforeClass()
22+
{
23+
self::$cacheFile = tempnam(sys_get_temp_dir(), 'sf2_cache_warmer_dir');
24+
}
25+
26+
public static function tearDownAfterClass()
27+
{
28+
@unlink(self::$cacheFile);
29+
}
30+
31+
public function testWriteCacheFileCreatesTheFile()
32+
{
33+
$warmer = new TestCacheWarmer(self::$cacheFile);
34+
$warmer->warmUp(dirname(self::$cacheFile));
35+
36+
$this->assertTrue(file_exists(self::$cacheFile));
37+
}
38+
39+
/**
40+
* @expectedException \RuntimeException
41+
*/
42+
public function testWriteNonWritableCacheFileThrowsARuntimeException()
43+
{
44+
$nonWritableFile = '/this/file/is/very/probably/not/writable';
45+
$warmer = new TestCacheWarmer($nonWritableFile);
46+
$warmer->warmUp(dirname($nonWritableFile));
47+
}
48+
}
49+
50+
class TestCacheWarmer extends CacheWarmer
51+
{
52+
protected $file;
53+
54+
public function __construct($file)
55+
{
56+
$this->file = $file;
57+
}
58+
59+
public function warmUp($cacheDir)
60+
{
61+
$this->writeCacheFile($this->file, 'content');
62+
}
63+
64+
public function isOptional()
65+
{
66+
return false;
67+
}
68+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.