FileLocatorTest.php 4.0 KB

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