From b070f11910eb9a3f96a33a28e2babea24150d8d8 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 10:05:34 +0300 Subject: [PATCH 01/24] Add more tests --- tests/Shared/AbstractCollectorTestCase.php | 12 +++++ .../Collector/ContainerInterfaceProxyTest.php | 15 ++++++- .../EventDispatcherInterfaceProxyTest.php | 35 +++++++++++++++ tests/Unit/Collector/LoggerProxyTest.php | 19 ++++---- .../Unit/Collector/TimelineCollectorTest.php | 44 +++++++++++++++++++ 5 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 tests/Unit/Collector/EventDispatcherInterfaceProxyTest.php create mode 100644 tests/Unit/Collector/TimelineCollectorTest.php diff --git a/tests/Shared/AbstractCollectorTestCase.php b/tests/Shared/AbstractCollectorTestCase.php index 80ac2cd62..25dd6c63a 100644 --- a/tests/Shared/AbstractCollectorTestCase.php +++ b/tests/Shared/AbstractCollectorTestCase.php @@ -40,6 +40,18 @@ public function testEmptyCollector(): void } } + public function testInactiveCollector(): void + { + $collector = $this->getCollector(); + + $this->collectTestData($collector); + + $this->assertEquals([], $collector->getCollected()); + if ($collector instanceof SummaryCollectorInterface) { + $this->assertEquals([], $collector->getSummary()); + } + } + abstract protected function getCollector(): CollectorInterface; abstract protected function collectTestData(CollectorInterface $collector): void; diff --git a/tests/Unit/Collector/ContainerInterfaceProxyTest.php b/tests/Unit/Collector/ContainerInterfaceProxyTest.php index b38e7bdee..cf27b2a69 100644 --- a/tests/Unit/Collector/ContainerInterfaceProxyTest.php +++ b/tests/Unit/Collector/ContainerInterfaceProxyTest.php @@ -143,6 +143,10 @@ public function testGetWithoutConfig(): void public function testGetAndHasWithWrongId(): void { + $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $this->getConfig()); + + $this->assertFalse($containerProxy->has(CollectorInterface::class)); + $this->expectException(ContainerExceptionInterface::class); $this->expectExceptionMessage( sprintf( @@ -151,11 +155,18 @@ public function testGetAndHasWithWrongId(): void CollectorInterface::class ) ); + $containerProxy->get(CollectorInterface::class); + } + public function testGetContainerItself(): void + { $containerProxy = new ContainerInterfaceProxy($this->getContainer(), $this->getConfig()); - $containerProxy->has(CollectorInterface::class); - $containerProxy->get(CollectorInterface::class); + $this->assertTrue($containerProxy->has(ContainerInterface::class)); + + $container = $containerProxy->get(ContainerInterface::class); + $this->assertNotNull($container); + $this->assertInstanceOf(ContainerInterface::class, $container); } public function testGetAndHasWithNotService(): void diff --git a/tests/Unit/Collector/EventDispatcherInterfaceProxyTest.php b/tests/Unit/Collector/EventDispatcherInterfaceProxyTest.php new file mode 100644 index 000000000..be091d9f2 --- /dev/null +++ b/tests/Unit/Collector/EventDispatcherInterfaceProxyTest.php @@ -0,0 +1,35 @@ +startup(); + + $eventDispatcherMock = $this->createMock(EventDispatcherInterface::class); + $eventDispatcherMock + ->expects($this->once()) + ->method('dispatch') + ->with($event) + ->willReturn($event); + $eventDispatcher = new EventDispatcherInterfaceProxy($eventDispatcherMock, $collector); + + $newEvent = $eventDispatcher->dispatch($event); + + $this->assertSame($event, $newEvent); + $this->assertCount(1, $collector->getCollected()); + } +} diff --git a/tests/Unit/Collector/LoggerProxyTest.php b/tests/Unit/Collector/LoggerProxyTest.php index a5a09eefd..ba9c2f660 100644 --- a/tests/Unit/Collector/LoggerProxyTest.php +++ b/tests/Unit/Collector/LoggerProxyTest.php @@ -44,16 +44,15 @@ public function testMethodLog($method, string $level, string $message, array $co $proxy->log($level, $message, $context); } - public function logMethodsProvider(): array + public static function logMethodsProvider(): iterable { - return [ - ['alert', LogLevel::ALERT, 'message', []], - ['critical', LogLevel::CRITICAL, 'message', []], - ['debug', LogLevel::DEBUG, 'message', []], - ['emergency', LogLevel::EMERGENCY, 'message', []], - ['error', LogLevel::ERROR, 'message', ['context']], - ['info', LogLevel::INFO, 'message', ['context']], - ['warning', LogLevel::WARNING, 'message', ['context']], - ]; + yield 'alert' => ['alert', LogLevel::ALERT, 'message', []]; + yield 'critical' => ['critical', LogLevel::CRITICAL, 'message', []]; + yield 'debug' => ['debug', LogLevel::DEBUG, 'message', []]; + yield 'emergency' => ['emergency', LogLevel::EMERGENCY, 'message', []]; + yield 'notice' => ['notice', LogLevel::NOTICE, 'message', []]; + yield 'error' => ['error', LogLevel::ERROR, 'message', ['context']]; + yield 'info' => ['info', LogLevel::INFO, 'message', ['context']]; + yield 'warning' => ['warning', LogLevel::WARNING, 'message', ['context']]; } } diff --git a/tests/Unit/Collector/TimelineCollectorTest.php b/tests/Unit/Collector/TimelineCollectorTest.php new file mode 100644 index 000000000..faf42052b --- /dev/null +++ b/tests/Unit/Collector/TimelineCollectorTest.php @@ -0,0 +1,44 @@ +collect(new LogCollector($collector), '123'); + $collector->collect(new LogCollector($collector), '345', 'context2', __FILE__ . ':' . __LINE__); + } + + protected function getCollector(): CollectorInterface + { + return new TimelineCollector(); + } + + protected function checkCollectedData(array $data): void + { + parent::checkCollectedData($data); + + $this->assertNotEmpty($data); + $this->assertCount(2, $data); + $this->assertCount(4, $data[0]); + $this->assertSame(LogCollector::class, $data[0][2]); + $this->assertSame('123', $data[0][1]); + $this->assertSame([], $data[0][3]); + + $this->assertCount(4, $data[1]); + $this->assertSame(LogCollector::class, $data[1][2]); + $this->assertSame('345', $data[1][1]); + $this->assertSame(['context2', __FILE__ . ':' . 29], $data[1][3]); + } +} From b8a74c4c49f1df9b7fb3f1e52b3e5a8967dfabcb Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 10:07:42 +0300 Subject: [PATCH 02/24] Fix test --- tests/Unit/Collector/TimelineCollectorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Collector/TimelineCollectorTest.php b/tests/Unit/Collector/TimelineCollectorTest.php index faf42052b..13829d105 100644 --- a/tests/Unit/Collector/TimelineCollectorTest.php +++ b/tests/Unit/Collector/TimelineCollectorTest.php @@ -17,7 +17,7 @@ final class TimelineCollectorTest extends AbstractCollectorTestCase protected function collectTestData(CollectorInterface $collector): void { $collector->collect(new LogCollector($collector), '123'); - $collector->collect(new LogCollector($collector), '345', 'context2', __FILE__ . ':' . __LINE__); + $collector->collect(new LogCollector($collector), '345', 'context2', __FILE__ . ':' . 123); } protected function getCollector(): CollectorInterface @@ -39,6 +39,6 @@ protected function checkCollectedData(array $data): void $this->assertCount(4, $data[1]); $this->assertSame(LogCollector::class, $data[1][2]); $this->assertSame('345', $data[1][1]); - $this->assertSame(['context2', __FILE__ . ':' . 29], $data[1][3]); + $this->assertSame(['context2', __FILE__ . ':' . 123], $data[1][3]); } } From 2a692244a98b91a2c7e49e8a5b97682392faf71f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 18:48:29 +0300 Subject: [PATCH 03/24] Cover BacktraceIgnoreMatcher --- .../Helper/BacktraceIgnoreMatcherTest.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/Unit/Helper/BacktraceIgnoreMatcherTest.php diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php new file mode 100644 index 000000000..65048a954 --- /dev/null +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -0,0 +1,70 @@ +assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [self::class])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [stdClass::class])); + + $backtrace[3] = $backtrace[0]; + + $this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [self::class])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [stdClass::class])); + } + + public function testFileIgnorance() + { + $backtrace = debug_backtrace(); + $reflection = new \ReflectionClass(TestCase::class); + $file = $reflection->getFileName(); + + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [$file])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); + + $backtrace[2] = $backtrace[0]; + + $this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [$file])); + $this->assertTrue( + BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [dirname($file) . DIRECTORY_SEPARATOR . '*']) + ); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); + } + public function testStringMatches() + { + $this->assertTrue( + BacktraceIgnoreMatcher::doesStringMatchPattern( + 'dev/123/456', + ['dev/123/456'] + ) + ); + $this->assertTrue( + BacktraceIgnoreMatcher::doesStringMatchPattern( + 'dev/123/456', + ['456'] + ) + ); + $this->assertTrue( + BacktraceIgnoreMatcher::doesStringMatchPattern( + 'dev/123/456', + ['dev/.*/456'] + ) + ); + $this->assertTrue( + BacktraceIgnoreMatcher::doesStringMatchPattern( + 'dev/123/456', + ['dev*/456', 'dev/123/*'] + ) + ); + } +} From e3db2f4927fadc7c7ee358efb1a1b583c1f23060 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 18:54:01 +0300 Subject: [PATCH 04/24] Cover VarDumperCollector --- src/Collector/VarDumperCollector.php | 4 +- .../Unit/Collector/VarDumperCollectorTest.php | 44 +++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Collector/VarDumperCollectorTest.php diff --git a/src/Collector/VarDumperCollector.php b/src/Collector/VarDumperCollector.php index 8eb9c3e4a..8042f1520 100644 --- a/src/Collector/VarDumperCollector.php +++ b/src/Collector/VarDumperCollector.php @@ -29,9 +29,7 @@ public function getCollected(): array return []; } - return [ - 'var-dumper' => $this->vars, - ]; + return $this->vars; } public function getSummary(): array diff --git a/tests/Unit/Collector/VarDumperCollectorTest.php b/tests/Unit/Collector/VarDumperCollectorTest.php new file mode 100644 index 000000000..e07c6c33b --- /dev/null +++ b/tests/Unit/Collector/VarDumperCollectorTest.php @@ -0,0 +1,44 @@ +collect('test', 'file:123'); + } + + protected function getCollector(): CollectorInterface + { + return new VarDumperCollector(new TimelineCollector()); + } + + protected function checkCollectedData(array $data): void + { + parent::checkCollectedData($data); + $this->assertCount(1, $data); + $this->assertCount(2, $data[0]); + $this->assertSame('test', $data[0]['variable']); + $this->assertSame('file:123', $data[0]['line']); + } + + protected function checkSummaryData(array $data): void + { + parent::checkSummaryData($data); + $this->assertCount(1, $data); + $this->assertArrayHasKey('var-dumper', $data); + $this->assertArrayHasKey('total', $data['var-dumper']); + $this->assertEquals(1, $data['var-dumper']['total']); + } +} From 99c239b861ece07454c62e713d3731ada4e732de Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 19:01:47 +0300 Subject: [PATCH 05/24] Cover ExceptionCollector --- src/Collector/ExceptionCollector.php | 6 ++ .../Unit/Collector/ExceptionCollectorTest.php | 66 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/Unit/Collector/ExceptionCollectorTest.php diff --git a/src/Collector/ExceptionCollector.php b/src/Collector/ExceptionCollector.php index f0ae8bfaa..32b42fbdb 100644 --- a/src/Collector/ExceptionCollector.php +++ b/src/Collector/ExceptionCollector.php @@ -19,6 +19,9 @@ public function __construct(private TimelineCollector $timelineCollector) public function getCollected(): array { + if (!$this->isActive()) { + return []; + } if ($this->exception === null) { return []; } @@ -45,6 +48,9 @@ public function collect(ApplicationError $error): void public function getSummary(): array { + if (!$this->isActive()) { + return []; + } return [ 'exception' => $this->exception === null ? [] : [ 'class' => $this->exception::class, diff --git a/tests/Unit/Collector/ExceptionCollectorTest.php b/tests/Unit/Collector/ExceptionCollectorTest.php new file mode 100644 index 000000000..08b8fcae7 --- /dev/null +++ b/tests/Unit/Collector/ExceptionCollectorTest.php @@ -0,0 +1,66 @@ +collect(new ApplicationError($exception)); + } + + protected function getCollector(): CollectorInterface + { + return new ExceptionCollector(new TimelineCollector()); + } + + protected function checkCollectedData(array $data): void + { + parent::checkCollectedData($data); + $this->assertCount(1, $data); + $exception = $data[0]; + + $this->assertArrayHasKey('class', $exception); + $this->assertArrayHasKey('message', $exception); + $this->assertArrayHasKey('file', $exception); + $this->assertArrayHasKey('line', $exception); + $this->assertArrayHasKey('code', $exception); + $this->assertArrayHasKey('trace', $exception); + $this->assertArrayHasKey('traceAsString', $exception); + + $this->assertEquals(Exception::class, $exception['class']); + $this->assertEquals('test', $exception['message']); + $this->assertEquals(777, $exception['code']); + } + + protected function checkSummaryData(array $data): void + { + parent::checkSummaryData($data); + $this->assertCount(1, $data); + $this->assertArrayHasKey('exception', $data); + + $exception = $data['exception']; + $this->assertArrayHasKey('class', $exception); + $this->assertArrayHasKey('message', $exception); + $this->assertArrayHasKey('file', $exception); + $this->assertArrayHasKey('line', $exception); + $this->assertArrayHasKey('code', $exception); + + $this->assertEquals(Exception::class, $exception['class']); + $this->assertEquals('test', $exception['message']); + $this->assertEquals(777, $exception['code']); + } +} From e93c5a87df55f814a81f71134eb753d6675b354f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 19:12:10 +0300 Subject: [PATCH 06/24] Cover HttpClientCollector --- src/Collector/HttpClientCollector.php | 56 ++++++++------- .../Collector/HttpClientCollectorTest.php | 69 +++++++++++++++++++ 2 files changed, 100 insertions(+), 25 deletions(-) create mode 100644 tests/Unit/Collector/HttpClientCollectorTest.php diff --git a/src/Collector/HttpClientCollector.php b/src/Collector/HttpClientCollector.php index 5e96aac43..1aef6d593 100644 --- a/src/Collector/HttpClientCollector.php +++ b/src/Collector/HttpClientCollector.php @@ -33,29 +33,7 @@ public function __construct(private TimelineCollector $timelineCollector) { } - public function getCollected(): array - { - return array_merge(...array_values($this->requests)); - } - - public function getSummary(): array - { - return [ - 'http' => [ - 'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)), - 'totalTime' => array_sum( - array_merge( - ...array_map( - static fn (array $entry) => array_column($entry, 'totalTime'), - array_values($this->requests) - ) - ) - ), - ], - ]; - } - - public function collect(RequestInterface $request, float|string $startTime, string $line, ?string $uniqueId): void + public function collect(RequestInterface $request, float $startTime, string $line, ?string $uniqueId): void { if (!$this->isActive()) { return; @@ -73,7 +51,7 @@ public function collect(RequestInterface $request, float|string $startTime, stri $this->timelineCollector->collect($this, $uniqueId); } - public function collectTotalTime(?ResponseInterface $response, float|string $startTime, ?string $uniqueId): void + public function collectTotalTime(?ResponseInterface $response, float $endTime, ?string $uniqueId): void { if (!$this->isActive()) { return; @@ -88,7 +66,35 @@ public function collectTotalTime(?ResponseInterface $response, float|string $sta $entry['responseStatus'] = $response->getStatusCode(); Message::rewindBody($response); } - $entry['endTime'] = $startTime; + $entry['endTime'] = $endTime; $entry['totalTime'] = $entry['endTime'] - $entry['startTime']; } + + public function getCollected(): array + { + if (!$this->isActive()) { + return []; + } + return array_merge(...array_values($this->requests)); + } + + public function getSummary(): array + { + if (!$this->isActive()) { + return []; + } + return [ + 'http' => [ + 'count' => array_sum(array_map(static fn (array $requests) => count($requests), $this->requests)), + 'totalTime' => array_sum( + array_merge( + ...array_map( + static fn (array $entry) => array_column($entry, 'totalTime'), + array_values($this->requests) + ) + ) + ), + ], + ]; + } } diff --git a/tests/Unit/Collector/HttpClientCollectorTest.php b/tests/Unit/Collector/HttpClientCollectorTest.php new file mode 100644 index 000000000..eb0190e33 --- /dev/null +++ b/tests/Unit/Collector/HttpClientCollectorTest.php @@ -0,0 +1,69 @@ +collect( + new \GuzzleHttp\Psr7\Request('GET', 'http://example.com'), + startTime: 10.10, + line: 'file1:123', + uniqueId: 'test1', + ); + $collector->collect( + new \GuzzleHttp\Psr7\Request('GET', 'http://yiiframework.com'), + startTime: 12.10, + line: 'file2:555', + uniqueId: 'test2' + ); + + $collector->collectTotalTime( + new Response(200, [], 'test'), + endTime: 13.10, + uniqueId: 'test1' + ); + $collector->collectTotalTime( + new Response(200, [], 'test'), + endTime: 12.20, + uniqueId: 'test2' + ); + } + + protected function getCollector(): CollectorInterface + { + return new HttpClientCollector(new TimelineCollector()); + } + + protected function checkCollectedData(array $data): void + { + parent::checkCollectedData($data); + + $this->assertCount(2, $data); + } + + protected function checkSummaryData(array $data): void + { + parent::checkSummaryData($data); + $this->assertCount(1, $data); + $this->assertArrayHasKey('http', $data); + $this->assertCount(2, $data['http']); + $this->assertArrayHasKey('count', $data['http']); + $this->assertArrayHasKey('totalTime', $data['http']); + + $this->assertEquals(2, $data['http']['count']); + $this->assertEquals(3.1, round($data['http']['totalTime'], 1)); + } +} From e09acbe82907a3c29402eaaa9260df0d317c6728 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 19:16:38 +0300 Subject: [PATCH 07/24] Cover HttpClientCollectorProxy --- .../HttpClientInterfaceProxyTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/Unit/Collector/HttpClientInterfaceProxyTest.php diff --git a/tests/Unit/Collector/HttpClientInterfaceProxyTest.php b/tests/Unit/Collector/HttpClientInterfaceProxyTest.php new file mode 100644 index 000000000..188b2c9fd --- /dev/null +++ b/tests/Unit/Collector/HttpClientInterfaceProxyTest.php @@ -0,0 +1,37 @@ +createMock(ClientInterface::class); + $client->expects($this->once()) + ->method('sendRequest') + ->with($request) + ->willReturn($response); + $collector = new HttpClientCollector(new TimelineCollector()); + $collector->startup(); + + $proxy = new HttpClientInterfaceProxy($client, $collector); + + $newResponse = $proxy->sendRequest($request); + + $this->assertSame($newResponse, $response); + $this->assertCount(1, $collector->getCollected()); + } +} From f60afc61146c8bbb7a03bb9c9ddedd30f3a6a35b Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 13 Oct 2023 16:16:54 +0000 Subject: [PATCH 08/24] Apply fixes from StyleCI --- tests/Unit/Helper/BacktraceIgnoreMatcherTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php index 65048a954..e3788a47d 100644 --- a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -40,6 +40,7 @@ public function testFileIgnorance() ); $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); } + public function testStringMatches() { $this->assertTrue( From f22a2da5129533247a4c9888197426f41077a6e3 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 19:20:16 +0300 Subject: [PATCH 09/24] Fix test --- tests/Unit/Helper/BacktraceIgnoreMatcherTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php index 65048a954..ca1531dbc 100644 --- a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -29,7 +29,7 @@ public function testFileIgnorance() $reflection = new \ReflectionClass(TestCase::class); $file = $reflection->getFileName(); - $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [$file])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote($file)])); $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); $backtrace[2] = $backtrace[0]; From 23dce698e6c53c7dfaebf44e0db74744407146e4 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 19:32:08 +0300 Subject: [PATCH 10/24] Cover more cases --- .../Unit/Collector/ExceptionCollectorTest.php | 28 ++++++----- .../Collector/HttpClientCollectorTest.php | 47 +++++++++++++++++-- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/tests/Unit/Collector/ExceptionCollectorTest.php b/tests/Unit/Collector/ExceptionCollectorTest.php index 08b8fcae7..cb5839e75 100644 --- a/tests/Unit/Collector/ExceptionCollectorTest.php +++ b/tests/Unit/Collector/ExceptionCollectorTest.php @@ -18,7 +18,7 @@ final class ExceptionCollectorTest extends AbstractCollectorTestCase */ protected function collectTestData(CollectorInterface $collector): void { - $exception = new Exception('test', 777); + $exception = new Exception('test', 777, new Exception('previous', 666)); $collector->collect(new ApplicationError($exception)); } @@ -30,20 +30,26 @@ protected function getCollector(): CollectorInterface protected function checkCollectedData(array $data): void { parent::checkCollectedData($data); - $this->assertCount(1, $data); - $exception = $data[0]; - - $this->assertArrayHasKey('class', $exception); - $this->assertArrayHasKey('message', $exception); - $this->assertArrayHasKey('file', $exception); - $this->assertArrayHasKey('line', $exception); - $this->assertArrayHasKey('code', $exception); - $this->assertArrayHasKey('trace', $exception); - $this->assertArrayHasKey('traceAsString', $exception); + $this->assertCount(2, $data); + foreach ($data as $exception) { + $this->assertArrayHasKey('class', $exception); + $this->assertArrayHasKey('message', $exception); + $this->assertArrayHasKey('file', $exception); + $this->assertArrayHasKey('line', $exception); + $this->assertArrayHasKey('code', $exception); + $this->assertArrayHasKey('trace', $exception); + $this->assertArrayHasKey('traceAsString', $exception); + } + $exception = $data[0]; $this->assertEquals(Exception::class, $exception['class']); $this->assertEquals('test', $exception['message']); $this->assertEquals(777, $exception['code']); + + $exception = $data[1]; + $this->assertEquals(Exception::class, $exception['class']); + $this->assertEquals('previous', $exception['message']); + $this->assertEquals(666, $exception['code']); } protected function checkSummaryData(array $data): void diff --git a/tests/Unit/Collector/HttpClientCollectorTest.php b/tests/Unit/Collector/HttpClientCollectorTest.php index eb0190e33..52c0c5b92 100644 --- a/tests/Unit/Collector/HttpClientCollectorTest.php +++ b/tests/Unit/Collector/HttpClientCollectorTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Yii\Debug\Tests\Unit\Collector; +use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\HttpClientCollector; @@ -18,17 +19,23 @@ final class HttpClientCollectorTest extends AbstractCollectorTestCase protected function collectTestData(CollectorInterface $collector): void { $collector->collect( - new \GuzzleHttp\Psr7\Request('GET', 'http://example.com'), + new Request('GET', 'http://example.com'), startTime: 10.10, line: 'file1:123', uniqueId: 'test1', ); $collector->collect( - new \GuzzleHttp\Psr7\Request('GET', 'http://yiiframework.com'), + new Request('POST', 'http://yiiframework.com'), startTime: 12.10, line: 'file2:555', uniqueId: 'test2' ); + $collector->collect( + new Request('GET', 'http://yiiframework.com'), + startTime: 15.00, + line: 'file2:666', + uniqueId: 'test3' + ); $collector->collectTotalTime( new Response(200, [], 'test'), @@ -40,6 +47,11 @@ protected function collectTestData(CollectorInterface $collector): void endTime: 12.20, uniqueId: 'test2' ); + $collector->collectTotalTime( + new Response(200, [], 'test'), + endTime: 20.00, + uniqueId: 'test4' + ); } protected function getCollector(): CollectorInterface @@ -51,7 +63,34 @@ protected function checkCollectedData(array $data): void { parent::checkCollectedData($data); - $this->assertCount(2, $data); + $this->assertCount(3, $data); + + $entry = $data[0]; + $this->assertEquals(10.10, $entry['startTime']); + $this->assertEquals(13.10, $entry['endTime']); + $this->assertEquals(3.0, $entry['totalTime']); + $this->assertEquals('GET', $entry['method']); + $this->assertEquals('http://example.com', $entry['uri']); + $this->assertEquals(['Host' => ['example.com']], $entry['headers']); + $this->assertEquals('file1:123', $entry['line']); + + $entry = $data[1]; + $this->assertEquals(12.10, $entry['startTime']); + $this->assertEquals(12.20, $entry['endTime']); + $this->assertEquals(0.1, round($entry['totalTime'], 1)); + $this->assertEquals('POST', $entry['method']); + $this->assertEquals('http://yiiframework.com', $entry['uri']); + $this->assertEquals(['Host' => ['yiiframework.com']], $entry['headers']); + $this->assertEquals('file2:555', $entry['line']); + + $entry = $data[2]; + $this->assertEquals(15.0, $entry['startTime']); + $this->assertEquals(15.0, $entry['endTime']); + $this->assertEquals(0.0, round($entry['totalTime'], 1)); + $this->assertEquals('GET', $entry['method']); + $this->assertEquals('http://yiiframework.com', $entry['uri']); + $this->assertEquals(['Host' => ['yiiframework.com']], $entry['headers']); + $this->assertEquals('file2:666', $entry['line']); } protected function checkSummaryData(array $data): void @@ -63,7 +102,7 @@ protected function checkSummaryData(array $data): void $this->assertArrayHasKey('count', $data['http']); $this->assertArrayHasKey('totalTime', $data['http']); - $this->assertEquals(2, $data['http']['count']); + $this->assertEquals(3, $data['http']['count']); $this->assertEquals(3.1, round($data['http']['totalTime'], 1)); } } From ef66022001abed38d29a5704107bc2bb1733de91 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 20:16:16 +0300 Subject: [PATCH 11/24] Cover more cases --- .../Console/ConsoleAppInfoCollector.php | 2 +- src/Collector/Web/WebAppInfoCollector.php | 4 +- src/Storage/FileStorage.php | 37 +++++++------- tests/Unit/Collector/.gitignore | 1 - .../Collector/ConsoleAppInfoCollectorTest.php | 21 +++++++- tests/Unit/Storage/AbstractStorageTest.php | 51 ++++++++++++++----- tests/Unit/Storage/FileStorageTest.php | 29 ++++++++++- tests/Unit/Storage/MemoryStorageTest.php | 22 ++++++++ 8 files changed, 127 insertions(+), 40 deletions(-) delete mode 100644 tests/Unit/Collector/.gitignore diff --git a/src/Collector/Console/ConsoleAppInfoCollector.php b/src/Collector/Console/ConsoleAppInfoCollector.php index 3211ee285..5aa75a225 100644 --- a/src/Collector/Console/ConsoleAppInfoCollector.php +++ b/src/Collector/Console/ConsoleAppInfoCollector.php @@ -43,7 +43,7 @@ public function getCollected(): array public function collect(object $event): void { - if (!is_object($event) || !$this->isActive()) { + if (!$this->isActive()) { return; } diff --git a/src/Collector/Web/WebAppInfoCollector.php b/src/Collector/Web/WebAppInfoCollector.php index 6f389b161..b22b711cd 100644 --- a/src/Collector/Web/WebAppInfoCollector.php +++ b/src/Collector/Web/WebAppInfoCollector.php @@ -12,8 +12,6 @@ use Yiisoft\Yii\Http\Event\AfterRequest; use Yiisoft\Yii\Http\Event\BeforeRequest; -use function is_object; - final class WebAppInfoCollector implements SummaryCollectorInterface { use CollectorTrait; @@ -44,7 +42,7 @@ public function getCollected(): array public function collect(object $event): void { - if (!is_object($event) || !$this->isActive()) { + if (!$this->isActive()) { return; } diff --git a/src/Storage/FileStorage.php b/src/Storage/FileStorage.php index ea39b7c1d..22809ba71 100644 --- a/src/Storage/FileStorage.php +++ b/src/Storage/FileStorage.php @@ -136,24 +136,25 @@ private function collectSummaryData(): array */ private function gc(): void { - $summaryFiles = glob($this->path . '/**/**/sumamry.json', GLOB_NOSORT); - if ((is_countable($summaryFiles) ? count($summaryFiles) : 0) >= $this->historySize + 1) { - uasort($summaryFiles, static fn ($a, $b) => filemtime($b) <=> filemtime($a)); - $excessFiles = array_slice($summaryFiles, $this->historySize); - foreach ($excessFiles as $file) { - $path1 = dirname($file); - $path2 = dirname($file, 2); - $path3 = dirname($file, 3); - $resource = substr($path1, strlen($path3)); - - - FileHelper::removeDirectory($this->path . $resource); - - // Clean empty group directories - $group = substr($path2, strlen($path3)); - if (FileHelper::isEmptyDirectory($this->path . $group)) { - FileHelper::removeDirectory($this->path . $group); - } + $summaryFiles = glob($this->path . '/**/**/summary.json', GLOB_NOSORT); + if (empty($summaryFiles) || count($summaryFiles) <= $this->historySize) { + return; + } + + uasort($summaryFiles, static fn ($a, $b) => filemtime($b) <=> filemtime($a)); + $excessFiles = array_slice($summaryFiles, $this->historySize); + foreach ($excessFiles as $file) { + $path1 = dirname($file); + $path2 = dirname($file, 2); + $path3 = dirname($file, 3); + $resource = substr($path1, strlen($path3)); + + FileHelper::removeDirectory($this->path . $resource); + + // Clean empty group directories + $group = substr($path2, strlen($path3)); + if (FileHelper::isEmptyDirectory($this->path . $group)) { + FileHelper::removeDirectory($this->path . $group); } } } diff --git a/tests/Unit/Collector/.gitignore b/tests/Unit/Collector/.gitignore deleted file mode 100644 index d19657b78..000000000 --- a/tests/Unit/Collector/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/stub/ diff --git a/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php b/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php index cf632672c..28454df7b 100644 --- a/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php +++ b/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php @@ -4,12 +4,17 @@ namespace Yiisoft\Yii\Debug\Tests\Unit\Collector; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Event\ConsoleCommandEvent; +use Symfony\Component\Console\Event\ConsoleErrorEvent; +use Symfony\Component\Console\Event\ConsoleTerminateEvent; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\NullOutput; use Yiisoft\Yii\Console\Event\ApplicationShutdown; use Yiisoft\Yii\Console\Event\ApplicationStartup; use Yiisoft\Yii\Debug\Collector\CollectorInterface; use Yiisoft\Yii\Debug\Collector\Console\ConsoleAppInfoCollector; use Yiisoft\Yii\Debug\Collector\TimelineCollector; -use Yiisoft\Yii\Debug\Collector\Web\WebAppInfoCollector; use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase; use function sleep; @@ -18,12 +23,19 @@ final class ConsoleAppInfoCollectorTest extends AbstractCollectorTestCase { /** - * @param CollectorInterface|WebAppInfoCollector $collector + * @param CollectorInterface|ConsoleAppInfoCollector $collector */ protected function collectTestData(CollectorInterface $collector): void { $collector->collect(new ApplicationStartup(null)); + $command = $this->createMock(Command::class); + $input = new ArrayInput([]); + $output = new NullOutput(); + $collector->collect(new ConsoleCommandEvent(null, $input, $output)); + $collector->collect(new ConsoleErrorEvent($input, $output, new \Exception())); + $collector->collect(new ConsoleTerminateEvent($command, $input, $output, 2)); + DIRECTORY_SEPARATOR === '\\' ? sleep(1) : usleep(123_000); $collector->collect(new ApplicationShutdown(0)); @@ -40,4 +52,9 @@ protected function checkCollectedData(array $data): void $this->assertGreaterThan(0.122, $data['applicationProcessingTime']); } + + protected function checkSummaryData(array $data): void + { + parent::checkSummaryData($data); + } } diff --git a/tests/Unit/Storage/AbstractStorageTest.php b/tests/Unit/Storage/AbstractStorageTest.php index 3ef6695fd..a6df5ab1a 100644 --- a/tests/Unit/Storage/AbstractStorageTest.php +++ b/tests/Unit/Storage/AbstractStorageTest.php @@ -5,8 +5,11 @@ namespace Yiisoft\Yii\Debug\Tests\Unit\Storage; use PHPUnit\Framework\TestCase; +use stdClass; use Yiisoft\Yii\Debug\Collector\CollectorInterface; +use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; use Yiisoft\Yii\Debug\DebuggerIdGenerator; +use Yiisoft\Yii\Debug\Dumper; use Yiisoft\Yii\Debug\Storage\MemoryStorage; use Yiisoft\Yii\Debug\Storage\StorageInterface; @@ -33,15 +36,19 @@ public function testRead(array $data): void { $idGenerator = new DebuggerIdGenerator(); $storage = $this->getStorage($idGenerator); - $collector = $this->createFakeCollector($data); - $storage->addCollector($collector); + $storage->addCollector($this->createFakeCollector($data)); + $storage->addCollector($this->createFakeSummaryCollector($data)); $expectedData = $storage->getData(); + $encodedExpectedData = \json_decode(Dumper::create($expectedData)->asJson(), true); + if (!$storage instanceof MemoryStorage) { $storage->flush(); } - $data = $storage->read(StorageInterface::TYPE_DATA); - $this->assertEquals([$idGenerator->getId() => $expectedData], $data); + + $result = $storage->read(StorageInterface::TYPE_DATA); + $encodedResult = \json_decode(Dumper::create($result)->asJson(), true); + $this->assertEquals([$idGenerator->getId() => $encodedExpectedData], $encodedResult); } /** @@ -60,17 +67,16 @@ public function testFlush(array $data): void abstract public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface; - public function dataProvider(): array + public static function dataProvider(): iterable { - return [ - [[1, 2, 3]], - [['string']], - [[[['', 0, false]]]], - [['test']], - [[false]], - [[null]], - [[0]], - ]; + yield [[1, 2, 3]]; + yield [['string']]; + yield [[[['', 0, false]]]]; + yield [['test']]; + yield [[false]]; + yield [[null]]; + yield [[0]]; + yield [[new stdClass()]]; } protected function createFakeCollector(array $data) @@ -85,4 +91,21 @@ protected function createFakeCollector(array $data) return $collector; } + + protected function createFakeSummaryCollector(array $data) + { + $collector = $this->getMockBuilder(SummaryCollectorInterface::class)->getMock(); + $collector + ->method('getCollected') + ->willReturn($data); + $collector + ->method('getName') + ->willReturn('SummaryMock_Collector'); + + $collector + ->method('getSummary') + ->willReturn(['summary' => 'summary data']); + + return $collector; + } } diff --git a/tests/Unit/Storage/FileStorageTest.php b/tests/Unit/Storage/FileStorageTest.php index 3306a31d2..743034603 100644 --- a/tests/Unit/Storage/FileStorageTest.php +++ b/tests/Unit/Storage/FileStorageTest.php @@ -35,6 +35,33 @@ public function testFlushWithGC(array $data): void $this->assertLessThanOrEqual(5, count($storage->read(StorageInterface::TYPE_SUMMARY, null))); } + /** + * @dataProvider dataProvider() + */ + public function testHistorySize(array $data): void + { + $idGenerator = new DebuggerIdGenerator(); + $idGenerator->reset(); + $storage = $this->getStorage($idGenerator); + $storage->setHistorySize(2); + $collector = $this->createFakeCollector($data); + + $storage->addCollector($collector); + $storage->flush(); + $idGenerator->reset(); + + $storage->addCollector($collector); + $storage->flush(); + $idGenerator->reset(); + + $storage->addCollector($collector); + $storage->flush(); + $idGenerator->reset(); + + $read = $storage->read(StorageInterface::TYPE_SUMMARY, null); + $this->assertEquals(2, count($read)); + } + /** * @dataProvider dataProvider() */ @@ -50,7 +77,7 @@ public function testClear(array $data): void $this->assertDirectoryDoesNotExist($this->path); } - public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface + public function getStorage(DebuggerIdGenerator $idGenerator): FileStorage { return new FileStorage( $this->path, diff --git a/tests/Unit/Storage/MemoryStorageTest.php b/tests/Unit/Storage/MemoryStorageTest.php index e211c8688..98f71de4d 100644 --- a/tests/Unit/Storage/MemoryStorageTest.php +++ b/tests/Unit/Storage/MemoryStorageTest.php @@ -14,4 +14,26 @@ public function getStorage(DebuggerIdGenerator $idGenerator): StorageInterface { return new MemoryStorage($idGenerator); } + + public function testSummaryCount() + { + $idGenerator = new DebuggerIdGenerator(); + $storage = $this->getStorage($idGenerator); + + $storage->addCollector($collector1 = $this->createFakeSummaryCollector(['test' => 'test'])); + $storage->addCollector($collector2 = $this->createFakeCollector(['test' => 'test'])); + + $result = $storage->read(StorageInterface::TYPE_SUMMARY, null); + $this->assertCount(1, $result); + + $this->assertEquals( + [ + $idGenerator->getId() => [ + 'id' => $idGenerator->getId(), + 'collectors' => [$collector1->getName(), $collector2->getName()], + ], + ], + $result + ); + } } From e4cc68d68b26749a4385bc3c4d55ebfae2daf5eb Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 13 Oct 2023 17:16:28 +0000 Subject: [PATCH 12/24] Apply fixes from StyleCI --- tests/Unit/Storage/FileStorageTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Storage/FileStorageTest.php b/tests/Unit/Storage/FileStorageTest.php index 743034603..8bf5a2f37 100644 --- a/tests/Unit/Storage/FileStorageTest.php +++ b/tests/Unit/Storage/FileStorageTest.php @@ -59,7 +59,7 @@ public function testHistorySize(array $data): void $idGenerator->reset(); $read = $storage->read(StorageInterface::TYPE_SUMMARY, null); - $this->assertEquals(2, count($read)); + $this->assertCount(2, $read); } /** From 4b6b3bfb2702895e3d78623328631bb4c58c8d13 Mon Sep 17 00:00:00 2001 From: xepozz Date: Fri, 13 Oct 2023 17:17:08 +0000 Subject: [PATCH 13/24] Apply Rector changes (CI) --- tests/Unit/Storage/AbstractStorageTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Storage/AbstractStorageTest.php b/tests/Unit/Storage/AbstractStorageTest.php index a6df5ab1a..95af83cb4 100644 --- a/tests/Unit/Storage/AbstractStorageTest.php +++ b/tests/Unit/Storage/AbstractStorageTest.php @@ -40,14 +40,14 @@ public function testRead(array $data): void $storage->addCollector($this->createFakeCollector($data)); $storage->addCollector($this->createFakeSummaryCollector($data)); $expectedData = $storage->getData(); - $encodedExpectedData = \json_decode(Dumper::create($expectedData)->asJson(), true); + $encodedExpectedData = \json_decode(Dumper::create($expectedData)->asJson(), true, 512, JSON_THROW_ON_ERROR); if (!$storage instanceof MemoryStorage) { $storage->flush(); } $result = $storage->read(StorageInterface::TYPE_DATA); - $encodedResult = \json_decode(Dumper::create($result)->asJson(), true); + $encodedResult = \json_decode(Dumper::create($result)->asJson(), true, 512, JSON_THROW_ON_ERROR); $this->assertEquals([$idGenerator->getId() => $encodedExpectedData], $encodedResult); } From 162d3bc43cc29b204490e514af9473ca7aed375a Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 20:21:47 +0300 Subject: [PATCH 14/24] Add checks --- tests/Unit/Collector/ConsoleAppInfoCollectorTest.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php b/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php index 28454df7b..da2eb9b85 100644 --- a/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php +++ b/tests/Unit/Collector/ConsoleAppInfoCollectorTest.php @@ -56,5 +56,16 @@ protected function checkCollectedData(array $data): void protected function checkSummaryData(array $data): void { parent::checkSummaryData($data); + + $this->assertArrayHasKey('console', $data); + $this->assertArrayHasKey('php', $data['console']); + $this->assertArrayHasKey('version', $data['console']['php']); + $this->assertArrayHasKey('request', $data['console']); + $this->assertArrayHasKey('startTime', $data['console']['request']); + $this->assertArrayHasKey('processingTime', $data['console']['request']); + $this->assertArrayHasKey('memory', $data['console']); + $this->assertArrayHasKey('peakUsage', $data['console']['memory']); + + $this->assertEquals(PHP_VERSION, $data['console']['php']['version']); } } From 383e6cebf5cb639de3bdc75737e80c681715eab8 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 20:35:01 +0300 Subject: [PATCH 15/24] Cover line --- tests/Unit/Helper/BacktraceIgnoreMatcherTest.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php index 3f1bc79de..b6091494d 100644 --- a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -10,7 +10,7 @@ final class BacktraceIgnoreMatcherTest extends TestCase { - public function testClassIgnorance() + public function testClassIgnorance(): void { $backtrace = debug_backtrace(); @@ -23,7 +23,7 @@ public function testClassIgnorance() $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass($backtrace, [stdClass::class])); } - public function testFileIgnorance() + public function testFileIgnorance(): void { $backtrace = debug_backtrace(); $reflection = new \ReflectionClass(TestCase::class); @@ -41,7 +41,7 @@ public function testFileIgnorance() $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); } - public function testStringMatches() + public function testStringMatches(): void { $this->assertTrue( BacktraceIgnoreMatcher::doesStringMatchPattern( @@ -68,4 +68,11 @@ public function testStringMatches() ) ); } + + public function testEmptyBacktrace(): void + { + $this->assertFalse(BacktraceIgnoreMatcher::doesStringMatchPattern('dev/123/456', [])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile([], ['dev/123/456'])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByClass([], ['dev/123/456'])); + } } From de1c417690434988e696c671c35fc751bfdbb8b9 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 13 Oct 2023 21:21:48 +0300 Subject: [PATCH 16/24] Try fix --- .github/workflows/build.yml | 3 ++- tests/Unit/Helper/BacktraceIgnoreMatcherTest.php | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0d83a43cb..711bc2f64 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,6 +10,7 @@ on: - 'psalm.xml' push: + branches: ['master'] paths-ignore: - 'docs/**' - 'README.md' @@ -29,4 +30,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['8.0', '8.1'] + ['8.0', '8.1', '8.2'] diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php index b6091494d..980d572c4 100644 --- a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -30,15 +30,18 @@ public function testFileIgnorance(): void $file = $reflection->getFileName(); $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote($file)])); - $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote(__FILE__)])); $backtrace[2] = $backtrace[0]; $this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [$file])); $this->assertTrue( - BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [dirname($file) . DIRECTORY_SEPARATOR . '*']) + BacktraceIgnoreMatcher::isIgnoredByFile( + $backtrace, + [preg_quote(dirname($file)) . DIRECTORY_SEPARATOR . '*'] + ) ); - $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [__FILE__])); + $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote(__FILE__)])); } public function testStringMatches(): void From d2791db5ff7d1a667011f22bcb6729af2e8d5812 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 13 Oct 2023 21:23:38 +0300 Subject: [PATCH 17/24] fix ci --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 711bc2f64..69b727dea 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -30,4 +30,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['8.0', '8.1', '8.2'] + ['8.0', '8.1'] From 43d5203c5d0634d43e525366fd496f755b62484b Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 13 Oct 2023 21:24:50 +0300 Subject: [PATCH 18/24] rollback --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 69b727dea..0d83a43cb 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,6 @@ on: - 'psalm.xml' push: - branches: ['master'] paths-ignore: - 'docs/**' - 'README.md' From 01d734e78a7883e09127fe0ea79ccd3c2ae130b2 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 13 Oct 2023 21:28:20 +0300 Subject: [PATCH 19/24] fix --- tests/Unit/Helper/BacktraceIgnoreMatcherTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php index 980d572c4..ae0fbfe75 100644 --- a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -34,7 +34,7 @@ public function testFileIgnorance(): void $backtrace[2] = $backtrace[0]; - $this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [$file])); + $this->assertTrue(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote($file)])); $this->assertTrue( BacktraceIgnoreMatcher::isIgnoredByFile( $backtrace, From 68135424ec75cd9997fde6e432fbd58e8e64bbbc Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Fri, 13 Oct 2023 21:30:56 +0300 Subject: [PATCH 20/24] fix --- tests/Unit/Helper/BacktraceIgnoreMatcherTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php index ae0fbfe75..98231d0f2 100644 --- a/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php +++ b/tests/Unit/Helper/BacktraceIgnoreMatcherTest.php @@ -38,7 +38,7 @@ public function testFileIgnorance(): void $this->assertTrue( BacktraceIgnoreMatcher::isIgnoredByFile( $backtrace, - [preg_quote(dirname($file)) . DIRECTORY_SEPARATOR . '*'] + [preg_quote(dirname($file) . DIRECTORY_SEPARATOR) . '*'] ) ); $this->assertFalse(BacktraceIgnoreMatcher::isIgnoredByFile($backtrace, [preg_quote(__FILE__)])); From a6f920e6eedadba32986d52c06f3490e8a93a802 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 14 Oct 2023 09:54:33 +0300 Subject: [PATCH 21/24] Cover more lines --- src/Dumper.php | 3 +- tests/Support/Stub/ThreeProperties.php | 12 ++++ .../Unit/Collector/ExceptionCollectorTest.php | 9 +++ tests/Unit/DebuggerTest.php | 5 +- tests/Unit/DumperTest.php | 61 +++++++++++++++++++ 5 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 tests/Support/Stub/ThreeProperties.php diff --git a/src/Dumper.php b/src/Dumper.php index 52414990e..72d579b96 100644 --- a/src/Dumper.php +++ b/src/Dumper.php @@ -97,7 +97,7 @@ private function dumpNested($variable, int $depth, int $objectCollapseLevel): mi return $this->dumpNestedInternal($variable, $depth, 0, $objectCollapseLevel); } - private function getObjectProperties($var): array + private function getObjectProperties(object $var): array { if (\__PHP_Incomplete_Class::class !== $var::class && method_exists($var, '__debugInfo')) { $var = $var->__debugInfo(); @@ -158,7 +158,6 @@ private function dumpNestedInternal($var, int $depth, int $level, int $objectCol $objectCollapseLevel ); } - break; case 'resource': case 'resource (closed)': diff --git a/tests/Support/Stub/ThreeProperties.php b/tests/Support/Stub/ThreeProperties.php new file mode 100644 index 000000000..751d47279 --- /dev/null +++ b/tests/Support/Stub/ThreeProperties.php @@ -0,0 +1,12 @@ +assertEquals('test', $exception['message']); $this->assertEquals(777, $exception['code']); } + + public function testNoExceptionCollected() + { + $collector = new ExceptionCollector(new TimelineCollector()); + + $collector->startup(); + + $this->assertEquals([], $collector->getCollected()); + } } diff --git a/tests/Unit/DebuggerTest.php b/tests/Unit/DebuggerTest.php index 31d3941a8..96a9adf7b 100644 --- a/tests/Unit/DebuggerTest.php +++ b/tests/Unit/DebuggerTest.php @@ -88,9 +88,10 @@ public function testIgnoreByEnv(): void $storage = $this->getMockBuilder(StorageInterface::class)->getMock(); $storage->expects($this->never())->method('flush'); - $_ENV['YII_DEBUG_IGNORE'] = 'true'; + putenv('YII_DEBUG_IGNORE=true'); $debugger = new Debugger($idGenerator, $storage, [$collector], []); - $debugger->startup(new ApplicationStartup('')); + $debugger->startup(new ApplicationStartup('command')); + putenv('YII_DEBUG_IGNORE=false'); $debugger->shutdown(); } diff --git a/tests/Unit/DumperTest.php b/tests/Unit/DumperTest.php index 0f5b6d81f..1ae7fd72d 100644 --- a/tests/Unit/DumperTest.php +++ b/tests/Unit/DumperTest.php @@ -8,6 +8,7 @@ use stdClass; use Yiisoft\Yii\Debug as D; use Yiisoft\Yii\Debug\Dumper; +use Yiisoft\Yii\Debug\Tests\Support\Stub\ThreeProperties; final class DumperTest extends TestCase { @@ -60,6 +61,57 @@ public function testAsJson($variable, string $result): void $this->assertEqualsWithoutLE($result, $output); } + public function testDeepNestedArray(): void + { + $variable = [[[[[['test']]]]]]; + $output = Dumper::create($variable)->asJson(2); + $result = '[["array [...]"]]'; + $this->assertEqualsWithoutLE($result, $output); + } + + public function testDeepNestedObject(): void + { + $object = new ThreeProperties(); + $object->first = $object; + $variable = [[$object]]; + + $output = Dumper::create($variable)->asJson(2); + $result = sprintf( + '[["%s#%d (...)"]]', + str_replace('\\', '\\\\', ThreeProperties::class), + spl_object_id($object), + ); + $this->assertEqualsWithoutLE($result, $output); + } + + public function testObjectVisibilityProperties(): void + { + $variable = new ThreeProperties(); + + $output = Dumper::create($variable)->asJson(2); + $result = sprintf( + '{"%s#%d":{"public $first":"first","protected $second":"second","private $third":"third"}}', + str_replace('\\', '\\\\', ThreeProperties::class), + spl_object_id($variable), + ); + $this->assertEqualsWithoutLE($result, $output); + } + + public function testFormatJson(): void + { + $variable = [['test']]; + + $output = Dumper::create($variable)->asJson(2, true); + $result = <<assertEqualsWithoutLE($result, $output); + } + public static function jsonDataProvider(): iterable { $emptyObject = new stdClass(); @@ -276,6 +328,15 @@ public static function jsonDataProvider(): iterable '"{closed resource}"', ]; + $socketResource = \socket_create(\AF_INET, \SOCK_STREAM, \SOL_TCP); + $socketResourceId = spl_object_id($socketResource); + yield 'socket resource' => [ + $socketResource, + << [ From 1ed2ddff9cb823096b353fc25ca5f628726a683c Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sat, 14 Oct 2023 10:12:08 +0300 Subject: [PATCH 22/24] Enhance command test --- tests/Support/Application/config/.merge-plan.php | 2 ++ tests/Support/Application/config/param1.php | 13 +++++++++++++ tests/Unit/Command/DebugContainerCommandTest.php | 7 ++++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/Support/Application/config/param1.php diff --git a/tests/Support/Application/config/.merge-plan.php b/tests/Support/Application/config/.merge-plan.php index 957a4471c..f60be9931 100644 --- a/tests/Support/Application/config/.merge-plan.php +++ b/tests/Support/Application/config/.merge-plan.php @@ -4,6 +4,8 @@ return [ '/'=>[ + 'params' => [ + ] ], ]; diff --git a/tests/Support/Application/config/param1.php b/tests/Support/Application/config/param1.php new file mode 100644 index 000000000..c1afe1996 --- /dev/null +++ b/tests/Support/Application/config/param1.php @@ -0,0 +1,13 @@ +[ + 'params' => [ + 'yiitest/yii-debug' => [ + 'param1.php' + ] + ] + ], +]; diff --git a/tests/Unit/Command/DebugContainerCommandTest.php b/tests/Unit/Command/DebugContainerCommandTest.php index dc039de5d..699bd8fe2 100644 --- a/tests/Unit/Command/DebugContainerCommandTest.php +++ b/tests/Unit/Command/DebugContainerCommandTest.php @@ -29,11 +29,16 @@ public function testCommand() $storage->expects($this->never())->method('clear'); $debugger = new Debugger($idGenerator, $storage, []); - $command = new DebugContainerCommand($container, $debugger); + $config = $container->get(ConfigInterface::class); + // trigger config build + $config->get('params'); + $command = new DebugContainerCommand($container, $debugger); $commandTester = new CommandTester($command); $commandTester->execute([]); + + $this->assertEquals(0, $commandTester->getStatusCode()); } private function createContainer(): ContainerInterface From 24d30259f7c91b06ca1c3a9a5df0a09749753f87 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sat, 14 Oct 2023 07:13:05 +0000 Subject: [PATCH 23/24] Apply fixes from StyleCI --- tests/Support/Application/config/param1.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Support/Application/config/param1.php b/tests/Support/Application/config/param1.php index c1afe1996..c9d956327 100644 --- a/tests/Support/Application/config/param1.php +++ b/tests/Support/Application/config/param1.php @@ -3,11 +3,11 @@ declare(strict_types=1); return [ - '/'=>[ + '/' => [ 'params' => [ 'yiitest/yii-debug' => [ - 'param1.php' - ] - ] + 'param1.php', + ], + ], ], ]; From 6ccfbd1923f23c51f194c864032d2ac66176aaca Mon Sep 17 00:00:00 2001 From: xepozz Date: Sat, 14 Oct 2023 07:13:50 +0000 Subject: [PATCH 24/24] Apply Rector changes (CI) --- tests/Support/Stub/ThreeProperties.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/Stub/ThreeProperties.php b/tests/Support/Stub/ThreeProperties.php index 751d47279..4dd455b62 100644 --- a/tests/Support/Stub/ThreeProperties.php +++ b/tests/Support/Stub/ThreeProperties.php @@ -8,5 +8,5 @@ class ThreeProperties { public $first = 'first'; protected $second = 'second'; - private $third = 'third'; + private string $third = 'third'; }