|
5 | 5 | */ |
6 | 6 | namespace Magento\Framework\Error; |
7 | 7 |
|
| 8 | +use Magento\TestFramework\Helper\Bootstrap; |
| 9 | + |
8 | 10 | require_once __DIR__ . '/../../../../../../../pub/errors/processor.php'; |
9 | 11 |
|
10 | 12 | class ProcessorTest extends \PHPUnit\Framework\TestCase |
11 | 13 | { |
12 | | - /** @var \Magento\Framework\Error\Processor */ |
| 14 | + /** |
| 15 | + * @var Processor |
| 16 | + */ |
13 | 17 | private $processor; |
14 | 18 |
|
15 | | - public function setUp() |
| 19 | + /** |
| 20 | + * @inheritdoc |
| 21 | + */ |
| 22 | + protected function setUp() |
16 | 23 | { |
17 | 24 | $this->processor = $this->createProcessor(); |
18 | 25 | } |
19 | 26 |
|
20 | | - public function tearDown() |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + * @throws \Exception |
| 30 | + */ |
| 31 | + protected function tearDown() |
21 | 32 | { |
22 | | - if ($this->processor->reportId) { |
23 | | - unlink($this->processor->_reportDir . '/' . $this->processor->reportId); |
24 | | - } |
| 33 | + $reportDir = $this->processor->_reportDir; |
| 34 | + $this->removeDirRecursively($reportDir); |
25 | 35 | } |
26 | 36 |
|
27 | | - public function testSaveAndLoadReport() |
28 | | - { |
| 37 | + /** |
| 38 | + * @param int $logReportDirNestingLevel |
| 39 | + * @param int $logReportDirNestingLevelChanged |
| 40 | + * @param string $exceptionMessage |
| 41 | + * @dataProvider dataProviderSaveAndLoadReport |
| 42 | + */ |
| 43 | + public function testSaveAndLoadReport( |
| 44 | + int $logReportDirNestingLevel, |
| 45 | + int $logReportDirNestingLevelChanged, |
| 46 | + string $exceptionMessage |
| 47 | + ) { |
| 48 | + $_ENV['MAGE_ERROR_REPORT_DIR_NESTING_LEVEL'] = $logReportDirNestingLevel; |
29 | 49 | $reportData = [ |
30 | | - 0 => 'exceptionMessage', |
| 50 | + 0 => $exceptionMessage, |
31 | 51 | 1 => 'exceptionTrace', |
32 | 52 | 'script_name' => 'processor.php' |
33 | 53 | ]; |
| 54 | + $reportData['report_id'] = hash('sha256', implode('', $reportData)); |
34 | 55 | $expectedReportData = array_merge($reportData, ['url' => '']); |
35 | | - $this->processor = $this->createProcessor(); |
36 | | - $this->processor->saveReport($reportData); |
37 | | - if (!$this->processor->reportId) { |
| 56 | + $processor = $this->createProcessor(); |
| 57 | + $processor->saveReport($reportData); |
| 58 | + $reportId = $processor->reportId; |
| 59 | + if (!$reportId) { |
38 | 60 | $this->fail("Failed to generate report id"); |
39 | 61 | } |
40 | | - $this->assertFileExists($this->processor->_reportDir . '/' . $this->processor->reportId); |
41 | | - $this->assertEquals($expectedReportData, $this->processor->reportData); |
| 62 | + $this->assertEquals($expectedReportData, $processor->reportData); |
| 63 | + $_ENV['MAGE_ERROR_REPORT_DIR_NESTING_LEVEL'] = $logReportDirNestingLevelChanged; |
| 64 | + $processor = $this->createProcessor(); |
| 65 | + $processor->loadReport($reportId); |
| 66 | + $this->assertEquals($expectedReportData, $processor->reportData, "File contents of report don't match"); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Data Provider for testSaveAndLoadReport |
| 71 | + * |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + public function dataProviderSaveAndLoadReport(): array |
| 75 | + { |
| 76 | + return [ |
| 77 | + [ |
| 78 | + 'logReportDirNestingLevel' => 0, |
| 79 | + 'logReportDirNestingLevelChanged' => 0, |
| 80 | + 'exceptionMessage' => '$exceptionMessage 0', |
| 81 | + ], |
| 82 | + [ |
| 83 | + 'logReportDirNestingLevel' => 1, |
| 84 | + 'logReportDirNestingLevelChanged' => 1, |
| 85 | + 'exceptionMessage' => '$exceptionMessage 1', |
| 86 | + ], |
| 87 | + [ |
| 88 | + 'logReportDirNestingLevel' => 2, |
| 89 | + 'logReportDirNestingLevelChanged' => 2, |
| 90 | + 'exceptionMessage' => '$exceptionMessage 2', |
| 91 | + ], |
| 92 | + [ |
| 93 | + 'logReportDirNestingLevel' => 3, |
| 94 | + 'logReportDirNestingLevelChanged' => 23, |
| 95 | + 'exceptionMessage' => '$exceptionMessage 2', |
| 96 | + ], |
| 97 | + [ |
| 98 | + 'logReportDirNestingLevel' => 32, |
| 99 | + 'logReportDirNestingLevelChanged' => 32, |
| 100 | + 'exceptionMessage' => '$exceptionMessage 3', |
| 101 | + ], |
| 102 | + [ |
| 103 | + 'logReportDirNestingLevel' => 100, |
| 104 | + 'logReportDirNestingLevelChanged' => 100, |
| 105 | + 'exceptionMessage' => '$exceptionMessage 100', |
| 106 | + ], |
| 107 | + ]; |
| 108 | + } |
42 | 109 |
|
43 | | - $loadProcessor = $this->createProcessor(); |
44 | | - $loadProcessor->loadReport($this->processor->reportId); |
45 | | - $this->assertEquals($expectedReportData, $loadProcessor->reportData, "File contents of report don't match"); |
| 110 | + /** |
| 111 | + * @return Processor |
| 112 | + */ |
| 113 | + private function createProcessor(): Processor |
| 114 | + { |
| 115 | + return Bootstrap::getObjectManager()->create(Processor::class); |
46 | 116 | } |
47 | 117 |
|
48 | 118 | /** |
49 | | - * @return \Magento\Framework\Error\Processor |
| 119 | + * Remove dir recursively |
| 120 | + * |
| 121 | + * @param string $dir |
| 122 | + * @param int $i |
| 123 | + * @return bool |
| 124 | + * @throws \Exception |
50 | 125 | */ |
51 | | - private function createProcessor() |
| 126 | + private function removeDirRecursively(string $dir, int $i = 0): bool |
52 | 127 | { |
53 | | - return \Magento\TestFramework\Helper\Bootstrap::getObjectManager() |
54 | | - ->create(\Magento\Framework\Error\Processor::class); |
| 128 | + if ($i >= 100) { |
| 129 | + throw new \Exception('Emergency exit from recursion'); |
| 130 | + } |
| 131 | + $files = array_diff(scandir($dir), ['.', '..']); |
| 132 | + foreach ($files as $file) { |
| 133 | + $i++; |
| 134 | + (is_dir("$dir/$file")) |
| 135 | + ? $this->removeDirRecursively("$dir/$file", $i) |
| 136 | + : unlink("$dir/$file"); |
| 137 | + } |
| 138 | + return rmdir($dir); |
55 | 139 | } |
56 | 140 | } |
0 commit comments