FilesTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /***
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Utility;
  7. use Magento\Framework\App\Utility\Files;
  8. use Magento\Framework\Component\ComponentRegistrar;
  9. class FilesTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Framework\App\Utility\Files */
  12. protected $model;
  13. /** @var array */
  14. protected $moduleTests = [];
  15. /** @var array */
  16. protected $frameworkTests = [];
  17. /** @var array */
  18. protected $libTests = [];
  19. /** @var string */
  20. protected $rootTestsDir = '#dev/tests/#';
  21. /** @var string */
  22. protected $setupTestsDir = '#setup/src/Magento/Setup/Test#';
  23. public function setUp()
  24. {
  25. $componentRegistrar = new ComponentRegistrar();
  26. $dirSearch = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  27. ->create(\Magento\Framework\Component\DirSearch::class);
  28. $themePackageList = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  29. ->create(\Magento\Framework\View\Design\Theme\ThemePackageList::class);
  30. $this->model = new Files($componentRegistrar, $dirSearch, $themePackageList);
  31. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleDir) {
  32. $this->moduleTests[] = '#' . $moduleDir . '/Test#';
  33. }
  34. foreach ($componentRegistrar->getPaths(ComponentRegistrar::LIBRARY) as $libraryDir) {
  35. $this->libTests[] = '#' . $libraryDir . '/Test#';
  36. $this->frameworkTests[] = '#' . $libraryDir . '/[\\w]+/Test#';
  37. }
  38. }
  39. public function testGetPhpFilesExcludeTests()
  40. {
  41. $this->assertNoTestDirs(
  42. $this->model->getPhpFiles(
  43. Files::INCLUDE_APP_CODE
  44. | Files::INCLUDE_PUB_CODE
  45. | Files::INCLUDE_DEV_TOOLS
  46. | Files::INCLUDE_LIBS
  47. | Files::INCLUDE_TEMPLATES
  48. | Files::INCLUDE_NON_CLASSES
  49. )
  50. );
  51. }
  52. public function testGetComposerExcludeTests()
  53. {
  54. $this->assertNoTestDirs(
  55. $this->model->getComposerFiles(ComponentRegistrar::MODULE, false)
  56. );
  57. }
  58. public function testGetPhpFilesOnlyTests()
  59. {
  60. $classFiles = $this->model->getPhpFiles(Files::INCLUDE_TESTS);
  61. foreach ($this->moduleTests as $moduleTest) {
  62. $classFiles = preg_grep($moduleTest, $classFiles, PREG_GREP_INVERT);
  63. }
  64. foreach ($this->libTests as $libraryTest) {
  65. $classFiles = preg_grep($libraryTest, $classFiles, PREG_GREP_INVERT);
  66. }
  67. foreach ($this->frameworkTests as $frameworkTest) {
  68. $classFiles = preg_grep($frameworkTest, $classFiles, PREG_GREP_INVERT);
  69. }
  70. $classFiles = preg_grep($this->rootTestsDir, $classFiles, PREG_GREP_INVERT);
  71. $classFiles = preg_grep($this->setupTestsDir, $classFiles, PREG_GREP_INVERT);
  72. $this->assertEmpty($classFiles);
  73. }
  74. public function testGetConfigFiles()
  75. {
  76. $actual = $this->model->getConfigFiles('*.xml');
  77. $this->assertNotEmpty($actual);
  78. foreach ($actual as $file) {
  79. $this->assertStringEndsWith('.xml', $file[0]);
  80. }
  81. }
  82. public function testGetLayoutConfigFiles()
  83. {
  84. $actual = $this->model->getLayoutConfigFiles('*.xml');
  85. $this->assertNotEmpty($actual);
  86. foreach ($actual as $file) {
  87. $this->assertStringEndsWith('.xml', $file[0]);
  88. }
  89. }
  90. public function testGetXmlCatalogFiles()
  91. {
  92. $actual = $this->model->getXmlCatalogFiles('*.xml');
  93. $this->assertNotEmpty($actual);
  94. foreach ($actual as $file) {
  95. $this->assertStringEndsWith('.xml', $file[0]);
  96. }
  97. $actual = $this->model->getXmlCatalogFiles('*.xsd');
  98. $this->assertNotEmpty($actual);
  99. foreach ($actual as $file) {
  100. $this->assertStringEndsWith('.xsd', $file[0]);
  101. }
  102. }
  103. /**
  104. * Verify that the given array of files does not contain anything in test directories
  105. *
  106. * @param array $files
  107. */
  108. protected function assertNoTestDirs($files)
  109. {
  110. foreach ($this->moduleTests as $moduleTest) {
  111. $this->assertEmpty(preg_grep($moduleTest, $files));
  112. }
  113. foreach ($this->frameworkTests as $frameworkTest) {
  114. $this->assertEmpty(preg_grep($frameworkTest, $files));
  115. }
  116. foreach ($this->libTests as $libTest) {
  117. $this->assertEmpty(preg_grep($libTest, $files));
  118. }
  119. }
  120. /**
  121. * @magentoComponentsDir Magento/Framework/App/Utility/_files/fixtures
  122. */
  123. public function testReadLists()
  124. {
  125. $fixtureDir = str_replace('\\', '/', __DIR__) . '/_files/fixtures/';
  126. $expected = [
  127. $fixtureDir . 'language/One.php',
  128. $fixtureDir . 'language/registration.php',
  129. $fixtureDir . 'library/One.php',
  130. $fixtureDir . 'module/One.php',
  131. $fixtureDir . 'module/registration.php',
  132. $fixtureDir . 'theme/One.php',
  133. ];
  134. $actual = $this->model->readLists(__DIR__ . '/_files/patterns/paths*.txt');
  135. sort($actual);
  136. foreach ($actual as &$file) {
  137. $file = str_replace('\\', '/', $file);
  138. }
  139. $this->assertSame($expected, $actual);
  140. }
  141. }