Skip to content
Open
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
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
"Murtukov\\PHPCodeGenerator\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"require-dev": {
"phpstan/phpstan": "^1.12.4",
"phpunit/phpunit": "^10",
Expand Down
4 changes: 4 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ private static function stringifyValue(mixed $value, bool $topLevel = false): st

case 'object':
if (!$value instanceof GeneratorInterface) {
if (enum_exists($value::class)) {
return '\\'.$value::class.'::'.$value->name;
}

try {
$result = json_encode($value->__toString());

Expand Down
9 changes: 9 additions & 0 deletions tests/Fixtures/BasicEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Tests\Fixtures;

enum BasicEnum
{
case ONE;
case TWO;
}
9 changes: 9 additions & 0 deletions tests/Fixtures/StringBackedEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Tests\Fixtures;

enum StringBackedEnum: string
{
case ONE = 'one';
case TWO = 'two';
}
18 changes: 18 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Murtukov\PHPCodeGenerator\Utils;
use PHPUnit\Framework\TestCase;
use Tests\Fixtures\BasicEnum;
use Tests\Fixtures\StringBackedEnum;

class UtilsTest extends TestCase
{
Expand All @@ -26,6 +28,22 @@ public function __toString(): string
Utils::stringify(new stdClass());
}

/**
* @test
*/
public function stringifyEnum(): void
{
$this->assertEquals('\\Tests\\Fixtures\\BasicEnum::ONE', Utils::stringify(BasicEnum::ONE));
}

/**
* @test
*/
public function stringifyBackedEnum(): void
{
$this->assertEquals('\\Tests\\Fixtures\\StringBackedEnum::ONE', Utils::stringify(StringBackedEnum::ONE));
}

/**
* @test
*/
Expand Down