Skip to content

Commit

Permalink
Javascript functionality and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonferens committed Oct 22, 2024
1 parent a15b7d4 commit 24c3c26
Show file tree
Hide file tree
Showing 13 changed files with 526 additions and 70 deletions.
68 changes: 68 additions & 0 deletions build/report.junit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="/Users/brandon/Code/paragon/phpunit.xml.dist" tests="41" assertions="104" errors="0" failures="0" skipped="0" time="0.433768">
<testsuite name="Paragon Test Suite" tests="41" assertions="104" errors="0" failures="0" skipped="0" time="0.433768">
<testsuite name="Tests\Architecture\CommandsTest" file="tests/Architecture/CommandsTest.php" tests="4" assertions="4" errors="0" failures="0" skipped="0" time="0.088649">
<testcase name="expect 'Kirschbaum\Paragon\Commands' → classes → toHaveSuffix 'Command'" file="tests/Architecture/CommandsTest.php::expect 'Kirschbaum\Paragon\Commands' → classes → toHaveSuffix 'Command'" class="Tests\Architecture\CommandsTest" classname="Tests.Architecture.CommandsTest" assertions="1" time="0.071571"/>
<testcase name="expect 'Kirschbaum\Paragon\Commands' → toHaveAttribute 'Symfony\Component\Console\Att…ommand'" file="tests/Architecture/CommandsTest.php::expect 'Kirschbaum\Paragon\Commands' → toHaveAttribute 'Symfony\Component\Console\Att…ommand'" class="Tests\Architecture\CommandsTest" classname="Tests.Architecture.CommandsTest" assertions="1" time="0.005776"/>
<testcase name="expect 'Kirschbaum\Paragon\Commands' → not → toHavePrivateMethodsBesides ['__construct', 'handle']" file="tests/Architecture/CommandsTest.php::expect 'Kirschbaum\Paragon\Commands' → not → toHavePrivateMethodsBesides ['__construct', 'handle']" class="Tests\Architecture\CommandsTest" classname="Tests.Architecture.CommandsTest" assertions="1" time="0.005805"/>
<testcase name="expect 'Kirschbaum\Paragon\Commands' → not → toHavePublicMethodsBesides ['__construct', 'handle']" file="tests/Architecture/CommandsTest.php::expect 'Kirschbaum\Paragon\Commands' → not → toHavePublicMethodsBesides ['__construct', 'handle']" class="Tests\Architecture\CommandsTest" classname="Tests.Architecture.CommandsTest" assertions="1" time="0.005497"/>
</testsuite>
<testsuite name="Tests\Unit\Commands\GenerateJsEnumsCommandTest" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php" tests="10" assertions="39" errors="0" failures="0" skipped="0" time="0.109040">
<testcase name="it ignores static methods" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it ignores static methods" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="1" time="0.029866"/>
<testcase name="it generates non-backed enums" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it generates non-backed enums" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="8" time="0.008372"/>
<testcase name="it creates public methods" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it creates public methods" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="2" time="0.008806"/>
<testcase name="it generates number backed enums" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it generates number backed enums" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="8" time="0.008812"/>
<testcase name="it generates abstract enum" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it generates abstract enum" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="8" time="0.008539"/>
<testcase name="it generates enums recursively" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it generates enums recursively" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="1" time="0.009679"/>
<testcase name="it ignores methods with 'IgnoreParagon' attribute" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it ignores methods with 'IgnoreParagon' attribute" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="1" time="0.010053"/>
<testcase name="it generates string backed enums" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it generates string backed enums" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="8" time="0.008296"/>
<testcase name="it ignores enums within ignored paths" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it ignores enums within ignored paths" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="1" time="0.008138"/>
<testcase name="it ignores enums with 'IgnoreParagon' attribute" file="tests/Unit/Commands/GenerateJsEnumsCommandTest.php::it ignores enums with 'IgnoreParagon' attribute" class="Tests\Unit\Commands\GenerateJsEnumsCommandTest" classname="Tests.Unit.Commands.GenerateJsEnumsCommandTest" assertions="1" time="0.008479"/>
</testsuite>
<testsuite name="Tests\Architecture\PresetTest" file="tests/Architecture/PresetTest.php" tests="2" assertions="2" errors="0" failures="0" skipped="0" time="0.043342">
<testcase name="expect ['array_rand', 'assert', 'create_function', …] → not → toBeUsed " file="tests/Architecture/PresetTest.php::expect ['array_rand', 'assert', 'create_function', …] → not → toBeUsed " class="Tests\Architecture\PresetTest" classname="Tests.Architecture.PresetTest" assertions="1" time="0.031104"/>
<testcase name="preset → php " file="tests/Architecture/PresetTest.php::preset → php " class="Tests\Architecture\PresetTest" classname="Tests.Architecture.PresetTest" assertions="1" time="0.012238"/>
</testsuite>
<testsuite name="Tests\Unit\Commands\GenerateEnumsCommandTest" file="tests/Unit/Commands/GenerateEnumsCommandTest.php" tests="10" assertions="39" errors="0" failures="0" skipped="0" time="0.085712">
<testcase name="it generates non-backed enums" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it generates non-backed enums" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="8" time="0.009397"/>
<testcase name="it creates public methods" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it creates public methods" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="2" time="0.008704"/>
<testcase name="it generates string backed enums" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it generates string backed enums" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="8" time="0.008757"/>
<testcase name="it generates number backed enums" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it generates number backed enums" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="8" time="0.008208"/>
<testcase name="it generates abstract enum" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it generates abstract enum" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="8" time="0.008235"/>
<testcase name="it ignores methods with 'IgnoreParagon' attribute" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it ignores methods with 'IgnoreParagon' attribute" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="1" time="0.007996"/>
<testcase name="it generates enums recursively" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it generates enums recursively" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="1" time="0.008307"/>
<testcase name="it ignores enums with 'IgnoreParagon' attribute" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it ignores enums with 'IgnoreParagon' attribute" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="1" time="0.008137"/>
<testcase name="it ignores enums within ignored paths" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it ignores enums within ignored paths" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="1" time="0.009355"/>
<testcase name="it ignores static methods" file="tests/Unit/Commands/GenerateEnumsCommandTest.php::it ignores static methods" class="Tests\Unit\Commands\GenerateEnumsCommandTest" classname="Tests.Unit.Commands.GenerateEnumsCommandTest" assertions="1" time="0.008615"/>
</testsuite>
<testsuite name="Tests\Unit\Commands\ClearCacheCommandTest" file="tests/Unit/Commands/ClearCacheCommandTest.php" tests="1" assertions="2" errors="0" failures="0" skipped="0" time="0.010737">
<testcase name="it removes the cache directory" file="tests/Unit/Commands/ClearCacheCommandTest.php::it removes the cache directory" class="Tests\Unit\Commands\ClearCacheCommandTest" classname="Tests.Unit.Commands.ClearCacheCommandTest" assertions="2" time="0.010737"/>
</testsuite>
<testsuite name="Tests\Architecture\AttributesTest" file="tests/Architecture/AttributesTest.php" tests="2" assertions="2" errors="0" failures="0" skipped="0" time="0.011910">
<testcase name="expect 'Kirschbaum\Paragon\Concerns\I…aragon' → toHaveAttribute 'Attribute'" file="tests/Architecture/AttributesTest.php::expect 'Kirschbaum\Paragon\Concerns\I…aragon' → toHaveAttribute 'Attribute'" class="Tests\Architecture\AttributesTest" classname="Tests.Architecture.AttributesTest" assertions="1" time="0.006413"/>
<testcase name="expect 'Kirschbaum\Paragon\Concerns\I…aragon' → toExtendNothing " file="tests/Architecture/AttributesTest.php::expect 'Kirschbaum\Paragon\Concerns\I…aragon' → toExtendNothing " class="Tests\Architecture\AttributesTest" classname="Tests.Architecture.AttributesTest" assertions="1" time="0.005497"/>
</testsuite>
<testsuite name="Tests\Unit\Commands\MakeEnumMethodCommandTest" file="tests/Unit/Commands/MakeEnumMethodCommandTest.php" tests="2" assertions="4" errors="0" failures="0" skipped="0" time="0.014867">
<testcase name="it generates enum methods" file="tests/Unit/Commands/MakeEnumMethodCommandTest.php::it generates enum methods" class="Tests\Unit\Commands\MakeEnumMethodCommandTest" classname="Tests.Unit.Commands.MakeEnumMethodCommandTest" assertions="2" time="0.007192"/>
<testcase name="it imports the method into the base enum" file="tests/Unit/Commands/MakeEnumMethodCommandTest.php::it imports the method into the base enum" class="Tests\Unit\Commands\MakeEnumMethodCommandTest" classname="Tests.Unit.Commands.MakeEnumMethodCommandTest" assertions="2" time="0.007675"/>
</testsuite>
<testsuite name="Tests\Unit\Commands\MakeJsEnumMethodCommandTest" file="tests/Unit/Commands/MakeJsEnumMethodCommandTest.php" tests="2" assertions="4" errors="0" failures="0" skipped="0" time="0.015414">
<testcase name="it imports the method into the base enum" file="tests/Unit/Commands/MakeJsEnumMethodCommandTest.php::it imports the method into the base enum" class="Tests\Unit\Commands\MakeJsEnumMethodCommandTest" classname="Tests.Unit.Commands.MakeJsEnumMethodCommandTest" assertions="2" time="0.007857"/>
<testcase name="it generates enum methods" file="tests/Unit/Commands/MakeJsEnumMethodCommandTest.php::it generates enum methods" class="Tests\Unit\Commands\MakeJsEnumMethodCommandTest" classname="Tests.Unit.Commands.MakeJsEnumMethodCommandTest" assertions="2" time="0.007557"/>
</testsuite>
<testsuite name="Tests\Architecture\GeneratorsTest" file="tests/Architecture/GeneratorsTest.php" tests="5" assertions="5" errors="0" failures="0" skipped="0" time="0.036263">
<testcase name="expect 'Kirschbaum\Paragon\Generators' → toExtendNothing " file="tests/Architecture/GeneratorsTest.php::expect 'Kirschbaum\Paragon\Generators' → toExtendNothing " class="Tests\Architecture\GeneratorsTest" classname="Tests.Architecture.GeneratorsTest" assertions="1" time="0.015589"/>
<testcase name="expect 'Kirschbaum\Paragon\Generators' → not → toHavePrivateMethodsBesides ['__construct', '__invoke']" file="tests/Architecture/GeneratorsTest.php::expect 'Kirschbaum\Paragon\Generators' → not → toHavePrivateMethodsBesides ['__construct', '__invoke']" class="Tests\Architecture\GeneratorsTest" classname="Tests.Architecture.GeneratorsTest" assertions="1" time="0.005312"/>
<testcase name="expect 'Kirschbaum\Paragon\Generators' → toBeInvokable " file="tests/Architecture/GeneratorsTest.php::expect 'Kirschbaum\Paragon\Generators' → toBeInvokable " class="Tests\Architecture\GeneratorsTest" classname="Tests.Architecture.GeneratorsTest" assertions="1" time="0.004822"/>
<testcase name="expect 'Kirschbaum\Paragon\Generators' → not → toHavePublicMethodsBesides ['__construct', '__invoke']" file="tests/Architecture/GeneratorsTest.php::expect 'Kirschbaum\Paragon\Generators' → not → toHavePublicMethodsBesides ['__construct', '__invoke']" class="Tests\Architecture\GeneratorsTest" classname="Tests.Architecture.GeneratorsTest" assertions="1" time="0.005535"/>
<testcase name="expect 'Kirschbaum\Paragon\Generators' → classes → toHaveSuffix 'Generator'" file="tests/Architecture/GeneratorsTest.php::expect 'Kirschbaum\Paragon\Generators' → classes → toHaveSuffix 'Generator'" class="Tests\Architecture\GeneratorsTest" classname="Tests.Architecture.GeneratorsTest" assertions="1" time="0.005004"/>
</testsuite>
<testsuite name="Tests\Architecture\DiscoveryTest" file="tests/Architecture/DiscoveryTest.php" tests="3" assertions="3" errors="0" failures="0" skipped="0" time="0.017835">
<testcase name="expect 'Kirschbaum\Paragon\Concerns\D…rEnums' → not → toHavePrivateMethodsBesides ['within']" file="tests/Architecture/DiscoveryTest.php::expect 'Kirschbaum\Paragon\Concerns\D…rEnums' → not → toHavePrivateMethodsBesides ['within']" class="Tests\Architecture\DiscoveryTest" classname="Tests.Architecture.DiscoveryTest" assertions="1" time="0.007642"/>
<testcase name="expect 'Kirschbaum\Paragon\Concerns\D…rEnums' → toExtendNothing " file="tests/Architecture/DiscoveryTest.php::expect 'Kirschbaum\Paragon\Concerns\D…rEnums' → toExtendNothing " class="Tests\Architecture\DiscoveryTest" classname="Tests.Architecture.DiscoveryTest" assertions="1" time="0.004790"/>
<testcase name="expect 'Kirschbaum\Paragon\Concerns\D…rEnums' → not → toHavePublicMethodsBesides ['within']" file="tests/Architecture/DiscoveryTest.php::expect 'Kirschbaum\Paragon\Concerns\D…rEnums' → not → toHavePublicMethodsBesides ['within']" class="Tests\Architecture\DiscoveryTest" classname="Tests.Architecture.DiscoveryTest" assertions="1" time="0.005403"/>
</testsuite>
</testsuite>
</testsuite>
</testsuites>
35 changes: 33 additions & 2 deletions src/Commands/GenerateEnumsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Kirschbaum\Paragon\Concerns\Builders\EnumBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumJsBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumTsBuilder;
use Kirschbaum\Paragon\Concerns\DiscoverEnums;
use Kirschbaum\Paragon\Concerns\IgnoreParagon;
use Kirschbaum\Paragon\Generators\AbstractEnumGenerator;
use Kirschbaum\Paragon\Generators\EnumGenerator;
use ReflectionEnum;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption;

#[AsCommand(name: 'paragon:generate-enums', description: 'Generate Typescript versions of existing PHP enums')]
class GenerateEnumsCommand extends Command
Expand All @@ -21,13 +25,15 @@ class GenerateEnumsCommand extends Command
*/
public function handle(): int
{
$builder = $this->builder();

$generatedEnums = $this->enums()
->map(fn ($enum) => app(EnumGenerator::class, ['enum' => $enum])())
->map(fn ($enum) => app(EnumGenerator::class, ['enum' => $enum, 'builder' => $builder])())
->filter();

$this->components->info("{$generatedEnums->count()} enums have been (re)generated.");

app(AbstractEnumGenerator::class)();
app(AbstractEnumGenerator::class, ['builder' => $builder])();

$this->components->info('Abstract enum class has been (re)generated.');

Expand Down Expand Up @@ -58,4 +64,29 @@ protected function enums(): Collection
})
->values();
}

protected function builder(): EnumBuilder
{
return $this->option('javascript')
? app(EnumJsBuilder::class)
: app(EnumTsBuilder::class);

}

/**
* Get the console command options.
*
* @return array<int, InputOption>
*/
protected function getOptions(): array
{
return [
new InputOption(
name: 'javascript',
shortcut: ['j', 'js'],
mode: InputOption::VALUE_NONE,
description: 'Output Javascript files',
),
];
}
}
36 changes: 33 additions & 3 deletions src/Commands/MakeEnumMethodCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace Kirschbaum\Paragon\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Console\GeneratorCommand;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Kirschbaum\Paragon\Concerns\Builders\EnumBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumJsBuilder;
use Kirschbaum\Paragon\Concerns\Builders\EnumTsBuilder;
use Kirschbaum\Paragon\Generators\AbstractEnumGenerator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

use function Laravel\Prompts\text;

Expand All @@ -25,7 +28,7 @@ public function handle(): ?bool
{
parent::handle();

app(AbstractEnumGenerator::class)();
app(AbstractEnumGenerator::class, ['builder' => $this->builder()])();

$this->components->info("Abstract enum class has been rebuilt to include new [{$this->name()}] method.");

Expand Down Expand Up @@ -87,7 +90,9 @@ protected function buildClass($name): string
*/
protected function getPath($name): string
{
return resource_path(config('paragon.enums.paths.methods')) . "/{$this->name()}.ts";
$extension = $this->option('javascript') ? 'js' : 'ts';

return resource_path(config('paragon.enums.paths.methods')) . "/{$this->name()}.{$extension}";
}

/**
Expand All @@ -105,4 +110,29 @@ protected function name(): string

throw new Exception('[name] argument is not a string.');
}

protected function builder(): EnumBuilder
{
return $this->option('javascript')
? app(EnumJsBuilder::class)
: app(EnumTsBuilder::class);

}

/**
* Get the console command options.
*
* @return array<int, InputOption>
*/
protected function getOptions(): array
{
return [
new InputOption(
name: 'javascript',
shortcut: ['j', 'js'],
mode: InputOption::VALUE_NONE,
description: 'Output Javascript files',
),
];
}
}
Loading

0 comments on commit 24c3c26

Please sign in to comment.