Skip to content

Commit

Permalink
Improved FileResource and DirectoryResource
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz authored and fabpot committed Feb 3, 2016
1 parent b33eb05 commit 3e762a6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/Symfony/Component/Config/Resource/DirectoryResource.php
Expand Up @@ -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));
}
}

/**
Expand Down
16 changes: 9 additions & 7 deletions src/Symfony/Component/Config/Resource/FileResource.php
Expand Up @@ -29,22 +29,28 @@ 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));
}
}

/**
* {@inheritdoc}
*/
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()
{
Expand All @@ -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()
Expand Down
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');
}

Expand Down
21 changes: 19 additions & 2 deletions src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php
Expand Up @@ -29,6 +29,10 @@ protected function setUp()

protected function tearDown()
{
if (!file_exists($this->file)) {
return;
}

unlink($this->file);
}

Expand All @@ -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()
Expand Down

0 comments on commit 3e762a6

Please sign in to comment.