1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12use Twig\Environment; 13use Twig\Loader\ArrayLoader; 14 15require_once __DIR__.'/FilesystemHelper.php'; 16 17class Twig_Tests_FileCachingTest extends \PHPUnit\Framework\TestCase 18{ 19 private $env; 20 private $tmpDir; 21 22 protected function setUp() 23 { 24 $this->tmpDir = sys_get_temp_dir().'/TwigTests'; 25 if (!file_exists($this->tmpDir)) { 26 @mkdir($this->tmpDir, 0777, true); 27 } 28 29 if (!is_writable($this->tmpDir)) { 30 $this->markTestSkipped(sprintf('Unable to run the tests as "%s" is not writable.', $this->tmpDir)); 31 } 32 33 $this->env = new Environment(new ArrayLoader(['index' => 'index', 'index2' => 'index2']), ['cache' => $this->tmpDir]); 34 } 35 36 protected function tearDown() 37 { 38 Twig_Tests_FilesystemHelper::removeDir($this->tmpDir); 39 } 40 41 /** 42 * @group legacy 43 */ 44 public function testWritingCacheFiles() 45 { 46 $name = 'index'; 47 $this->env->load($name); 48 $cacheFileName = $this->env->getCacheFilename($name); 49 50 $this->assertFileExists($cacheFileName, 'Cache file does not exist.'); 51 } 52 53 /** 54 * @group legacy 55 */ 56 public function testClearingCacheFiles() 57 { 58 $name = 'index2'; 59 $this->env->load($name); 60 $cacheFileName = $this->env->getCacheFilename($name); 61 62 $this->assertFileExists($cacheFileName, 'Cache file does not exist.'); 63 $this->env->clearCacheFiles(); 64 $this->assertFileNotExists($cacheFileName, 'Cache file was not cleared.'); 65 } 66} 67