2011-02-10 15:15:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-10 15:15:51 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\Config\Tests;
|
2011-02-10 15:15:51 +00:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2011-02-10 15:15:51 +00:00
|
|
|
use Symfony\Component\Config\FileLocator;
|
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class FileLocatorTest extends TestCase
|
2011-02-10 15:15:51 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider getIsAbsolutePathTests
|
|
|
|
*/
|
|
|
|
public function testIsAbsolutePath($path)
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
$loader = new FileLocator([]);
|
2011-02-10 15:15:51 +00:00
|
|
|
$r = new \ReflectionObject($loader);
|
|
|
|
$m = $r->getMethod('isAbsolutePath');
|
|
|
|
$m->setAccessible(true);
|
|
|
|
|
|
|
|
$this->assertTrue($m->invoke($loader, $path), '->isAbsolutePath() returns true for an absolute path');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIsAbsolutePathTests()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [
|
|
|
|
['/foo.xml'],
|
|
|
|
['c:\\\\foo.xml'],
|
|
|
|
['c:/foo.xml'],
|
|
|
|
['\\server\\foo.xml'],
|
|
|
|
['https://server/foo.xml'],
|
|
|
|
['phar://server/foo.xml'],
|
|
|
|
];
|
2011-02-10 15:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLocate()
|
|
|
|
{
|
2011-02-14 15:29:31 +00:00
|
|
|
$loader = new FileLocator(__DIR__.'/Fixtures');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2018-07-26 12:13:39 +01:00
|
|
|
__DIR__.\DIRECTORY_SEPARATOR.'FileLocatorTest.php',
|
2011-02-14 15:29:31 +00:00
|
|
|
$loader->locate('FileLocatorTest.php', __DIR__),
|
|
|
|
'->locate() returns the absolute filename if the file exists in the given path'
|
|
|
|
);
|
2011-02-10 15:15:51 +00:00
|
|
|
|
2011-02-14 15:29:31 +00:00
|
|
|
$this->assertEquals(
|
2018-07-26 12:13:39 +01:00
|
|
|
__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml',
|
2011-02-14 15:29:31 +00:00
|
|
|
$loader->locate('foo.xml', __DIR__),
|
|
|
|
'->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
|
|
|
|
);
|
|
|
|
|
2011-03-05 14:24:44 +00:00
|
|
|
$this->assertEquals(
|
2018-07-26 12:13:39 +01:00
|
|
|
__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml',
|
|
|
|
$loader->locate(__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__),
|
2011-03-05 14:24:44 +00:00
|
|
|
'->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
|
|
|
|
);
|
|
|
|
|
2019-01-16 09:39:14 +00:00
|
|
|
$loader = new FileLocator([__DIR__.'/Fixtures', __DIR__.'/Fixtures/Again']);
|
2011-02-10 15:15:51 +00:00
|
|
|
|
2011-02-14 15:29:31 +00:00
|
|
|
$this->assertEquals(
|
2019-01-16 09:39:14 +00:00
|
|
|
[__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.\DIRECTORY_SEPARATOR.'foo.xml'],
|
2011-02-14 15:29:31 +00:00
|
|
|
$loader->locate('foo.xml', __DIR__, false),
|
|
|
|
'->locate() returns an array of absolute filenames'
|
|
|
|
);
|
2011-03-05 14:24:44 +00:00
|
|
|
|
|
|
|
$this->assertEquals(
|
2019-01-16 09:39:14 +00:00
|
|
|
[__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.\DIRECTORY_SEPARATOR.'foo.xml'],
|
2011-03-05 14:24:44 +00:00
|
|
|
$loader->locate('foo.xml', __DIR__.'/Fixtures', false),
|
|
|
|
'->locate() returns an array of absolute filenames'
|
|
|
|
);
|
|
|
|
|
|
|
|
$loader = new FileLocator(__DIR__.'/Fixtures/Again');
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2019-01-16 09:39:14 +00:00
|
|
|
[__DIR__.'/Fixtures'.\DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.\DIRECTORY_SEPARATOR.'foo.xml'],
|
2011-03-05 14:24:44 +00:00
|
|
|
$loader->locate('foo.xml', __DIR__.'/Fixtures', false),
|
|
|
|
'->locate() returns an array of absolute filenames'
|
|
|
|
);
|
2011-02-10 15:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
|
|
|
|
{
|
2019-08-01 23:48:42 +01:00
|
|
|
$this->expectException('Symfony\Component\Config\Exception\FileLocatorFileNotFoundException');
|
|
|
|
$this->expectExceptionMessage('The file "foobar.xml" does not exist');
|
2019-01-16 09:39:14 +00:00
|
|
|
$loader = new FileLocator([__DIR__.'/Fixtures']);
|
2011-02-10 15:15:51 +00:00
|
|
|
|
|
|
|
$loader->locate('foobar.xml', __DIR__);
|
|
|
|
}
|
2011-03-05 14:24:44 +00:00
|
|
|
|
|
|
|
public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath()
|
|
|
|
{
|
2019-08-01 23:48:42 +01:00
|
|
|
$this->expectException('Symfony\Component\Config\Exception\FileLocatorFileNotFoundException');
|
2019-01-16 09:39:14 +00:00
|
|
|
$loader = new FileLocator([__DIR__.'/Fixtures']);
|
2011-03-05 14:24:44 +00:00
|
|
|
|
|
|
|
$loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__);
|
|
|
|
}
|
2014-03-14 21:17:30 +00:00
|
|
|
|
|
|
|
public function testLocateEmpty()
|
|
|
|
{
|
2019-08-01 23:48:42 +01:00
|
|
|
$this->expectException('InvalidArgumentException');
|
|
|
|
$this->expectExceptionMessage('An empty file name is not valid to be located.');
|
2019-01-16 09:39:14 +00:00
|
|
|
$loader = new FileLocator([__DIR__.'/Fixtures']);
|
2014-03-14 21:17:30 +00:00
|
|
|
|
|
|
|
$loader->locate(null, __DIR__);
|
|
|
|
}
|
2011-02-10 15:15:51 +00:00
|
|
|
}
|