FileResolverTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit\Config;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. class FileResolverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * Files resolver
  12. *
  13. * @var \Magento\Framework\App\Config\FileResolver
  14. */
  15. protected $model;
  16. /**
  17. * Filesystem
  18. *
  19. * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $filesystem;
  22. /**
  23. * File iterator factory
  24. *
  25. * @var \Magento\Framework\Config\FileIteratorFactory|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. protected $iteratorFactory;
  28. /**
  29. * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. protected $moduleReader;
  32. protected function setUp()
  33. {
  34. $this->iteratorFactory = $this->getMockBuilder(\Magento\Framework\Config\FileIteratorFactory::class)
  35. ->disableOriginalConstructor()
  36. ->setConstructorArgs(['getPath'])
  37. ->getMock();
  38. $this->filesystem = $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryRead']);
  39. $this->moduleReader = $this->getMockBuilder(\Magento\Framework\Module\Dir\Reader::class)
  40. ->disableOriginalConstructor()
  41. ->setConstructorArgs(['getConfigurationFiles'])
  42. ->getMock();
  43. $this->model = new \Magento\Framework\App\Config\FileResolver(
  44. $this->moduleReader,
  45. $this->filesystem,
  46. $this->iteratorFactory
  47. );
  48. }
  49. /**
  50. * Test for get method with primary scope
  51. *
  52. * @dataProvider providerGet
  53. * @param string $filename
  54. * @param array $fileList
  55. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  56. */
  57. public function testGetPrimary($filename, $fileList)
  58. {
  59. $scope = 'primary';
  60. $directory = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
  61. $directory->expects(
  62. $this->once()
  63. )->method(
  64. 'search'
  65. )->with(
  66. sprintf('{%1$s,*/%1$s}', $filename)
  67. )->will(
  68. $this->returnValue($fileList)
  69. );
  70. $i = 1;
  71. foreach ($fileList as $file) {
  72. $directory->expects($this->at($i++))->method('getAbsolutePath')->willReturn($file);
  73. }
  74. $this->filesystem->expects(
  75. $this->once()
  76. )->method(
  77. 'getDirectoryRead'
  78. )->with(
  79. DirectoryList::CONFIG
  80. )->will(
  81. $this->returnValue($directory)
  82. );
  83. $this->iteratorFactory->expects(
  84. $this->once()
  85. )->method(
  86. 'create'
  87. )->with(
  88. $fileList
  89. )->will(
  90. $this->returnValue(true)
  91. );
  92. $this->assertTrue($this->model->get($filename, $scope));
  93. }
  94. /**
  95. * Test for get method with global scope
  96. *
  97. * @dataProvider providerGet
  98. * @param string $filename
  99. * @param array $fileList
  100. */
  101. public function testGetGlobal($filename, $fileList)
  102. {
  103. $scope = 'global';
  104. $this->moduleReader->expects(
  105. $this->once()
  106. )->method(
  107. 'getConfigurationFiles'
  108. )->with(
  109. $filename
  110. )->will(
  111. $this->returnValue($fileList)
  112. );
  113. $this->assertEquals($fileList, $this->model->get($filename, $scope));
  114. }
  115. /**
  116. * Test for get method with default scope
  117. *
  118. * @dataProvider providerGet
  119. * @param string $filename
  120. * @param array $fileList
  121. */
  122. public function testGetDefault($filename, $fileList)
  123. {
  124. $scope = 'some_scope';
  125. $this->moduleReader->expects(
  126. $this->once()
  127. )->method(
  128. 'getConfigurationFiles'
  129. )->with(
  130. $scope . '/' . $filename
  131. )->will(
  132. $this->returnValue($fileList)
  133. );
  134. $this->assertEquals($fileList, $this->model->get($filename, $scope));
  135. }
  136. /**
  137. * Data provider for get tests
  138. *
  139. * @return array
  140. */
  141. public function providerGet()
  142. {
  143. return [
  144. ['di.xml', ['di.xml', 'anotherfolder/di.xml']],
  145. ['no_files.xml', []],
  146. ['one_file.xml', ['one_file.xml']]
  147. ];
  148. }
  149. }