diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php index 38413934f35d..eaef8d5b1e9b 100644 --- a/src/Symfony/Component/Config/Resource/DirectoryResource.php +++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php @@ -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)); + } } /** diff --git a/src/Symfony/Component/Config/Resource/FileResource.php b/src/Symfony/Component/Config/Resource/FileResource.php index 54a974caef3d..f94640fffc34 100644 --- a/src/Symfony/Component/Config/Resource/FileResource.php +++ b/src/Symfony/Component/Config/Resource/FileResource.php @@ -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 @@ public function __construct($resource) */ 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 @@ public function getResource() */ 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() diff --git a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php index 2bbaadc3a721..507224f7c4da 100644 --- a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php @@ -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 @@ public function testGetResource() 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'); } diff --git a/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php index db85cf7bd0ee..4151f66bc5ca 100644 --- a/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php @@ -29,6 +29,10 @@ protected function setUp() protected function tearDown() { + if (!file_exists($this->file)) { + return; + } + unlink($this->file); } @@ -42,14 +46,27 @@ public function testToString() $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()