[Config] updated DirectoryResource tests

This commit is contained in:
everzet 2011-11-25 12:11:24 +01:00
parent 1f9ba382ee
commit 45a45baf2f
2 changed files with 44 additions and 28 deletions

View File

@ -43,7 +43,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
$childs = array();
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) {
// if regex filtering is enabled only return matching files
if (!$this->isFileMatchesFilters($file)) {
if ($file->isFile() && !$this->isFileMatchesPattern($file)) {
continue;
}
@ -71,7 +71,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
$resources = array();
foreach ($iterator as $file) {
// if regex filtering is enabled only return matching files
if (!$this->isFileMatchesFilters($file)) {
if ($file->isFile() && !$this->isFileMatchesPattern($file)) {
continue;
}
@ -109,6 +109,11 @@ class DirectoryResource implements ResourceInterface, \Serializable
return $this->resource;
}
/**
* Returns check pattern.
*
* @return mixed
*/
public function getPattern()
{
return $this->pattern;
@ -145,7 +150,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
return false;
}
return $this->getModificationTime() <= $timestamp;
return $this->getModificationTime() < $timestamp;
}
/**
@ -155,7 +160,7 @@ class DirectoryResource implements ResourceInterface, \Serializable
*/
public function exists()
{
return file_exists($this->resource);
return is_dir($this->resource);
}
public function serialize()
@ -175,18 +180,10 @@ class DirectoryResource implements ResourceInterface, \Serializable
*
* @return Boolean
*/
private function isFileMatchesFilters(\SplFileInfo $file)
private function isFileMatchesPattern(\SplFileInfo $file)
{
if (isset($this->filterRegexList) && $file->isFile()) {
$regexMatched = false;
foreach ($this->filterRegexList as $regex) {
if (preg_match($regex, (string) $file)) {
$regexMatched = true;
}
}
if (!$regexMatched) {
return false;
}
if ($this->pattern) {
return preg_match($this->pattern, $file->getBasename());
}
return true;

View File

@ -118,20 +118,8 @@ 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::exists
*/
public function testExists()
{
$this->assertTrue($this->resource->exists(), '->exists() returns true if the directory still exist');
$this->removeDirectory($this->directory);
$this->assertFalse($this->resource->exists(), '->exists() returns false if the directory does not exist');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
* @covers Symfony\Component\Config\Resource\DirectoryResource::getModificationTime
*/
public function testIsFreshCreateFileInSubdirectory()
{
@ -161,7 +149,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::isFresh
* @covers Symfony\Component\Config\Resource\DirectoryResource::getFilteredChilds
*/
public function testFilterRegexListNoMatch()
{
@ -181,4 +168,36 @@ 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 ');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::exists
*/
public function testDirectoryExists()
{
$resource = new DirectoryResource($this->directory);
$this->assertTrue($resource->exists(), '->exists() returns true if directory exists ');
unlink($this->directory.'/tmp.xml');
rmdir($this->directory);
$this->assertFalse($resource->exists(), '->exists() returns false if directory does not exists');
}
/**
* @covers Symfony\Component\Config\Resource\DirectoryResource::getModificationTime
*/
public function testGetModificationTime()
{
$resource = new DirectoryResource($this->directory, '/\.(foo|xml)$/');
touch($this->directory.'/new.xml', $time = time() + 20);
$this->assertSame($time, $resource->getModificationTime(), '->getModificationTime() returns time of the last modificated resource');
touch($this->directory.'/some', time() + 60);
$this->assertSame($time, $resource->getModificationTime(), '->getModificationTime() returns time of last modificated resource, that only matches pattern');
touch($this->directory, $time2 = time() + 90);
$this->assertSame($time2, $resource->getModificationTime(), '->getModificationTime() returns modification time of the directory itself');
}
}