AutogeneratedClassNotInConstructorTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Legacy;
  7. use Magento\Framework\App\Utility\Files;
  8. use Magento\TestFramework\Utility\ClassNameExtractor;
  9. use Magento\TestFramework\Utility\AutogeneratedClassNotInConstructorFinder;
  10. use Magento\TestFramework\Utility\ChangedFiles;
  11. class AutogeneratedClassNotInConstructorTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var string[]
  15. */
  16. private $autogeneratedClassesWhitelist;
  17. /**
  18. * @var ClassNameExtractor
  19. */
  20. private $classNameExtractor;
  21. /**
  22. * @var AutogeneratedClassNotInConstructorFinder
  23. */
  24. private $autogeneratedClassNotInConstructorFinder;
  25. /**
  26. * @var Files
  27. */
  28. private $fileUtilities;
  29. protected function setUp()
  30. {
  31. $this->classNameExtractor = new ClassNameExtractor();
  32. $this->autogeneratedClassNotInConstructorFinder = new AutogeneratedClassNotInConstructorFinder(
  33. $this->classNameExtractor
  34. );
  35. $this->fileUtilities = Files::init();
  36. }
  37. public function testAutogeneratedClassesRequestedInConstructor()
  38. {
  39. $fileTypes = Files::INCLUDE_APP_CODE | Files::INCLUDE_LIBS | Files::INCLUDE_SETUP | Files::AS_DATA_SET;
  40. $changedFiles = ChangedFiles::getPhpFiles(__DIR__ . '/_files/changed_files*', $fileTypes);
  41. $phpFiles = $this->fileUtilities->getPhpFiles($fileTypes);
  42. $existingClasses = [];
  43. $classesCreatedByObjectManager = [];
  44. foreach ($phpFiles as $file) {
  45. $filePath = $file[0];
  46. $fileContent = file_get_contents($filePath);
  47. $className = $this->classNameExtractor->getNameWithNamespace($fileContent);
  48. if ($className) {
  49. $existingClasses = array_merge(
  50. $existingClasses,
  51. [$filePath => $className]
  52. );
  53. }
  54. }
  55. foreach ($changedFiles as $file) {
  56. $filePath = $file[0];
  57. $fileContent = file_get_contents($filePath);
  58. $className = $this->classNameExtractor->getNameWithNamespace($fileContent);
  59. if ($className) {
  60. $tmpClassesCreatedByObjectManager = array_diff(
  61. $this->autogeneratedClassNotInConstructorFinder->find($fileContent),
  62. $this->getWhitelistedClasses()
  63. );
  64. if (!empty($tmpClassesCreatedByObjectManager)) {
  65. $classesCreatedByObjectManager = array_merge(
  66. $classesCreatedByObjectManager,
  67. [$filePath => $tmpClassesCreatedByObjectManager]
  68. );
  69. }
  70. }
  71. }
  72. $existingClasses = array_unique($existingClasses);
  73. $generatedDependenciesNotInConstructor = [];
  74. foreach ($classesCreatedByObjectManager as $key => $classes) {
  75. $autogeneratedClasses = array_diff($classes, $existingClasses);
  76. if (!empty($autogeneratedClasses)) {
  77. $generatedDependenciesNotInConstructor[$key] = $autogeneratedClasses;
  78. }
  79. }
  80. $this->assertEmpty(
  81. $generatedDependenciesNotInConstructor,
  82. "The following autogenerated classes need to be requested in constructor, otherwise compiler "
  83. . "will not be able to find and generate these classes \r\n"
  84. . print_r($generatedDependenciesNotInConstructor, true)
  85. );
  86. }
  87. /**
  88. * Get whitelisted classes
  89. *
  90. * @return string[]
  91. */
  92. private function getWhitelistedClasses()
  93. {
  94. if (!$this->autogeneratedClassesWhitelist) {
  95. $this->autogeneratedClassesWhitelist = require_once __DIR__
  96. . '/_files/autogenerated_class_not_in_constructor_whitelist.php';
  97. }
  98. return $this->autogeneratedClassesWhitelist;
  99. }
  100. }