From cd02befbe1b229d623b26b09b97b30e121e1bcfc Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Tue, 28 May 2024 13:21:28 +0400 Subject: [PATCH] test: cover `tr()` function --- composer.lock | 12 ++-- psalm-baseline.xml | 2 +- tests/Unit/Client/FunctionTrapTest.php | 9 ++- tests/Unit/Client/TrTest.php | 77 ++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 tests/Unit/Client/TrTest.php diff --git a/composer.lock b/composer.lock index 46fd5f80..44e686df 100644 --- a/composer.lock +++ b/composer.lock @@ -1592,16 +1592,16 @@ }, { "name": "composer/pcre", - "version": "3.1.3", + "version": "3.1.4", "source": { "type": "git", "url": "https://github.com/composer/pcre.git", - "reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8" + "reference": "04229f163664973f68f38f6f73d917799168ef24" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/pcre/zipball/5b16e25a5355f1f3afdfc2f954a0a80aec4826a8", - "reference": "5b16e25a5355f1f3afdfc2f954a0a80aec4826a8", + "url": "https://api.github.com/repos/composer/pcre/zipball/04229f163664973f68f38f6f73d917799168ef24", + "reference": "04229f163664973f68f38f6f73d917799168ef24", "shasum": "" }, "require": { @@ -1643,7 +1643,7 @@ ], "support": { "issues": "https://github.com/composer/pcre/issues", - "source": "https://github.com/composer/pcre/tree/3.1.3" + "source": "https://github.com/composer/pcre/tree/3.1.4" }, "funding": [ { @@ -1659,7 +1659,7 @@ "type": "tidelift" } ], - "time": "2024-03-19T10:26:25+00:00" + "time": "2024-05-27T13:40:54+00:00" }, { "name": "composer/semver", diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 5e64aac1..8a54c96c 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -204,7 +204,7 @@ - + getClass()]]> diff --git a/tests/Unit/Client/FunctionTrapTest.php b/tests/Unit/Client/FunctionTrapTest.php index 1a471215..e3cd7399 100644 --- a/tests/Unit/Client/FunctionTrapTest.php +++ b/tests/Unit/Client/FunctionTrapTest.php @@ -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(); diff --git a/tests/Unit/Client/TrTest.php b/tests/Unit/Client/TrTest.php new file mode 100644 index 00000000..5bb22e03 --- /dev/null +++ b/tests/Unit/Client/TrTest.php @@ -0,0 +1,77 @@ +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)); + } +}