Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests #227

Merged
merged 26 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Collector/Console/ConsoleAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getCollected(): array

public function collect(object $event): void
{
if (!is_object($event) || !$this->isActive()) {
if (!$this->isActive()) {
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Collector/ExceptionCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function __construct(private TimelineCollector $timelineCollector)

public function getCollected(): array
{
if (!$this->isActive()) {
return [];
}
if ($this->exception === null) {
return [];
}
Expand All @@ -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,
Expand Down
56 changes: 31 additions & 25 deletions src/Collector/HttpClientCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)
)
)
),
],
];
}
}
4 changes: 1 addition & 3 deletions src/Collector/VarDumperCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public function getCollected(): array
return [];
}

return [
'var-dumper' => $this->vars,
];
return $this->vars;
}

public function getSummary(): array
Expand Down
4 changes: 1 addition & 3 deletions src/Collector/Web/WebAppInfoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,7 +42,7 @@ public function getCollected(): array

public function collect(object $event): void
{
if (!is_object($event) || !$this->isActive()) {
if (!$this->isActive()) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -158,7 +158,6 @@ private function dumpNestedInternal($var, int $depth, int $level, int $objectCol
$objectCollapseLevel
);
}

break;
case 'resource':
case 'resource (closed)':
Expand Down
37 changes: 19 additions & 18 deletions src/Storage/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
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
2 changes: 2 additions & 0 deletions tests/Support/Application/config/.merge-plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

return [
'/'=>[
'params' => [

]
],
];
13 changes: 13 additions & 0 deletions tests/Support/Application/config/param1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

return [
'/' => [
'params' => [
'yiitest/yii-debug' => [
'param1.php',
],
],
],
];
12 changes: 12 additions & 0 deletions tests/Support/Stub/ThreeProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Debug\Tests\Support\Stub;

class ThreeProperties
{
public $first = 'first';
protected $second = 'second';
private string $third = 'third';
}
1 change: 0 additions & 1 deletion tests/Unit/Collector/.gitignore

This file was deleted.

32 changes: 30 additions & 2 deletions tests/Unit/Collector/ConsoleAppInfoCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand All @@ -40,4 +52,20 @@ protected function checkCollectedData(array $data): void

$this->assertGreaterThan(0.122, $data['applicationProcessingTime']);
}

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']);
}
}
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());
}
}
Loading