Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Jan 12, 2024
1 parent a0c6e60 commit 0c4bc2f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function depth(int $depth): self
* The counter isn't incremented if the dump is not sent (any other condition is not met).
* It might be useful for debugging in loops, recursive or just multiple function calls.
*
* @param positive-int $times
* @param positive-int $times Zero means no limit.
* @param bool $fullStack If true, the counter is incremented for each stack trace, not for the line.
*/
public function times(int $times, bool $fullStack = false): self
Expand Down Expand Up @@ -145,6 +145,7 @@ private function haveToSend(): bool
}

if ($this->times > 0) {
\assert($this->timesCounterKey !== '');
return Counter::checkAndIncrement($this->timesCounterKey, $this->times);
}

Expand Down
14 changes: 13 additions & 1 deletion src/Client/TrapHandle/Counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

namespace Buggregator\Trap\Client\TrapHandle;

/**
* Static counter for {@see TrapHandle::once()} and {@see TrapHandle::times()} methods.
*
* @internal
* @psalm-internal Buggregator\Trap\Client
*/
final class Counter
{
/** @var array<string, int<0, max>> */
/** @var array<non-empty-string, int<0, max>> */
private static array $counters = [];

/**
* Returns true if the counter of related stack trace is less than $times. In this case, the counter is incremented.
*
* @param non-empty-string $key
* @param int<0, max> $times
*/
public static function checkAndIncrement(string $key, int $times): bool
Expand All @@ -25,4 +32,9 @@ public static function checkAndIncrement(string $key, int $times): bool

return false;
}

public static function clear(): void
{
self::$counters = [];
}
}
12 changes: 5 additions & 7 deletions tests/Unit/Client/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@

namespace Buggregator\Trap\Tests\Unit\Client;

use Buggregator\Trap\Client\TrapHandle\Counter;
use Buggregator\Trap\Client\TrapHandle\Dumper;
use Closure;
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Caster\ReflectionCaster;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;

class Base extends TestCase
{
protected static Data $lastData;
protected static ?Data $lastData = null;
protected static ?Closure $handler = null;

protected function setUp(): void
{
$cloner = new VarCloner();
/** @psalm-suppress InvalidArgument */
$cloner->addCasters(ReflectionCaster::UNSET_CLOSURE_FILE_INFO);
Counter::clear();
$dumper = $this->getMockBuilder(DataDumperInterface::class)
->getMock();
$dumper->expects($this->once())
$dumper->expects($this->atLeastOnce())
->method('dump')
->with(
$this->callback(static function (Data $data): bool {
Expand All @@ -41,6 +38,7 @@ protected function setUp(): void

protected function tearDown(): void
{
Counter::clear();
Dumper::setDumper(null);
parent::tearDown();
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/Client/FunctionTrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Tests\Unit\Client;

use PHPUnit\Framework\TestCase;

final class FunctionTrapTest extends TestCase
{
/**
* @runInSeparateProcess
*/
public function testLeak(): void
{
$object = new \stdClass();
$ref = \WeakReference::create($object);

\trap($object, $object);
unset($object);

$this->assertNull($ref->get());
}
}
46 changes: 46 additions & 0 deletions tests/Unit/Client/TrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public function testLabel(): void
$this->assertSame('FooName', static::$lastData->getContext()['label']);
}

/**
* Check the first line of dumped stacktrace string contains right file and line.
*/
public function testStackTrace(): void
{
$line = __FILE__ . ':' . __LINE__ and trap();
Expand All @@ -22,4 +25,47 @@ public function testStackTrace(): void

$this->assertStringContainsString($line, $neededLine);
}

/**
* After calling {@see trap()} the dumped data isn't stored in the memory.
*/
public function testLeak(): void
{
$object = new \stdClass();
$ref = \WeakReference::create($object);

\trap($object, $object);
unset($object);

$this->assertNull($ref->get());
}

public function testTrapOnce(): void
{
foreach ([false, true, true, true, true] as $isNull) {
\trap(42)->once();
self::assertSame($isNull, static::$lastData === null);
static::$lastData = null;
}
}

public static function provideTrapTimes(): iterable
{
yield 'no limit' => [0, [false, false, false, false, false]];
yield 'once' => [1, [false, true, true, true, true, true]];
yield 'twice' => [2, [false, false, true, true, true]];
yield 'x' => [10, [false, false, false, false, false, false, false, false, false, false, true, true, true]];
}

/**
* @dataProvider provideTrapTimes
*/
public function testTrapTimes(int $times, array $sequence): void
{
foreach ($sequence as $isNull) {
\trap(42)->times($times);
self::assertSame($isNull, static::$lastData === null);
static::$lastData = null;
}
}
}

0 comments on commit 0c4bc2f

Please sign in to comment.