CacheWarmerTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(): void
  17. {
  18. self::$cacheFile = tempnam(sys_get_temp_dir(), 'sf_cache_warmer_dir');
  19. }
  20. public static function tearDownAfterClass(): void
  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. public function testWriteNonWritableCacheFileThrowsARuntimeException()
  31. {
  32. $this->expectException(\RuntimeException::class);
  33. $nonWritableFile = '/this/file/is/very/probably/not/writable';
  34. $warmer = new TestCacheWarmer($nonWritableFile);
  35. $warmer->warmUp(\dirname($nonWritableFile));
  36. }
  37. }
  38. class TestCacheWarmer extends CacheWarmer
  39. {
  40. protected $file;
  41. public function __construct(string $file)
  42. {
  43. $this->file = $file;
  44. }
  45. /**
  46. * @return string[]
  47. */
  48. public function warmUp(string $cacheDir): array
  49. {
  50. $this->writeCacheFile($this->file, 'content');
  51. return [];
  52. }
  53. public function isOptional(): bool
  54. {
  55. return false;
  56. }
  57. }