CacheWarmerTest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(), 'sf_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. }