Skip to content

Commit

Permalink
test: cover tr() function
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed May 28, 2024
1 parent 3dc0c4f commit cd02bef
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
<code><![CDATA[new \SplQueue()]]></code>
</MixedPropertyTypeCoercion>
</file>
<file src="src/Support/ProtobufCaster.php">
<file src="src/Support/Caster/ProtobufCaster.php">
<MixedArgument>
<code><![CDATA[$ed->getClass()]]></code>
<code><![CDATA[$value]]></code>
Expand Down
9 changes: 4 additions & 5 deletions tests/Unit/Client/FunctionTrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

namespace Buggregator\Trap\Tests\Unit\Client;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use PHPUnit\Framework\TestCase;

final class FunctionTrapTest extends TestCase
{
/**
* @runInSeparateProcess
*
* @group phpunit-only
*/
#[RunInSeparateProcess]
#[Group('phpunit-only')]
public function testLeak(): void
{
$object = new \stdClass();
Expand Down
77 changes: 77 additions & 0 deletions tests/Unit/Client/TrTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Buggregator\Trap\Tests\Unit\Client;

use Buggregator\Trap\Client\TrapHandle\Dumper;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;

final class TrTest extends Base
{
public function testLabel(): void
{
tr(FooName: 'foo-value');
self::assertSame('FooName', self::$lastData->getContext()['label']);
}

/**
* Check the stacktrace contains three items and it begins with the right function name.
*/
#[RunInSeparateProcess]
#[Group('phpunit-only')]
public function testTrAsStackTrace(): void
{
tr();

// 3 Stack Trace items
self::assertCount(3, self::$lastData->getValue());
self::assertStringMatchesFormat('Trace #0 -.--- %fM', self::$lastData->getType());
$firstLineKey = \array_key_first(self::$lastData->getValue());
self::assertStringContainsString('tr()', $firstLineKey);

tr();

self::assertCount(3, self::$lastData->getValue());
self::assertStringMatchesFormat('Trace #1 +%fms %fM', self::$lastData->getType());
$firstLineKey = \array_key_first(self::$lastData->getValue());
self::assertStringContainsString('tr()', $firstLineKey);
}

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

tr($object, $object);
unset($object);

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

public function testReturn(): void
{
$this->assertSame(42, tr(42));
$this->assertSame(42, tr(named: 42));
$this->assertSame(42, tr(42, 43));
$this->assertSame('foo', tr(...['0' => 'foo', 42 => 90]));
$this->assertNull(tr(null));
}

public function testReturnSendsDumpOnce(): void
{
$dumper = $this->getMockBuilder(DataDumperInterface::class)
->getMock();
$dumper->expects($this->once())
->method('dump')
->willReturnArgument(1);
Dumper::setDumper($dumper);

$this->assertSame(42, tr(42));
}
}

0 comments on commit cd02bef

Please sign in to comment.