CacheWarmerTest.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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\CacheWarmer;
  13. class CacheWarmerTest extends TestCase
  14. {
  15. protected static $cacheFile;
  16. public static function setUpBeforeClass()
  17. {
  18. self::$cacheFile = tempnam(sys_get_temp_dir(), 'sf2_cache_warmer_dir');
  19. }
  20. public static function tearDownAfterClass()
  21. {
  22. @unlink(self::$cacheFile);
  23. }
  24. public function testWriteCacheFileCreatesTheFile()
  25. {
  26. $warmer = new TestCacheWarmer(self::$cacheFile);
  27. $warmer->warmUp(dirname(self::$cacheFile));
  28. $this->assertFileExists(self::$cacheFile);
  29. }
  30. /**
  31. * @expectedException \RuntimeException
  32. */
  33. public function testWriteNonWritableCacheFileThrowsARuntimeException()
  34. {
  35. $nonWritableFile = '/this/file/is/very/probably/not/writable';
  36. $warmer = new TestCacheWarmer($nonWritableFile);
  37. $warmer->warmUp(dirname($nonWritableFile));
  38. }
  39. }
  40. class TestCacheWarmer extends CacheWarmer
  41. {
  42. protected $file;
  43. public function __construct($file)
  44. {
  45. $this->file = $file;
  46. }
  47. public function warmUp($cacheDir)
  48. {
  49. $this->writeCacheFile($this->file, 'content');
  50. }
  51. public function isOptional()
  52. {
  53. return false;
  54. }
  55. }