Skip to content
Merged
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
14 changes: 10 additions & 4 deletions src/Commands/EnumAnnotateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Datomatic\LaravelEnumHelper\Commands;

use Composer\ClassMapGenerator\ClassMapGenerator;
use Datomatic\LaravelEnumHelper\LaravelEnumHelper;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Filesystem\Filesystem;
Expand Down Expand Up @@ -52,13 +53,13 @@ protected function annotateFolder(): int

if (count($searchDirectoryMap) > 0) {
foreach ($searchDirectoryMap as $class => $_) {
if(!enum_exists($class)){
if (! enum_exists($class)) {
continue;
}

$reflection = new ReflectionEnum($class);

if ($reflection->isSubclassOf(UnitEnum::class)) {
if ($reflection->isSubclassOf(UnitEnum::class) && $this->usesEnumHelperTrait($class)) {
$this->annotate($reflection);
}
}
Expand All @@ -71,6 +72,11 @@ protected function annotateFolder(): int
return self::FAILURE;
}

protected function usesEnumHelperTrait(string $className): bool
{
return in_array(LaravelEnumHelper::class, class_uses_recursive($className));
}

/**
* @throws ReflectionException|FileNotFoundException
*/
Expand All @@ -84,9 +90,9 @@ protected function annotateClass(string $className): int
}

$reflection = new ReflectionEnum($className);

if ($reflection->isSubclassOf(UnitEnum::class)) {
if (class_uses_recursive($class)['Datomatic\LaravelEnumHelper\LaravelEnumHelper'] ?? false) {
if ($this->usesEnumHelperTrait($className)) {
$this->annotate($reflection);
}
}
Expand Down
10 changes: 9 additions & 1 deletion tests/Feature/EnumAnnotateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare(strict_types=1);

use Datomatic\LaravelEnumHelper\Tests\Support\Enums\DoesntUseEnumHelperTrait;

beforeEach(function () {
if (! file_exists(app_path('Enums'))) {
mkdir(app_path('Enums'), 0755, true);
Expand Down Expand Up @@ -43,7 +45,6 @@
$this->assertEquals(1, substr_count($contents, '@method static string noResponse()'));
});


it('can be success single file int backed enum', function () {
$this->artisan("enum:annotate --folder={$this->withoutDocBlockEnumsFolder} Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\WithoutDocBlockEnums\\\\StatusIntWithoutDocBlock")
->assertSuccessful();
Expand Down Expand Up @@ -96,6 +97,13 @@
$this->assertEquals(1, substr_count($contents, '@method static string noResponse()'));
});

it('doesnt annotate enums that dont use LaravelEnumTrait', function () {
$this->artisan('enum:annotate Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\Enums\\\\DoesntUseEnumHelperTrait')
->assertSuccessful();
$e = new ReflectionEnum(DoesntUseEnumHelperTrait::class);
$this->assertEquals(false, $e->getDocComment());
});

it('can be failed with class', function () {
$this->artisan('enum:annotate Datomatic\\\\LaravelEnumHelper\\\\Tests\\\\Support\\\\NotEnums\\\\TestClass')
->assertFailed();
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/LaravelEnumHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,13 @@
'translations with lang and cases param' => [Status::class, [Status::ACCEPTED, Status::NO_RESPONSE], 'it', ['ITA news ACCEPTED', 'ITA news NO_RESPONSE']],
]);

//it('can\'t return an array of method results with method name both singular and plural', function ($enumClass, $cases, $lang) {
// it('can\'t return an array of method results with method name both singular and plural', function ($enumClass, $cases, $lang) {
// expect(fn() => $enumClass::news($cases, $lang))->toRuntimeError();
//})->with([
// })->with([
// 'enum with method' => [StatusString::class, null, null],
// 'enum with method with cases param' => [StatusString::class, [StatusString::DISCARDED, StatusString::ACCEPTED], null],
// 'enum with method with lang and cases param' => [StatusString::class, [StatusString::NO_RESPONSE, StatusString::ACCEPTED], 'it'],
//]);
// ]);

it('can return an array of translations with magic method', function ($enumClass, $cases, $lang, $result) {
expect($enumClass::excerpts($cases, $lang))->toBe($result);
Expand Down
11 changes: 11 additions & 0 deletions tests/Support/Enums/DoesntUseEnumHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Datomatic\LaravelEnumHelper\Tests\Support\Enums;

enum DoesntUseEnumHelperTrait
{
case FOO;
case BAR;
}
10 changes: 10 additions & 0 deletions tests/stubs/DoesntUseEnumHelperTrait.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Datomatic\LaravelEnumHelper\Tests\Support\Enums;

enum DoesntUseEnumHelperTrait {
case FOO;
case BAR;
}
Loading