ObsoleteResponseTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Component\ComponentRegistrar;
  8. /**
  9. * Temporary test that will be removed in scope of MAGETWO-28356.
  10. * Test verifies obsolete usages in modules that were refactored to work with ResultInterface.
  11. */
  12. class ObsoleteResponseTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $obsoleteMethods = [];
  18. /**
  19. * @var array
  20. */
  21. protected $filesBlackList = [];
  22. /**
  23. * @var string
  24. */
  25. protected $appPath;
  26. protected function setUp()
  27. {
  28. $this->obsoleteMethods = include __DIR__ . '/_files/response/obsolete_response_methods.php';
  29. $this->filesBlackList = $this->getBlackList();
  30. }
  31. /**
  32. * Test verify that obsolete methods do not appear in refactored folders
  33. */
  34. public function testObsoleteResponseMethods()
  35. {
  36. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  37. $invoker(
  38. function ($file) {
  39. $content = file_get_contents($file);
  40. foreach ($this->obsoleteMethods as $method) {
  41. $quotedMethod = preg_quote($method, '/');
  42. $this->assertSame(
  43. 0,
  44. preg_match('/(?<=[a-z\\d_:]|->|function\\s)' . $quotedMethod . '\\s*\\(/iS', $content),
  45. "File: $file\nContains obsolete method: $method . "
  46. );
  47. }
  48. },
  49. $this->modulesFilesDataProvider()
  50. );
  51. }
  52. /**
  53. * Return refactored files
  54. *
  55. * @return array
  56. */
  57. public function modulesFilesDataProvider()
  58. {
  59. $filesList = [];
  60. $componentRegistrar = new ComponentRegistrar();
  61. foreach ($this->getFilesData('whitelist/refactored_modules*') as $refactoredModule) {
  62. if ($componentRegistrar->getPath(ComponentRegistrar::MODULE, $refactoredModule)) {
  63. $files = \Magento\Framework\App\Utility\Files::init()->getFiles(
  64. [$componentRegistrar->getPath(ComponentRegistrar::MODULE, $refactoredModule)],
  65. '*.php'
  66. );
  67. $filesList = array_merge($filesList, $files);
  68. }
  69. }
  70. $result = array_map('realpath', $filesList);
  71. $result = array_diff($result, $this->filesBlackList);
  72. return \Magento\Framework\App\Utility\Files::composeDataSets($result);
  73. }
  74. /**
  75. * @return array
  76. */
  77. protected function getBlackList()
  78. {
  79. $blackListFiles = [];
  80. $componentRegistrar = new ComponentRegistrar();
  81. foreach ($this->getFilesData('blacklist/files_list*') as $fileInfo) {
  82. $blackListFiles[] = $componentRegistrar->getPath(ComponentRegistrar::MODULE, $fileInfo[0])
  83. . DIRECTORY_SEPARATOR . $fileInfo[1];
  84. }
  85. return $blackListFiles;
  86. }
  87. /**
  88. * @param string $filePattern
  89. * @return array
  90. */
  91. protected function getFilesData($filePattern)
  92. {
  93. $result = [];
  94. foreach (glob(__DIR__ . '/_files/response/' . $filePattern) as $file) {
  95. $fileData = include $file;
  96. $result = array_merge($result, $fileData);
  97. }
  98. return $result;
  99. }
  100. }