1<?php 2 3/* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Symfony\Component\Config\Tests; 13 14use PHPUnit\Framework\TestCase; 15use Symfony\Component\Config\FileLocator; 16 17class FileLocatorTest extends TestCase 18{ 19 /** 20 * @dataProvider getIsAbsolutePathTests 21 */ 22 public function testIsAbsolutePath($path) 23 { 24 $loader = new FileLocator([]); 25 $r = new \ReflectionObject($loader); 26 $m = $r->getMethod('isAbsolutePath'); 27 $m->setAccessible(true); 28 29 $this->assertTrue($m->invoke($loader, $path), '->isAbsolutePath() returns true for an absolute path'); 30 } 31 32 public function getIsAbsolutePathTests() 33 { 34 return [ 35 ['/foo.xml'], 36 ['c:\\\\foo.xml'], 37 ['c:/foo.xml'], 38 ['\\server\\foo.xml'], 39 ['https://server/foo.xml'], 40 ['phar://server/foo.xml'], 41 ]; 42 } 43 44 public function testLocate() 45 { 46 $loader = new FileLocator(__DIR__.'/Fixtures'); 47 48 $this->assertEquals( 49 __DIR__.\DIRECTORY_SEPARATOR.'FileLocatorTest.php', 50 $loader->locate('FileLocatorTest.php', __DIR__), 51 '->locate() returns the absolute filename if the file exists in the given path' 52 ); 53 54 $this->assertEquals( 55 __DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', 56 $loader->locate('foo.xml', __DIR__), 57 '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor' 58 ); 59 60 $this->assertEquals( 61 __DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', 62 $loader->locate(__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__), 63 '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor' 64 ); 65 66 $loader = new FileLocator([__DIR__.'/Fixtures', __DIR__.'/Fixtures/Again']); 67 68 $this->assertEquals( 69 [__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.\DIRECTORY_SEPARATOR.'foo.xml'], 70 $loader->locate('foo.xml', __DIR__, false), 71 '->locate() returns an array of absolute filenames' 72 ); 73 74 $this->assertEquals( 75 [__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.\DIRECTORY_SEPARATOR.'foo.xml'], 76 $loader->locate('foo.xml', __DIR__.'/Fixtures', false), 77 '->locate() returns an array of absolute filenames' 78 ); 79 80 $loader = new FileLocator(__DIR__.'/Fixtures/Again'); 81 82 $this->assertEquals( 83 [__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.\DIRECTORY_SEPARATOR.'foo.xml'], 84 $loader->locate('foo.xml', __DIR__.'/Fixtures', false), 85 '->locate() returns an array of absolute filenames' 86 ); 87 } 88 89 public function testLocateThrowsAnExceptionIfTheFileDoesNotExists() 90 { 91 $this->expectException('Symfony\Component\Config\Exception\FileLocatorFileNotFoundException'); 92 $this->expectExceptionMessage('The file "foobar.xml" does not exist'); 93 $loader = new FileLocator([__DIR__.'/Fixtures']); 94 95 $loader->locate('foobar.xml', __DIR__); 96 } 97 98 public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath() 99 { 100 $this->expectException('Symfony\Component\Config\Exception\FileLocatorFileNotFoundException'); 101 $loader = new FileLocator([__DIR__.'/Fixtures']); 102 103 $loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__); 104 } 105 106 public function testLocateEmpty() 107 { 108 $this->expectException('InvalidArgumentException'); 109 $this->expectExceptionMessage('An empty file name is not valid to be located.'); 110 $loader = new FileLocator([__DIR__.'/Fixtures']); 111 112 $loader->locate(null, __DIR__); 113 } 114} 115