CacheWarmerAggregateTest.php 3.0 KB

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