CacheWarmerAggregateTest.php 2.9 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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
  13. class CacheWarmerAggregateTest extends TestCase
  14. {
  15. protected static $cacheDir;
  16. public static function setUpBeforeClass()
  17. {
  18. self::$cacheDir = tempnam(sys_get_temp_dir(), 'sf2_cache_warmer_dir');
  19. }
  20. public static function tearDownAfterClass()
  21. {
  22. @unlink(self::$cacheDir);
  23. }
  24. public function testInjectWarmersUsingConstructor()
  25. {
  26. $warmer = $this->getCacheWarmerMock();
  27. $warmer
  28. ->expects($this->once())
  29. ->method('warmUp');
  30. $aggregate = new CacheWarmerAggregate(array($warmer));
  31. $aggregate->warmUp(self::$cacheDir);
  32. }
  33. public function testInjectWarmersUsingAdd()
  34. {
  35. $warmer = $this->getCacheWarmerMock();
  36. $warmer
  37. ->expects($this->once())
  38. ->method('warmUp');
  39. $aggregate = new CacheWarmerAggregate();
  40. $aggregate->add($warmer);
  41. $aggregate->warmUp(self::$cacheDir);
  42. }
  43. public function testInjectWarmersUsingSetWarmers()
  44. {
  45. $warmer = $this->getCacheWarmerMock();
  46. $warmer
  47. ->expects($this->once())
  48. ->method('warmUp');
  49. $aggregate = new CacheWarmerAggregate();
  50. $aggregate->setWarmers(array($warmer));
  51. $aggregate->warmUp(self::$cacheDir);
  52. }
  53. public function testWarmupDoesCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsEnabled()
  54. {
  55. $warmer = $this->getCacheWarmerMock();
  56. $warmer
  57. ->expects($this->never())
  58. ->method('isOptional');
  59. $warmer
  60. ->expects($this->once())
  61. ->method('warmUp');
  62. $aggregate = new CacheWarmerAggregate(array($warmer));
  63. $aggregate->enableOptionalWarmers();
  64. $aggregate->warmUp(self::$cacheDir);
  65. }
  66. public function testWarmupDoesNotCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsNotEnabled()
  67. {
  68. $warmer = $this->getCacheWarmerMock();
  69. $warmer
  70. ->expects($this->once())
  71. ->method('isOptional')
  72. ->will($this->returnValue(true));
  73. $warmer
  74. ->expects($this->never())
  75. ->method('warmUp');
  76. $aggregate = new CacheWarmerAggregate(array($warmer));
  77. $aggregate->warmUp(self::$cacheDir);
  78. }
  79. protected function getCacheWarmerMock()
  80. {
  81. $warmer = $this->getMockBuilder('Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface')
  82. ->disableOriginalConstructor()
  83. ->getMock();
  84. return $warmer;
  85. }
  86. }