Skip to content

Commit

Permalink
test: cover Trace and TraceFile
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 28, 2024
1 parent 1414261 commit 5ca4f9c
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Client/Caster/Trace.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Trace
/**
* @param int<0, max> $number The tick number.
* @param float $delta The time delta between the current and previous tick.
* @param int $memory The memory usage.
* @param int<0, max> $memory The memory usage.
* @param list<array{
* function: non-empty-string,
* line?: int,
Expand All @@ -42,7 +42,7 @@ public function __toString(): string
$delta < 0.01 => \sprintf('+%.2fms', $delta * 1000),
$delta < 1 => \sprintf('+%.1fms', ($delta * 1000)),
$delta < 10 => \sprintf('+%.2fs', $delta),
$delta < 60 => \sprintf('+%.3fs', $delta),
$delta < 60 => \sprintf('+%.1fs', $delta),
default => \sprintf('+%dm %ds', (int) $delta % 60, (int) $delta % 60),
};

Expand Down
2 changes: 2 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ function tr(mixed ...$values): mixed
* When no arguments passed, it works like {@see tr()}.
*
* @param mixed ...$values
*
* @codeCoverageIgnore
*/
function td(mixed ...$values): never
{
Expand Down
43 changes: 43 additions & 0 deletions tests/Unit/Client/Caster/TraceFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Tests\Unit\Client\Caster;

use PHPUnit\Framework\TestCase;

final class TraceFileTest extends TestCase
{
public function testToString(): void
{
$traceFile = new \Buggregator\Trap\Client\Caster\TraceFile([
'function' => 'foo',
'line' => 42,
'file' => '/path/to/file.php',
'class' => 'Foo',
'type' => '->',
'args' => ['bar'],
]);

self::assertSame('file.php:42', (string) $traceFile);
}

public function testToStringWithoutFile(): void
{
$traceFile = new \Buggregator\Trap\Client\Caster\TraceFile([
'function' => 'foo',
]);

self::assertSame('<internal>', (string) $traceFile);
}

public function testToStringWithoutLine(): void
{
$traceFile = new \Buggregator\Trap\Client\Caster\TraceFile([
'function' => 'foo',
'file' => '/path/to/file.php',
]);

self::assertSame('<internal>', (string) $traceFile);
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Client/Caster/TraceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Tests\Unit\Client\Caster;

use Buggregator\Trap\Client\Caster\Trace;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class TraceTest extends TestCase
{
public static function provideToString(): iterable
{
yield [0, 0.0, 102400, 'Trace #0 -.--- 0.10M'];
yield [5, 0.000123, 123456, 'Trace #5 +0.123ms 0.12M'];
yield [4, 0.00123, 123456, 'Trace #4 +1.23ms 0.12M'];
yield [3, 0.123, 123456, 'Trace #3 +123.0ms 0.12M'];
yield [2, 1.23, 123456, 'Trace #2 +1.23s 0.12M'];
yield [1, 42.3, 123456, 'Trace #1 +42.3s 0.12M'];
yield [42, 10050.0, 0, 'Trace #42 +30m 30s 0.00M'];
}

/**
* @param int<0, max> $number
* @param int<0, max> $memory
* @param non-empty-string $result
*/
#[DataProvider('provideToString')]
public function testToString(int $number, float $delta, int $memory, string $result): void
{
$trace = new Trace(
number: $number,
delta: $delta,
memory: $memory,
stack: [
[
'function' => 'foo',
'line' => 42,
'file' => '/path/to/file.php',
'class' => 'Foo',
'type' => '->',
'args' => ['bar'],
],
[
'function' => 'bar',
'line' => 23,
'file' => '/path/to/file.php',
'class' => 'Bar',
'type' => '::',
'args' => ['baz'],
],
],
);

self::assertSame($result, (string) $trace);
}
}

0 comments on commit 5ca4f9c

Please sign in to comment.