Skip to content

Commit

Permalink
Fix that two DirectoryResources with different patterns would be dedu…
Browse files Browse the repository at this point in the history
…plicated

ResourceInterface::__toString is mainly important because in various places, array_uniqe() is called to perform a de-duplication of resources and will use the string representation for objects.

Thus, we need to take care that if DirectoryResources apply different patterns they must be kept after array_unique calls.
  • Loading branch information
mpdude authored and fabpot committed Sep 10, 2015
1 parent cfd8cc2 commit 2b36ac5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Expand Up @@ -38,7 +38,7 @@ public function __construct($resource, $pattern = null)
*/
public function __toString()
{
return (string) $this->resource;
return md5(serialize(array($this->resource, $this->pattern)));
}

/**
Expand Down
Expand Up @@ -54,7 +54,6 @@ public function testGetResource()
{
$resource = new DirectoryResource($this->directory);
$this->assertSame($this->directory, $resource->getResource(), '->getResource() returns the path to the resource');
$this->assertSame($this->directory, (string) $resource, '->__toString() returns the path to the resource');
}

public function testGetPattern()
Expand Down Expand Up @@ -87,6 +86,13 @@ public function testIsFreshNewFile()
$this->assertFalse($resource->isFresh(time() + 10), '->isFresh() returns false if a new file is added');
}

public function testIsFreshNewFileWithDifferentPattern()
{
$resource = new DirectoryResource($this->directory, '/.xml$/');
touch($this->directory.'/new.yaml', time() + 20);
$this->assertTrue($resource->isFresh(time() + 10), '->isFresh() returns true if a new file with a non-matching pattern is added');
}

public function testIsFreshDeleteFile()
{
$resource = new DirectoryResource($this->directory);
Expand Down Expand Up @@ -149,4 +155,12 @@ public function testSerializeUnserialize()
$this->assertSame($this->directory, $resource->getResource());
$this->assertSame('/\.(foo|xml)$/', $resource->getPattern());
}

public function testResourcesWithDifferentPatternsAreDifferent()
{
$resourceA = new DirectoryResource($this->directory, '/.xml$/');
$resourceB = new DirectoryResource($this->directory, '/.yaml$/');

$this->assertEquals(2, count(array_unique(array($resourceA, $resourceB))));
}
}

0 comments on commit 2b36ac5

Please sign in to comment.