From 922e0fccb28cb19de461326811bee9ef1a6cac84 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Fri, 12 Jan 2024 16:33:16 +0400 Subject: [PATCH] Add `trap()->stackTrace()` method. Now `trap()` doesn't print stack trace --- LICENSE.md | 2 +- src/Client/TrapHandle.php | 26 +++++++++++++++----------- tests/Unit/Client/Base.php | 5 +++-- tests/Unit/Client/TrapTest.php | 12 +++++++++++- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/LICENSE.md b/LICENSE.md index 952502fa..9e66dc87 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # BSD-3 License -Copyright (c) 2023, Buggregator. All rights reserved. +Copyright (c) 2023–2024, Buggregator. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/src/Client/TrapHandle.php b/src/Client/TrapHandle.php index 04737bc6..d3cb066f 100644 --- a/src/Client/TrapHandle.php +++ b/src/Client/TrapHandle.php @@ -45,6 +45,20 @@ public function if(bool|callable $condition): self return $this; } + /** + * Add stack trace to the dump. + */ + public function stackTrace(): self + { + $cwd = \getcwd(); + $this->values['Stack trace'] = [ + 'cwd' => $cwd, + 'trace' => new TraceStub($this->staticState->stackTrace), + ]; + + return $this; + } + /** * Set max depth for the dump. * @@ -102,16 +116,6 @@ private function sendDump(): void $_SERVER['VAR_DUMPER_SERVER'] = '127.0.0.1:9912'; } - // If there are no values - stack trace - if ($this->values === []) { - VarDumper::dump([ - 'cwd' => \getcwd(), - // todo StackTrace::stackTrace(\getcwd()) - add CWD - 'trace' => new TraceStub($this->staticState->stackTrace), - ], depth: $this->depth); - return; - } - // Dump single value if (\array_keys($this->values) === [0]) { VarDumper::dump($this->values[0], depth: $this->depth); @@ -140,7 +144,7 @@ private function __construct( private function haveToSend(): bool { - if (!$this->haveToSend) { + if (!$this->haveToSend || $this->values === []) { return false; } diff --git a/tests/Unit/Client/Base.php b/tests/Unit/Client/Base.php index 12b71dda..e3454e8c 100644 --- a/tests/Unit/Client/Base.php +++ b/tests/Unit/Client/Base.php @@ -14,14 +14,14 @@ class Base extends TestCase { protected static ?Data $lastData = null; - protected static ?Closure $handler = null; protected function setUp(): void { + self::$lastData = null; Counter::clear(); $dumper = $this->getMockBuilder(DataDumperInterface::class) ->getMock(); - $dumper->expects($this->atLeastOnce()) + $dumper->expects($this->any()) ->method('dump') ->with( $this->callback(static function (Data $data): bool { @@ -38,6 +38,7 @@ protected function setUp(): void protected function tearDown(): void { + self::$lastData = null; Counter::clear(); Dumper::setDumper(null); parent::tearDown(); diff --git a/tests/Unit/Client/TrapTest.php b/tests/Unit/Client/TrapTest.php index 104433ee..05dc01d8 100644 --- a/tests/Unit/Client/TrapTest.php +++ b/tests/Unit/Client/TrapTest.php @@ -17,7 +17,7 @@ public function testLabel(): void */ public function testStackTrace(): void { - $line = __FILE__ . ':' . __LINE__ and trap(); + $line = __FILE__ . ':' . __LINE__ and trap()->stackTrace(); $this->assertArrayHasKey('trace', static::$lastData->getValue()); @@ -26,6 +26,16 @@ public function testStackTrace(): void $this->assertStringContainsString($line, $neededLine); } + /** + * Nothing is dumped if no arguments are passed to {@see trap()}. + */ + public function testEmptyTrapCall(): void + { + trap(); + + self::assertNull(self::$lastData); + } + /** * After calling {@see trap()} the dumped data isn't stored in the memory. */