From b070f11910eb9a3f96a33a28e2babea24150d8d8 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 13 Oct 2023 10:05:34 +0300 Subject: [PATCH] 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 80ac2cd6..25dd6c63 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 b38e7bde..cf27b2a6 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 00000000..be091d9f --- /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 a5a09eef..ba9c2f66 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 00000000..faf42052 --- /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]); + } +}