FileLoaderTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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\Loader;
  11. use Symfony\Component\Config\Loader\FileLoader;
  12. use Symfony\Component\Config\Loader\LoaderResolver;
  13. use Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException;
  14. class FileLoaderTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @covers Symfony\Component\Config\Loader\FileLoader
  18. */
  19. public function testImport()
  20. {
  21. $locatorMock = $this->getMock('Symfony\Component\Config\FileLocatorInterface');
  22. $locatorMock->expects($this->any())->method('locate')->will($this->onConsecutiveCalls(
  23. array('path/to/file1'), // Default
  24. array('path/to/file1', 'path/to/file2'), // First is imported
  25. array('path/to/file1', 'path/to/file2'), // Second is imported
  26. array('path/to/file1'), // Exception
  27. array('path/to/file1', 'path/to/file2') // Exception
  28. ));
  29. $fileLoader = new TestFileLoader($locatorMock);
  30. $fileLoader->setCurrentDir('.');
  31. $fileLoader->setResolver($loaderResolver = new LoaderResolver(array($fileLoader)));
  32. // Default case
  33. $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
  34. // Check first file is imported if not already loading
  35. $this->assertSame('path/to/file1', $fileLoader->import('my_resource'));
  36. // Check second file is imported if first is already loading
  37. $fileLoader->addLoading('path/to/file1');
  38. $this->assertSame('path/to/file2', $fileLoader->import('my_resource'));
  39. // Check exception throws if first (and only available) file is already loading
  40. try {
  41. $fileLoader->import('my_resource');
  42. $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  43. } catch (\Exception $e) {
  44. $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  45. }
  46. // Check exception throws if all files are already loading
  47. try {
  48. $fileLoader->addLoading('path/to/file2');
  49. $fileLoader->import('my_resource');
  50. $this->fail('->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  51. } catch (\Exception $e) {
  52. $this->assertInstanceOf('Symfony\Component\Config\Exception\FileLoaderImportCircularReferenceException', $e, '->import() throws a FileLoaderImportCircularReferenceException if the resource is already loading');
  53. }
  54. }
  55. }
  56. class TestFileLoader extends FileLoader
  57. {
  58. public function load($resource, $type = null)
  59. {
  60. return $resource;
  61. }
  62. public function supports($resource, $type = null)
  63. {
  64. return true;
  65. }
  66. public function addLoading($resource)
  67. {
  68. self::$loading[$resource] = true;
  69. }
  70. public function removeLoading($resource)
  71. {
  72. unset(self::$loading[$resource]);
  73. }
  74. public function clearLoading()
  75. {
  76. self::$loading = array();
  77. }
  78. }