Skip to content

Commit 711408a

Browse files
javiereguiluzfabpot
authored andcommitted
Improved FileResource and DirectoryResource
1 parent 6ac9bc9 commit 711408a

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

Resource/DirectoryResource.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,17 @@ class DirectoryResource implements SelfCheckingResourceInterface, \Serializable
2626
*
2727
* @param string $resource The file path to the resource
2828
* @param string|null $pattern A pattern to restrict monitored files
29+
*
30+
* @throws \InvalidArgumentException
2931
*/
3032
public function __construct($resource, $pattern = null)
3133
{
32-
$this->resource = $resource;
34+
$this->resource = realpath($resource);
3335
$this->pattern = $pattern;
36+
37+
if (false === $this->resource || !is_dir($this->resource)) {
38+
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $resource));
39+
}
3440
}
3541

3642
/**

Resource/FileResource.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,28 @@ class FileResource implements SelfCheckingResourceInterface, \Serializable
2929
* Constructor.
3030
*
3131
* @param string $resource The file path to the resource
32+
*
33+
* @throws \InvalidArgumentException
3234
*/
3335
public function __construct($resource)
3436
{
3537
$this->resource = realpath($resource);
38+
39+
if (false === $this->resource) {
40+
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource));
41+
}
3642
}
3743

3844
/**
3945
* {@inheritdoc}
4046
*/
4147
public function __toString()
4248
{
43-
return (string) $this->resource;
49+
return $this->resource;
4450
}
4551

4652
/**
47-
* @return string|false The canonicalized, absolute path to the resource or false if the resource does not exist.
53+
* @return string The canonicalized, absolute path to the resource.
4854
*/
4955
public function getResource()
5056
{
@@ -56,11 +62,7 @@ public function getResource()
5662
*/
5763
public function isFresh($timestamp)
5864
{
59-
if (false === $this->resource || !file_exists($this->resource)) {
60-
return false;
61-
}
62-
63-
return filemtime($this->resource) <= $timestamp;
65+
return file_exists($this->resource) && @filemtime($this->resource) <= $timestamp;
6466
}
6567

6668
public function serialize()

Tests/Resource/DirectoryResourceTest.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase
1919

2020
protected function setUp()
2121
{
22-
$this->directory = sys_get_temp_dir().'/symfonyDirectoryIterator';
22+
$this->directory = realpath(sys_get_temp_dir()).'/symfonyDirectoryIterator';
2323
if (!file_exists($this->directory)) {
2424
mkdir($this->directory);
2525
}
@@ -58,17 +58,31 @@ public function testGetResource()
5858

5959
public function testGetPattern()
6060
{
61-
$resource = new DirectoryResource('foo', 'bar');
61+
$resource = new DirectoryResource($this->directory, 'bar');
6262
$this->assertEquals('bar', $resource->getPattern());
6363
}
6464

65+
/**
66+
* @expectedException \InvalidArgumentException
67+
* @expectedExceptionMessageRegExp /The directory ".*" does not exist./
68+
*/
69+
public function testResourceDoesNotExist()
70+
{
71+
$resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
72+
}
73+
6574
public function testIsFresh()
6675
{
6776
$resource = new DirectoryResource($this->directory);
6877
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if the resource has not changed');
6978
$this->assertFalse($resource->isFresh(time() - 86400), '->isFresh() returns false if the resource has been updated');
79+
}
80+
81+
public function testIsFreshForDeletedResources()
82+
{
83+
$resource = new DirectoryResource($this->directory);
84+
$this->removeDirectory($this->directory);
7085

71-
$resource = new DirectoryResource('/____foo/foobar'.mt_rand(1, 999999));
7286
$this->assertFalse($resource->isFresh(time()), '->isFresh() returns false if the resource does not exist');
7387
}
7488

Tests/Resource/FileResourceTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ protected function setUp()
2929

3030
protected function tearDown()
3131
{
32+
if (!file_exists($this->file)) {
33+
return;
34+
}
35+
3236
unlink($this->file);
3337
}
3438

@@ -42,14 +46,27 @@ public function testToString()
4246
$this->assertSame(realpath($this->file), (string) $this->resource);
4347
}
4448

49+
/**
50+
* @expectedException \InvalidArgumentException
51+
* @expectedExceptionMessageRegExp /The file ".*" does not exist./
52+
*/
53+
public function testResourceDoesNotExist()
54+
{
55+
$resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999));
56+
}
57+
4558
public function testIsFresh()
4659
{
4760
$this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
4861
$this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
4962
$this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
63+
}
5064

51-
$resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999));
52-
$this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist');
65+
public function testIsFreshForDeletedResources()
66+
{
67+
unlink($this->file);
68+
69+
$this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist');
5370
}
5471

5572
public function testSerializeUnserialize()

0 commit comments

Comments
 (0)