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