Sfoglia il codice sorgente

[Config] Added missing tests to satisfy 100% test coverage of FileLocator. Made sure that every path returned with locate() is unique.

Jakub Zalas 14 anni fa
parent
commit
bdd4194b6f

+ 1 - 1
src/Symfony/Component/Config/FileLocator.php

@@ -75,7 +75,7 @@ class FileLocator implements FileLocatorInterface
             throw new \InvalidArgumentException(sprintf('The file "%s" does not exist (in: %s%s).', $name, null !== $currentPath ? $currentPath.', ' : '', implode(', ', $this->paths)));
         }
 
-        return $filepaths;
+        return array_values(array_unique($filepaths));
     }
 
     /**

+ 30 - 0
tests/Symfony/Tests/Component/Config/FileLocatorTest.php

@@ -54,6 +54,12 @@ class FileLocatorTest extends \PHPUnit_Framework_TestCase
             '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
         );
 
+        $this->assertEquals(
+            __DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml',
+            $loader->locate(__DIR__.'/Fixtures/foo.xml', __DIR__),
+            '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
+        );
+
         $loader = new FileLocator(array(__DIR__.'/Fixtures', __DIR__.'/Fixtures/Again'));
 
         $this->assertEquals(
@@ -61,6 +67,20 @@ class FileLocatorTest extends \PHPUnit_Framework_TestCase
             $loader->locate('foo.xml', __DIR__, false),
             '->locate() returns an array of absolute filenames'
         );
+
+        $this->assertEquals(
+            array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
+            $loader->locate('foo.xml', __DIR__.'/Fixtures', false),
+            '->locate() returns an array of absolute filenames'
+        );
+
+        $loader = new FileLocator(__DIR__.'/Fixtures/Again');
+
+        $this->assertEquals(
+            array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
+            $loader->locate('foo.xml', __DIR__.'/Fixtures', false),
+            '->locate() returns an array of absolute filenames'
+        );
     }
 
     /**
@@ -72,4 +92,14 @@ class FileLocatorTest extends \PHPUnit_Framework_TestCase
 
         $loader->locate('foobar.xml', __DIR__);
     }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath()
+    {
+        $loader = new FileLocator(array(__DIR__.'/Fixtures'));
+
+        $loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__);
+    }
 }