Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Oct 13, 2023
1 parent 71ac2a8 commit b070f11
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 12 deletions.
12 changes: 12 additions & 0 deletions tests/Shared/AbstractCollectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 13 additions & 2 deletions tests/Unit/Collector/ContainerInterfaceProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/Collector/EventDispatcherInterfaceProxyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Tests\Unit\Collector;

use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use stdClass;
use Yiisoft\Yii\Debug\Collector\EventCollector;
use Yiisoft\Yii\Debug\Collector\EventDispatcherInterfaceProxy;
use Yiisoft\Yii\Debug\Collector\TimelineCollector;

final class EventDispatcherInterfaceProxyTest extends TestCase
{
public function testDispatch()
{
$event = new stdClass();
$collector = new EventCollector(new TimelineCollector());
$collector->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());
}
}
19 changes: 9 additions & 10 deletions tests/Unit/Collector/LoggerProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']];
}
}
44 changes: 44 additions & 0 deletions tests/Unit/Collector/TimelineCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Tests\Unit\Collector;

use Yiisoft\Yii\Debug\Collector\CollectorInterface;
use Yiisoft\Yii\Debug\Collector\LogCollector;
use Yiisoft\Yii\Debug\Collector\TimelineCollector;
use Yiisoft\Yii\Debug\Tests\Shared\AbstractCollectorTestCase;

final class TimelineCollectorTest extends AbstractCollectorTestCase
{
/**
* @param CollectorInterface|TimelineCollector $collector
*/
protected function collectTestData(CollectorInterface $collector): void
{
$collector->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]);
}
}

0 comments on commit b070f11

Please sign in to comment.