merged branch jakzal/2.3-config-tests (PR #8178)

This PR was merged into the 2.3 branch.

Discussion
----------

[Config] Added few tests

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

804b182 [Config] Added tests for the FileResource and DirectoryResource.
c5dda79 [Config] Fixed @covers annotation which ignored some of the methods from the code coverage.
bf769e0 [Config] Added tests for the ConfigCache.
This commit is contained in:
Fabien Potencier 2013-06-04 17:04:14 +02:00
commit b219e0a9c6
4 changed files with 164 additions and 39 deletions

View File

@ -0,0 +1,138 @@
<?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;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;
class ConfigTest extends \PHPUnit_Framework_TestCase
{
private $resourceFile = null;
private $cacheFile = null;
private $metaFile = null;
public function setUp()
{
$this->resourceFile = tempnam(sys_get_temp_dir(), '_resource');
$this->cacheFile = tempnam(sys_get_temp_dir(), 'config_');
$this->metaFile = $this->cacheFile.'.meta';
$this->makeCacheFresh();
$this->generateMetaFile();
}
public function tearDown()
{
$files = array($this->cacheFile, $this->metaFile, $this->resourceFile);
foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
public function testToString()
{
$cache = new ConfigCache($this->cacheFile, true);
$this->assertSame($this->cacheFile, (string) $cache);
}
public function testCacheIsNotFreshIfFileDoesNotExist()
{
unlink($this->cacheFile);
$cache = new ConfigCache($this->cacheFile, false);
$this->assertFalse($cache->isFresh());
}
public function testCacheIsAlwaysFreshIfFileExistsWithDebugDisabled()
{
$this->makeCacheStale();
$cache = new ConfigCache($this->cacheFile, false);
$this->assertTrue($cache->isFresh());
}
public function testCacheIsNotFreshWithoutMetaFile()
{
unlink($this->metaFile);
$cache = new ConfigCache($this->cacheFile, true);
$this->assertFalse($cache->isFresh());
}
public function testCacheIsFreshIfResourceIsFresh()
{
$cache = new ConfigCache($this->cacheFile, true);
$this->assertTrue($cache->isFresh());
}
public function testCacheIsNotFreshIfOneOfTheResourcesIsNotFresh()
{
$this->makeCacheStale();
$cache = new ConfigCache($this->cacheFile, true);
$this->assertFalse($cache->isFresh());
}
public function testWriteDumpsFile()
{
unlink($this->cacheFile);
unlink($this->metaFile);
$cache = new ConfigCache($this->cacheFile, false);
$cache->write('FOOBAR');
$this->assertFileExists($this->cacheFile, 'Cache file is created');
$this->assertSame('FOOBAR', file_get_contents($this->cacheFile));
$this->assertFileNotExists($this->metaFile, 'Meta file is not created');
}
public function testWriteDumpsMetaFileWithDebugEnabled()
{
unlink($this->cacheFile);
unlink($this->metaFile);
$metadata = array(new FileResource($this->resourceFile));
$cache = new ConfigCache($this->cacheFile, true);
$cache->write('FOOBAR', $metadata);
$this->assertFileExists($this->cacheFile, 'Cache file is created');
$this->assertFileExists($this->metaFile, 'Meta file is created');
$this->assertSame(serialize($metadata), file_get_contents($this->metaFile));
}
private function makeCacheFresh()
{
touch($this->resourceFile, filemtime($this->cacheFile) - 3600);
}
private function makeCacheStale()
{
touch($this->cacheFile, time() - 3600);
}
private function generateMetaFile()
{
file_put_contents($this->metaFile, serialize(array(new FileResource($this->resourceFile))));
}
}

View File

@ -18,7 +18,7 @@ use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceExceptio
class FileLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\Config\Loader\FileLoader::import
* @covers Symfony\Component\Config\Loader\FileLoader
*/
public function testImport()
{

View File

@ -50,13 +50,11 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
rmdir($directory);
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::getResource
*/
public function testGetResource()
{
$resource = new DirectoryResource($this->directory);
$this->assertEquals($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
$this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
$this->assertSame($this->directory, (string) $resource, '->__toString() returns the path to the resource');
}
public function testGetPattern()
@ -65,9 +63,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('bar', $resource->getPattern());
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFresh()
{
$resource = new DirectoryResource($this->directory);
@ -78,9 +73,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshUpdateFile()
{
$resource = new DirectoryResource($this->directory);
@ -88,9 +80,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an existing file is modified');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshNewFile()
{
$resource = new DirectoryResource($this->directory);
@ -98,9 +87,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshDeleteFile()
{
$resource = new DirectoryResource($this->directory);
@ -108,9 +94,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if an existing file is removed');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshDeleteDirectory()
{
$resource = new DirectoryResource($this->directory);
@ -118,9 +101,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the whole resource is removed');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshCreateFileInSubdirectory()
{
$subdirectory = $this->directory.'/subdirectory';
@ -133,9 +113,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file in a subdirectory is added');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFreshModifySubdirectory()
{
$resource = new DirectoryResource($this->directory);
@ -147,9 +124,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a subdirectory is modified (e.g. a file gets deleted)');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testFilterRegexListNoMatch()
{
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
@ -158,9 +132,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file not matching the filter regex is created');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testFilterRegexListMatch()
{
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
@ -168,4 +139,14 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
touch($this->directory.'/new.xml', time() + 20);
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if an new file matching the filter regex is created ');
}
public function testSerializeUnserialize()
{
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
$unserialized = unserialize(serialize($resource));
$this->assertSame($this->directory, $resource->getResource());
$this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
}
}

View File

@ -30,17 +30,16 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
unlink($this->file);
}
/**
* @covers Symfony\Component\Config\Resource\FileResource::getResource
*/
public function testGetResource()
{
$this->assertEquals(realpath($this->file), $this->resource->getResource(), '->getResource() returns the path to the resource');
$this->assertSame(realpath($this->file), $this->resource->getResource(), '->getResource() returns the path to the resource');
}
public function testToString()
{
$this->assertSame(realpath($this->file), (string) $this->resource);
}
/**
* @covers Symfony\Component\Config\Resource\FileResource::isFresh
*/
public function testIsFresh()
{
$this->assertTrue($this->resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
@ -49,4 +48,11 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
$resource = new FileResource('/____foo/foobar'.rand(1, 999999));
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
}
public function testSerializeUnserialize()
{
$unserialized = unserialize(serialize($this->resource));
$this->assertSame($this->file, $this->resource->getResource());
}
}