Improved FileResource and DirectoryResource

This commit is contained in:
Javier Eguiluz 2016-01-29 10:11:55 +01:00 committed by Fabien Potencier
parent b33eb05b7c
commit 3e762a6f15
4 changed files with 52 additions and 13 deletions

View File

@ -26,11 +26,17 @@ class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
*
* @param string $resource The file path to the resource
* @param string|null $pattern A pattern to restrict monitored files
*
* @throws \InvalidArgumentException
*/
public function __construct($resource, $pattern = null)
{
$this->resource = $resource;
$this->resource = realpath($resource);
$this->pattern = $pattern;
if (false === $this->resource || !is_dir($this->resource)) {
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $resource));
}
}
/**

View File

@ -29,10 +29,16 @@ class FileResource implements SelfCheckingResourceInterface, \Serializable
* Constructor.
*
* @param string $resource The file path to the resource
*
* @throws \InvalidArgumentException
*/
public function __construct($resource)
{
$this->resource = realpath($resource);
if (false === $this->resource) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource));
}
}
/**
@ -40,11 +46,11 @@ class FileResource implements SelfCheckingResourceInterface, \Serializable
*/
public function __toString()
{
return (string) $this->resource;
return $this->resource;
}
/**
* @return string|false The canonicalized, absolute path to the resource or false if the resource does not exist.
* @return string The canonicalized, absolute path to the resource.
*/
public function getResource()
{
@ -56,11 +62,7 @@ class FileResource implements SelfCheckingResourceInterface, \Serializable
*/
public function isFresh($timestamp)
{
if (false === $this->resource || !file_exists($this->resource)) {
return false;
}
return filemtime($this->resource) <= $timestamp;
return file_exists($this->resource) && @filemtime($this->resource) <= $timestamp;
}
public function serialize()

View File

@ -19,7 +19,7 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->directory = sys_get_temp_dir().'/symfonyDirectoryIterator';
$this->directory = realpath(sys_get_temp_dir()).'/symfonyDirectoryIterator';
if (!file_exists($this->directory)) {
mkdir($this->directory);
}
@ -58,17 +58,31 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
public function testGetPattern()
{
$resource = new DirectoryResource('foo', 'bar');
$resource = new DirectoryResource($this->directory, 'bar');
$this->assertEquals('bar', $resource->getPattern());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The directory ".*" does not exist./
*/
public function testResourceDoesNotExist()
{
$resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
}
public function testIsFresh()
{
$resource = new DirectoryResource($this->directory);
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
$this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
}
public function testIsFreshForDeletedResources()
{
$resource = new DirectoryResource($this->directory);
$this->removeDirectory($this->directory);
$resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
}

View File

@ -29,6 +29,10 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
protected function tearDown()
{
if (!file_exists($this->file)) {
return;
}
unlink($this->file);
}
@ -42,14 +46,27 @@ class FileResourceTest extends \PHPUnit_Framework_TestCase
$this->assertSame(realpath($this->file), (string) $this->resource);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The file ".*" does not exist./
*/
public function testResourceDoesNotExist()
{
$resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999));
}
public function testIsFresh()
{
$this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
$this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
$this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
}
$resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999));
$this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist');
public function testIsFreshForDeletedResources()
{
unlink($this->file);
$this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist');
}
public function testSerializeUnserialize()