FileLocatorTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Symfony\Component\Config\FileLocator;
  12. class FileLocatorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider getIsAbsolutePathTests
  16. */
  17. public function testIsAbsolutePath($path)
  18. {
  19. $loader = new FileLocator(array());
  20. $r = new \ReflectionObject($loader);
  21. $m = $r->getMethod('isAbsolutePath');
  22. $m->setAccessible(true);
  23. $this->assertTrue($m->invoke($loader, $path), '->isAbsolutePath() returns true for an absolute path');
  24. }
  25. public function getIsAbsolutePathTests()
  26. {
  27. return array(
  28. array('/foo.xml'),
  29. array('c:\\\\foo.xml'),
  30. array('c:/foo.xml'),
  31. array('\\server\\foo.xml'),
  32. array('https://server/foo.xml'),
  33. array('phar://server/foo.xml'),
  34. );
  35. }
  36. public function testLocate()
  37. {
  38. $loader = new FileLocator(__DIR__.'/Fixtures');
  39. $this->assertEquals(
  40. __DIR__.DIRECTORY_SEPARATOR.'FileLocatorTest.php',
  41. $loader->locate('FileLocatorTest.php', __DIR__),
  42. '->locate() returns the absolute filename if the file exists in the given path'
  43. );
  44. $this->assertEquals(
  45. __DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml',
  46. $loader->locate('foo.xml', __DIR__),
  47. '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
  48. );
  49. $this->assertEquals(
  50. __DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml',
  51. $loader->locate(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__),
  52. '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
  53. );
  54. $loader = new FileLocator(array(__DIR__.'/Fixtures', __DIR__.'/Fixtures/Again'));
  55. $this->assertEquals(
  56. array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
  57. $loader->locate('foo.xml', __DIR__, false),
  58. '->locate() returns an array of absolute filenames'
  59. );
  60. $this->assertEquals(
  61. array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
  62. $loader->locate('foo.xml', __DIR__.'/Fixtures', false),
  63. '->locate() returns an array of absolute filenames'
  64. );
  65. $loader = new FileLocator(__DIR__.'/Fixtures/Again');
  66. $this->assertEquals(
  67. array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
  68. $loader->locate('foo.xml', __DIR__.'/Fixtures', false),
  69. '->locate() returns an array of absolute filenames'
  70. );
  71. }
  72. /**
  73. * @expectedException \InvalidArgumentException
  74. */
  75. public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
  76. {
  77. $loader = new FileLocator(array(__DIR__.'/Fixtures'));
  78. $loader->locate('foobar.xml', __DIR__);
  79. }
  80. /**
  81. * @expectedException \InvalidArgumentException
  82. */
  83. public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath()
  84. {
  85. $loader = new FileLocator(array(__DIR__.'/Fixtures'));
  86. $loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__);
  87. }
  88. }