‪TYPO3CMS  ‪main
IntrospectionProcessorTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use PHPUnit\Framework\MockObject\MockObject;
23 use Psr\Log\LogLevel;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪IntrospectionProcessorTest extends UnitTestCase
29 {
31 
35  protected array ‪$dummyBacktrace = [
36  [
37  'file' => '/foo/filename1.php',
38  'line' => 1,
39  'class' => 'class1',
40  'function' => 'function1',
41  ],
42  [
43  'file' => '/foo/filename2.php',
44  'line' => 2,
45  'class' => 'class2',
46  'function' => 'function2',
47  ],
48  [
49  'class' => 'class3',
50  'function' => 'function3',
51  ],
52  [
53  'file' => '/foo/filename4.php',
54  'line' => 4,
55  'class' => 'class4',
56  'function' => 'function4',
57  ],
58  ];
59 
63  protected function ‪setUp(): void
64  {
65  parent::setUp();
66  $this->processor = $this->getAccessibleMock(IntrospectionProcessor::class, ['getDebugBacktrace']);
67  }
68 
69  #[Test]
71  {
72  $this->processor->method('getDebugBacktrace')->willReturn($this->dummyBacktrace);
73  $logRecord = new ‪LogRecord('test.core.log', LogLevel::DEBUG, 'test');
74  $logRecord = $this->processor->processLogRecord($logRecord);
75 
76  self::assertEquals($this->dummyBacktrace[0]['file'], $logRecord['data']['file']);
77  self::assertEquals($this->dummyBacktrace[0]['line'], $logRecord['data']['line']);
78  self::assertEquals($this->dummyBacktrace[0]['class'], $logRecord['data']['class']);
79  self::assertEquals($this->dummyBacktrace[0]['function'], $logRecord['data']['function']);
80  }
81 
82  #[Test]
84  {
86  array_unshift(
88  [
89  'file' => '/foo/Log.php',
90  'line' => 999,
91  'class' => 'TYPO3\CMS\Core\Log\Bar\Foo',
92  'function' => 'function999',
93  ],
94  [
95  'file' => '/foo/Log2.php',
96  'line' => 888,
97  'class' => 'TYPO3\CMS\Core\Log\Bar2\Foo2',
98  'function' => 'function888',
99  ]
100  );
101  $this->processor->method('getDebugBacktrace')->willReturn(‪$dummyBacktrace);
102 
103  $logRecord = new ‪LogRecord('test.core.log', LogLevel::DEBUG, 'test');
104  $logRecord = $this->processor->processLogRecord($logRecord);
105 
106  self::assertEquals($this->dummyBacktrace[0]['file'], $logRecord['data']['file']);
107  self::assertEquals($this->dummyBacktrace[0]['line'], $logRecord['data']['line']);
108  self::assertEquals($this->dummyBacktrace[0]['class'], $logRecord['data']['class']);
109  self::assertEquals($this->dummyBacktrace[0]['function'], $logRecord['data']['function']);
110  }
111 
116  {
117  return [
118  ['0'],
119  ['1'],
120  ['3'],
121  ];
122  }
123 
124  #[DataProvider('introspectionProcessorShiftsGivenNumberOfEntriesFromBacktraceDataProvider')]
125  #[Test]
127  {
128  $this->processor->method('getDebugBacktrace')->willReturn($this->dummyBacktrace);
129  $this->processor->setShiftBackTraceLevel($number);
130 
131  $logRecord = new ‪LogRecord('test.core.log', LogLevel::DEBUG, 'test');
132  $logRecord = $this->processor->processLogRecord($logRecord);
133 
134  self::assertEquals($this->dummyBacktrace[$number]['file'], $logRecord['data']['file']);
135  self::assertEquals($this->dummyBacktrace[$number]['line'], $logRecord['data']['line']);
136  self::assertEquals($this->dummyBacktrace[$number]['class'], $logRecord['data']['class']);
137  self::assertEquals($this->dummyBacktrace[$number]['function'], $logRecord['data']['function']);
138  }
139 
140  #[Test]
142  {
143  $this->processor->method('getDebugBacktrace')->willReturn($this->dummyBacktrace);
144  $this->processor->setShiftBackTraceLevel(4);
145 
146  $logRecord = new ‪LogRecord('test.core.log', LogLevel::DEBUG, 'test');
147  $logRecord = $this->processor->processLogRecord($logRecord);
148 
149  self::assertEquals($this->dummyBacktrace[3]['file'], $logRecord['data']['file']);
150  self::assertEquals($this->dummyBacktrace[3]['line'], $logRecord['data']['line']);
151  self::assertEquals($this->dummyBacktrace[3]['class'], $logRecord['data']['class']);
152  self::assertEquals($this->dummyBacktrace[3]['function'], $logRecord['data']['function']);
153  }
154 
155  #[Test]
157  {
158  $this->processor->method('getDebugBacktrace')->willReturn($this->dummyBacktrace);
159 
160  $this->processor->setAppendFullBackTrace(true);
161 
162  $logRecord = new ‪LogRecord('test.core.log', LogLevel::DEBUG, 'test');
163  $logRecord = $this->processor->processLogRecord($logRecord);
164 
165  self::assertContains($this->dummyBacktrace[0], $logRecord['data']['backtrace']);
166  self::assertContains($this->dummyBacktrace[1], $logRecord['data']['backtrace']);
167  self::assertContains($this->dummyBacktrace[2], $logRecord['data']['backtrace']);
168  self::assertContains($this->dummyBacktrace[3], $logRecord['data']['backtrace']);
169  }
170 }
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\$processor
‪IntrospectionProcessor &MockObject $processor
Definition: IntrospectionProcessorTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\setUp
‪setUp()
Definition: IntrospectionProcessorTest.php:63
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\introspectionProcessorShiftsGivenNumberOfEntriesFromBacktraceDataProvider
‪static introspectionProcessorShiftsGivenNumberOfEntriesFromBacktraceDataProvider()
Definition: IntrospectionProcessorTest.php:115
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest
Definition: IntrospectionProcessorTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor
Definition: AbstractMemoryProcessorTest.php:18
‪TYPO3\CMS\Core\Log\Processor\IntrospectionProcessor
Definition: IntrospectionProcessor.php:24
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\appendFullBacktraceAddsTheFullBacktraceAsStringToTheLog
‪appendFullBacktraceAddsTheFullBacktraceAsStringToTheLog()
Definition: IntrospectionProcessorTest.php:156
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\introspectionProcessorLeavesOneEntryIfGivenNumberOfEntriesFromBacktraceIsGreaterOrEqualNumberOfBacktraceLevels
‪introspectionProcessorLeavesOneEntryIfGivenNumberOfEntriesFromBacktraceIsGreaterOrEqualNumberOfBacktraceLevels()
Definition: IntrospectionProcessorTest.php:141
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:24
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\introspectionProcessorAddsLastBacktraceItemToLogRecord
‪introspectionProcessorAddsLastBacktraceItemToLogRecord()
Definition: IntrospectionProcessorTest.php:70
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\introspectionProcessorShiftsGivenNumberOfEntriesFromBacktrace
‪introspectionProcessorShiftsGivenNumberOfEntriesFromBacktrace($number)
Definition: IntrospectionProcessorTest.php:126
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\$dummyBacktrace
‪array $dummyBacktrace
Definition: IntrospectionProcessorTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Log\Processor\IntrospectionProcessorTest\introspectionProcessorShiftsLogRelatedFunctionsFromBacktrace
‪introspectionProcessorShiftsLogRelatedFunctionsFromBacktrace()
Definition: IntrospectionProcessorTest.php:83