Skip to content

Commit

Permalink
Test enum generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Jan 10, 2025
1 parent f8bfab1 commit cd288a6
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 1 deletion.
40 changes: 40 additions & 0 deletions tests/Feature/EnumMakeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Cerbero\Enum\Enums;

it('fails if the enum name is not provided', function() {
expect(runEnum('make'))
->output->toContain('The name of the enum is missing.')
->status->toBe(1);
});

it('succeeds if an enum already exists', function() {
expect(runEnum('make App/Enums/Enum1'))
->output->toContain('The enum App\Enums\Enum1 already exists.')
->status->toBe(0);
});

it('fails if the enum cases are not provided', function() {
expect(runEnum('make App/Enums/Test'))
->output->toContain('The cases of the enum are missing.')
->status->toBe(1);
});

it('fails if the backed option is not supported', function() {
expect(runEnum('make App/Enums/Test one --backed=test'))
->output->toContain('The option --backed supports only')
->status->toBe(1);
});

it('generates annotated enums', function(string $enum, string $backed) {
Enums::setBasePath(__DIR__ . '/../Skeleton');
Enums::setPaths('app/Enums', 'domain/*/Enums');

expect(fn() => runEnum("make \"{$enum}\" CaseOne CaseTwo --backed={$backed}"))->toGenerate($enum);

(fn() => self::$paths = [])->bindTo(null, Enums::class)();
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
})->with([
['App\Enums\Generated1', 'bitwise'],
['Domain\Common\Enums\Generated2', 'snake'],
]);
39 changes: 39 additions & 0 deletions tests/Integration/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Cerbero\Enum\Enums;
use Cerbero\Enum\Services\Generator;

it('returns true if an enum already exists', function() {
$outcome = (new Generator('App\Enums\Enum1', []))->generate();

expect($outcome)->toBeTrue();
});

it('generates annotated enums', function(string $enum, ?string $backed) {
Enums::setBasePath(__DIR__ . '/../Skeleton');
Enums::setPaths('app/Enums', 'domain/*/Enums');

expect(fn() => (new Generator($enum, ['CaseOne', 'CaseTwo'], $backed))->generate())->toGenerate($enum);

(fn() => self::$paths = [])->bindTo(null, Enums::class)();
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
})->with([
['App\Enums\Generated1', 'bitwise'],
['Domain\Common\Enums\Generated2', 'snake'],
]);

it('creates sub-directories if needed', function() {
Enums::setBasePath(__DIR__ . '/../Skeleton');
Enums::setPaths('app/Enums', 'domain/*/Enums');

$enum = 'SubDirectory\Generated3';

try {
expect(fn() => (new Generator($enum, ['CaseOne', 'CaseTwo']))->generate())->toGenerate($enum);
} finally {
rmdir(Enums::basePath('SubDirectory'));
}

(fn() => self::$paths = [])->bindTo(null, Enums::class)();
(fn() => self::$basePath = null)->bindTo(null, Enums::class)();
});
29 changes: 28 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
|
*/

use function Cerbero\Enum\className;
use function Cerbero\Enum\cli;
use function Cerbero\Enum\namespaceToPath;

expect()->extend('toAnnotate', function (array $enums, bool $overwrite = false) {
$oldContents = [];
Expand All @@ -45,7 +47,7 @@
}

foreach ($oldContents as $filename => $oldContent) {
$stub = __DIR__ . '/stubs/' . basename($filename, '.php') . '.stub';
$stub = __DIR__ . '/stubs/annotate/' . basename($filename, '.php') . '.stub';

if ($overwrite && file_exists($path = __DIR__ . '/stubs/' . basename($filename, '.php') . '.force.stub')) {
$stub = $path;
Expand All @@ -64,6 +66,31 @@
}
});

expect()->extend('toGenerate', function (string $enum) {
expect(class_exists($enum))->toBeFalse();

try {
if (is_bool($value = ($this->value)())) {
expect($value)->toBeTrue();
} else {
expect($value)
->output->toContain($enum)
->status->toBe(0);
}

$filename = namespaceToPath($enum);
$stub = sprintf('%s/stubs/%s/%s.stub', __DIR__, is_bool($value) ? 'generator' : 'make', className($enum));

// normalize content to avoid end-of-line incompatibility between OS
$enumContent = str_replace("\r\n", "\n", file_get_contents($filename));
$stubContent = str_replace("\r\n", "\n", file_get_contents($stub));

expect($enumContent)->toBe($stubContent);
} finally {
file_exists($filename) && unlink($filename);
}
});

/*
|--------------------------------------------------------------------------
| Functions
Expand Down
16 changes: 16 additions & 0 deletions tests/stubs/generator/Generated1.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use Cerbero\Enum\Concerns\Enumerates;

enum Generated1: int
{
use Enumerates;

case CaseOne = 1 << 0;

case CaseTwo = 1 << 1;
}
16 changes: 16 additions & 0 deletions tests/stubs/generator/Generated2.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Domain\Common\Enums;

use Cerbero\Enum\Concerns\Enumerates;

enum Generated2: string
{
use Enumerates;

case CaseOne = 'case_one';

case CaseTwo = 'case_two';
}
16 changes: 16 additions & 0 deletions tests/stubs/generator/Generated3.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace SubDirectory;

use Cerbero\Enum\Concerns\Enumerates;

enum Generated3
{
use Enumerates;

case CaseOne;

case CaseTwo;
}
20 changes: 20 additions & 0 deletions tests/stubs/make/Generated1.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Enums;

use Cerbero\Enum\Concerns\Enumerates;

/**
* @method static int CaseOne()
* @method static int CaseTwo()
*/
enum Generated1: int
{
use Enumerates;

case CaseOne = 1 << 0;

case CaseTwo = 1 << 1;
}
20 changes: 20 additions & 0 deletions tests/stubs/make/Generated2.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Domain\Common\Enums;

use Cerbero\Enum\Concerns\Enumerates;

/**
* @method static string CaseOne()
* @method static string CaseTwo()
*/
enum Generated2: string
{
use Enumerates;

case CaseOne = 'case_one';

case CaseTwo = 'case_two';
}

0 comments on commit cd288a6

Please sign in to comment.