This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Config/Tests/ResourceCheckerConfigCacheTest.php

151 lines
4.9 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests;
2017-02-18 17:42:54 +00:00
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Config\ResourceCheckerConfigCache;
2018-07-26 10:03:18 +01:00
use Symfony\Component\Config\Tests\Resource\ResourceStub;
2017-02-18 17:42:54 +00:00
class ResourceCheckerConfigCacheTest extends TestCase
{
private $cacheFile = null;
protected function setUp(): void
{
$this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
}
protected function tearDown(): void
{
2019-01-16 09:39:14 +00:00
$files = [$this->cacheFile, "{$this->cacheFile}.meta"];
foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function testGetPath()
{
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$this->assertSame($this->cacheFile, $cache->getPath());
}
public function testCacheIsNotFreshIfEmpty()
{
2016-12-19 15:48:05 +00:00
$checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock()
->expects($this->never())->method('supports');
/* If there is nothing in the cache, it needs to be filled (and thus it's not fresh).
It does not matter if you provide checkers or not. */
unlink($this->cacheFile); // remove tempnam() side effect
2019-01-16 09:39:14 +00:00
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker]);
$this->assertFalse($cache->isFresh());
}
public function testCacheIsFreshIfNoCheckerProvided()
{
/* For example in prod mode, you may choose not to run any checkers
at all. In that case, the cache should always be considered fresh. */
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$this->assertTrue($cache->isFresh());
}
public function testCacheIsFreshIfEmptyCheckerIteratorProvided()
{
2019-01-16 09:39:14 +00:00
$cache = new ResourceCheckerConfigCache($this->cacheFile, new \ArrayIterator([]));
$this->assertTrue($cache->isFresh());
}
public function testResourcesWithoutcheckersAreIgnoredAndConsideredFresh()
{
/* As in the previous test, but this time we have a resource. */
$cache = new ResourceCheckerConfigCache($this->cacheFile);
2019-01-16 09:39:14 +00:00
$cache->write('', [new ResourceStub()]);
$this->assertTrue($cache->isFresh()); // no (matching) ResourceChecker passed
}
public function testIsFreshWithchecker()
{
2016-12-19 15:48:05 +00:00
$checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
$checker->expects($this->once())
->method('supports')
->willReturn(true);
$checker->expects($this->once())
->method('isFresh')
->willReturn(true);
2019-01-16 09:39:14 +00:00
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker]);
$cache->write('', [new ResourceStub()]);
$this->assertTrue($cache->isFresh());
}
public function testIsNotFreshWithchecker()
{
2016-12-19 15:48:05 +00:00
$checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
$checker->expects($this->once())
->method('supports')
->willReturn(true);
$checker->expects($this->once())
->method('isFresh')
->willReturn(false);
2019-01-16 09:39:14 +00:00
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker]);
$cache->write('', [new ResourceStub()]);
$this->assertFalse($cache->isFresh());
}
public function testCacheIsNotFreshWhenUnserializeFails()
{
2016-12-19 15:48:05 +00:00
$checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
2019-01-16 09:39:14 +00:00
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker]);
$cache->write('foo', [new FileResource(__FILE__)]);
$metaFile = "{$this->cacheFile}.meta";
file_put_contents($metaFile, str_replace('FileResource', 'ClassNotHere', file_get_contents($metaFile)));
$this->assertFalse($cache->isFresh());
}
public function testCacheKeepsContent()
{
$cache = new ResourceCheckerConfigCache($this->cacheFile);
$cache->write('FOOBAR');
$this->assertSame('FOOBAR', file_get_contents($cache->getPath()));
}
public function testCacheIsNotFreshIfNotExistsMetaFile()
{
$checker = $this->getMockBuilder('\Symfony\Component\Config\ResourceCheckerInterface')->getMock();
2019-01-16 09:39:14 +00:00
$cache = new ResourceCheckerConfigCache($this->cacheFile, [$checker]);
$cache->write('foo', [new FileResource(__FILE__)]);
$metaFile = "{$this->cacheFile}.meta";
unlink($metaFile);
$this->assertFalse($cache->isFresh());
}
}