Skip to content

Commit

Permalink
merged branch jakzal/2.3-config-tests (PR #8178)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fabpot committed Jun 4, 2013
2 parents e1f530e + 804b182 commit b219e0a
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 39 deletions.
138 changes: 138 additions & 0 deletions src/Symfony/Component/Config/Tests/ConfigCacheTest.php
@@ -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))));
}
}
Expand Up @@ -18,7 +18,7 @@
class FileLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Symfony\Component\Config\Loader\FileLoader::import
* @covers Symfony\Component\Config\Loader\FileLoader
*/
public function testImport()
{
Expand Down
Expand Up @@ -50,13 +50,11 @@ protected function removeDirectory($directory)
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()
Expand All @@ -65,9 +63,6 @@ public function testGetPattern()
$this->assertEquals('bar', $resource->getPattern());
}

/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
*/
public function testIsFresh()
{
$resource = new DirectoryResource($this->directory);
Expand All @@ -78,49 +73,34 @@ public function testIsFresh()
$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);
touch($this->directory.'/tmp.xml', time() + 20);
$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);
touch($this->directory.'/new.xml', time() + 20);
$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);
unlink($this->directory.'/tmp.xml');
$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);
$this->removeDirectory($this->directory);
$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';
Expand All @@ -133,9 +113,6 @@ public function testIsFreshCreateFileInSubdirectory()
$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);
Expand All @@ -147,9 +124,6 @@ public function testIsFreshModifySubdirectory()
$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)$/');
Expand All @@ -158,14 +132,21 @@ public function testFilterRegexListNoMatch()
$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)$/');

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());
}
}
20 changes: 13 additions & 7 deletions src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php
Expand Up @@ -30,17 +30,16 @@ protected function tearDown()
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');
Expand All @@ -49,4 +48,11 @@ public function testIsFresh()
$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());
}
}

0 comments on commit b219e0a

Please sign in to comment.