Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,14 @@ public function isExpired(string $filename): bool
$this->getFileResolvedPath($filename)
);

$cache_fileatime = fileatime(
// mtime, not atime: every render reads the cache and moves its atime.
// And >= because mtimes are whole seconds: a template saved again in
// the same second it was compiled was otherwise never compiled again.
$cache_filemtime = filemtime(
$this->getCacheFileResolvedPath($filename)
);

return $fileatime > $cache_fileatime;
return $fileatime >= $cache_filemtime;
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,85 @@ public function testGetFileResolvedPathRejectsTraversal()
$this->filesystem->exists('../../../../../../etc/passwd')
);
}

/**
* Build a filesystem over a scratch directory holding one template, so
* the fixtures' timestamps are never touched.
*
* @return array{0: Filesystem, 1: string, 2: string} filesystem, template path, cache path
*/
private function makeScratchFilesystem(): array
{
$root = sys_get_temp_dir() . '/tintin_fs_' . uniqid();
mkdir($root . '/view', 0777, true);
mkdir($root . '/cache', 0777, true);

$template = $root . '/view/probe.tintin.php';
file_put_contents($template, 'Hello {{ $name }}');

$filesystem = new Filesystem([
'path' => $root . '/view',
'extension' => 'tintin.php',
'cache' => $root . '/cache',
]);

$filesystem->cache('probe', '<?php echo "compiled"; ?>');

return [$filesystem, $template, $filesystem->getCacheFileResolvedPath('probe')];
}

/**
* A template saved again in the same second it was compiled (mtimes are
* whole seconds) must be compiled again, not served stale forever.
*/
public function testIsExpiredWhenTemplateEditedInTheSameSecondAsItsCache()
{
[$filesystem, $template, $cache] = $this->makeScratchFilesystem();

$now = time();
touch($template, $now);
touch($cache, $now);
clearstatcache();

$this->assertTrue($filesystem->isExpired('probe'));
}

public function testIsExpiredFollowsTheTemplateMtime()
{
[$filesystem, $template, $cache] = $this->makeScratchFilesystem();

touch($template, time() - 100);
touch($cache, time() - 50);
clearstatcache();
$this->assertFalse($filesystem->isExpired('probe'), 'cache newer than the template is fresh');

touch($template, time() - 10);
clearstatcache();
$this->assertTrue($filesystem->isExpired('probe'), 'template saved after the cache is stale');
}

/**
* Rendering reads the cache and moves its atime; that must not count as
* the cache being "newer" than a template edited afterwards.
*/
public function testIsExpiredIgnoresTheCacheAccessTime()
{
[$filesystem, $template, $cache] = $this->makeScratchFilesystem();

touch($cache, time() - 100, time() + 3600); // old mtime, atime in the future
touch($template, time() - 50);
clearstatcache();

$this->assertTrue($filesystem->isExpired('probe'));
}

public function testIsExpiredWhenNothingIsCached()
{
[$filesystem, , $cache] = $this->makeScratchFilesystem();

unlink($cache);
clearstatcache();

$this->assertTrue($filesystem->isExpired('probe'));
}
}
Loading