Skip to content

Commit 5d6dc50

Browse files
Add stack trace to Test\DeprecationTriggered event
1 parent cf2e5bb commit 5d6dc50

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

src/Event/Emitter/DispatchingEmitter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,11 +791,12 @@ public function testTriggeredPhpDeprecation(Code\Test $test, string $message, st
791791
* @param non-empty-string $message
792792
* @param non-empty-string $file
793793
* @param positive-int $line
794+
* @param non-empty-string $stackTrace
794795
*
795796
* @throws InvalidArgumentException
796797
* @throws UnknownEventTypeException
797798
*/
798-
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void
799+
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger, string $stackTrace): void
799800
{
800801
$this->dispatcher->dispatch(
801802
new Test\DeprecationTriggered(
@@ -808,6 +809,7 @@ public function testTriggeredDeprecation(Code\Test $test, string $message, strin
808809
$ignoredByBaseline,
809810
$ignoredByTest,
810811
$trigger,
812+
$stackTrace,
811813
),
812814
);
813815
}

src/Event/Emitter/Emitter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ public function testTriggeredPhpDeprecation(Code\Test $test, string $message, st
190190
* @param non-empty-string $message
191191
* @param non-empty-string $file
192192
* @param positive-int $line
193+
* @param non-empty-string $stackTrace
193194
*/
194-
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger): void;
195+
public function testTriggeredDeprecation(Code\Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger, string $stackTrace): void;
195196

196197
/**
197198
* @param non-empty-string $message

src/Event/Events/Test/Issue/DeprecationTriggered.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@
4646
private bool $ignoredByTest;
4747
private IssueTrigger $trigger;
4848

49+
/**
50+
* @var non-empty-string
51+
*/
52+
private string $stackTrace;
53+
4954
/**
5055
* @param non-empty-string $message
5156
* @param non-empty-string $file
5257
* @param positive-int $line
58+
* @param non-empty-string $stackTrace
5359
*/
54-
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger)
60+
public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $message, string $file, int $line, bool $suppressed, bool $ignoredByBaseline, bool $ignoredByTest, IssueTrigger $trigger, string $stackTrace)
5561
{
5662
$this->telemetryInfo = $telemetryInfo;
5763
$this->test = $test;
@@ -62,6 +68,7 @@ public function __construct(Telemetry\Info $telemetryInfo, Test $test, string $m
6268
$this->ignoredByBaseline = $ignoredByBaseline;
6369
$this->ignoredByTest = $ignoredByTest;
6470
$this->trigger = $trigger;
71+
$this->stackTrace = $stackTrace;
6572
}
6673

6774
public function telemetryInfo(): Telemetry\Info
@@ -118,6 +125,14 @@ public function trigger(): IssueTrigger
118125
return $this->trigger;
119126
}
120127

128+
/**
129+
* @return non-empty-string
130+
*/
131+
public function stackTrace(): string
132+
{
133+
return $this->stackTrace;
134+
}
135+
121136
public function asString(): string
122137
{
123138
$message = $this->message;

src/Runner/ErrorHandler.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use function error_reporting;
3333
use function restore_error_handler;
3434
use function set_error_handler;
35+
use function sprintf;
3536
use PHPUnit\Event;
3637
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
3738
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
@@ -175,6 +176,7 @@ public function __invoke(int $errorNumber, string $errorString, string $errorFil
175176
$ignoredByBaseline,
176177
$ignoredByTest,
177178
$this->trigger($test, true),
179+
$this->stackTrace(),
178180
);
179181

180182
break;
@@ -388,4 +390,27 @@ private function frameIsMethod(array $frame, array $method): bool
388390
isset($frame['function']) &&
389391
$frame['function'] === $method['methodName'];
390392
}
393+
394+
/**
395+
* @return non-empty-string
396+
*/
397+
private function stackTrace(): string
398+
{
399+
$buffer = '';
400+
$excludeList = new ExcludeList(true);
401+
402+
foreach ($this->errorStackTrace() as $frame) {
403+
if ($excludeList->isExcluded($frame['file'])) {
404+
continue;
405+
}
406+
407+
$buffer .= sprintf(
408+
"%s:%s\n",
409+
$frame['file'],
410+
$frame['line'] ?? '?',
411+
);
412+
}
413+
414+
return $buffer;
415+
}
391416
}

tests/unit/Event/Events/Test/Issue/DeprecationTriggeredTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testConstructorSetsValues(): void
3030
$ignoredByBaseline = false;
3131
$ignoredByTest = false;
3232
$trigger = IssueTrigger::unknown();
33+
$stackTrace = 'stack trace';
3334

3435
$event = new DeprecationTriggered(
3536
$telemetryInfo,
@@ -41,6 +42,7 @@ public function testConstructorSetsValues(): void
4142
$ignoredByBaseline,
4243
$ignoredByTest,
4344
$trigger,
45+
$stackTrace,
4446
);
4547

4648
$this->assertSame($telemetryInfo, $event->telemetryInfo());
@@ -53,6 +55,7 @@ public function testConstructorSetsValues(): void
5355
$this->assertSame($ignoredByTest, $event->ignoredByTest());
5456
$this->assertSame('Test Triggered Deprecation (FooTest::testBar, unknown if issue was triggered in first-party code or third-party code)' . PHP_EOL . 'message', $event->asString());
5557
$this->assertSame($trigger, $event->trigger());
58+
$this->assertSame($stackTrace, $event->stackTrace());
5659
}
5760

5861
public function testCanBeIgnoredByBaseline(): void
@@ -67,6 +70,7 @@ public function testCanBeIgnoredByBaseline(): void
6770
true,
6871
false,
6972
IssueTrigger::unknown(),
73+
'stack trace',
7074
);
7175

7276
$this->assertTrue($event->ignoredByBaseline());
@@ -85,6 +89,7 @@ public function testCanBeIgnoredByTest(): void
8589
false,
8690
true,
8791
IssueTrigger::unknown(),
92+
'stack trace',
8893
);
8994

9095
$this->assertTrue($event->ignoredByTest());
@@ -103,6 +108,7 @@ public function testCanBeSuppressed(): void
103108
false,
104109
false,
105110
IssueTrigger::unknown(),
111+
'stack trace',
106112
);
107113

108114
$this->assertTrue($event->wasSuppressed());

0 commit comments

Comments
 (0)