From 2b36ac5a614c9319ac78870c3beefdd8f1e92bdc Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Mon, 7 Sep 2015 20:26:20 +0200 Subject: [PATCH] Fix that two DirectoryResources with different patterns would be deduplicated 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. --- .../Config/Resource/DirectoryResource.php | 2 +- .../Tests/Resource/DirectoryResourceTest.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Config/Resource/DirectoryResource.php b/src/Symfony/Component/Config/Resource/DirectoryResource.php index 515fb5c42d..7ae5694ff0 100644 --- a/src/Symfony/Component/Config/Resource/DirectoryResource.php +++ b/src/Symfony/Component/Config/Resource/DirectoryResource.php @@ -38,7 +38,7 @@ class DirectoryResource implements ResourceInterface, \Serializable */ public function __toString() { - return (string) $this->resource; + return md5(serialize(array($this->resource, $this->pattern))); } /** diff --git a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php index 226e2807dc..2bbaadc3a7 100644 --- a/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php +++ b/src/Symfony/Component/Config/Tests/Resource/DirectoryResourceTest.php @@ -54,7 +54,6 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase { $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() @@ -87,6 +86,13 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase $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); @@ -149,4 +155,12 @@ class DirectoryResourceTest extends \PHPUnit_Framework_TestCase $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)))); + } }