FileLocatorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Tests\Component\Config;
  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. );
  33. }
  34. public function testLocate()
  35. {
  36. $loader = new FileLocator(__DIR__.'/Fixtures');
  37. $this->assertEquals(
  38. __DIR__.DIRECTORY_SEPARATOR.'FileLocatorTest.php',
  39. $loader->locate('FileLocatorTest.php', __DIR__),
  40. '->locate() returns the absolute filename if the file exists in the given path'
  41. );
  42. $this->assertEquals(
  43. __DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml',
  44. $loader->locate('foo.xml', __DIR__),
  45. '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
  46. );
  47. $this->assertEquals(
  48. __DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml',
  49. $loader->locate(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__),
  50. '->locate() returns the absolute filename if the file exists in one of the paths given in the constructor'
  51. );
  52. $loader = new FileLocator(array(__DIR__.'/Fixtures', __DIR__.'/Fixtures/Again'));
  53. $this->assertEquals(
  54. array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
  55. $loader->locate('foo.xml', __DIR__, false),
  56. '->locate() returns an array of absolute filenames'
  57. );
  58. $this->assertEquals(
  59. array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
  60. $loader->locate('foo.xml', __DIR__.'/Fixtures', false),
  61. '->locate() returns an array of absolute filenames'
  62. );
  63. $loader = new FileLocator(__DIR__.'/Fixtures/Again');
  64. $this->assertEquals(
  65. array(__DIR__.'/Fixtures'.DIRECTORY_SEPARATOR.'foo.xml', __DIR__.'/Fixtures/Again'.DIRECTORY_SEPARATOR.'foo.xml'),
  66. $loader->locate('foo.xml', __DIR__.'/Fixtures', false),
  67. '->locate() returns an array of absolute filenames'
  68. );
  69. }
  70. /**
  71. * @expectedException \InvalidArgumentException
  72. */
  73. public function testLocateThrowsAnExceptionIfTheFileDoesNotExists()
  74. {
  75. $loader = new FileLocator(array(__DIR__.'/Fixtures'));
  76. $loader->locate('foobar.xml', __DIR__);
  77. }
  78. /**
  79. * @expectedException \InvalidArgumentException
  80. */
  81. public function testLocateThrowsAnExceptionIfTheFileDoesNotExistsInAbsolutePath()
  82. {
  83. $loader = new FileLocator(array(__DIR__.'/Fixtures'));
  84. $loader->locate(__DIR__.'/Fixtures/foobar.xml', __DIR__);
  85. }
  86. }