Skip to content

Commit

Permalink
Merge pull request #6 from RichardGGD/master
Browse files Browse the repository at this point in the history
Support array constants and more
  • Loading branch information
modstore authored Dec 1, 2022
2 parents d8c36b6 + cfb197d commit 29bee46
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":300:{a:2:{s:7:"defects";a:1:{s:79:"Modstore\LaravelEnumJs\Tests\Console\Commands\GenerateCommandTest::testGenerate";i:5;}s:5:"times";a:2:{s:79:"Modstore\LaravelEnumJs\Tests\Console\Commands\GenerateCommandTest::testGenerate";d:0.218;s:54:"Modstore\LaravelEnumJs\Tests\ExampleTest::true_is_true";d:0.046;}}}
{"version":1,"defects":{"Modstore\\LaravelEnumJs\\Tests\\Console\\Commands\\GenerateCommandTest::testGenerate":3},"times":{"Modstore\\LaravelEnumJs\\Tests\\Console\\Commands\\GenerateCommandTest::testGenerate":0.121}}
6 changes: 1 addition & 5 deletions src/Console/Commands/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ protected function writeFile(string $class)
$value = property_exists($value, 'value') ? $value->value : $value->name;
}

if (gettype($value) == gettype(" ")) {
$outputString .= sprintf("export const %s = \"%s\"\n", $key, $value);
} else {
$outputString .= sprintf("export const %s = %s\n", $key, $value);
}
$outputString .= sprintf("export const %s = %s\n", $key, json_encode($value));
}

Storage::disk(config('laravel-enum-js.output_disk'))->put($outputPath, $outputString);
Expand Down
20 changes: 13 additions & 7 deletions tests/Console/Commands/GenerateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ protected function setUp(): void
public function testGenerate()
{
// Include some classes for this test.
include('tests/resources/Enums/Status.php');
include('tests/resources/Enums/StringValue.php');
include('tests/resources/Enums/Sub/Type.php');
include('tests/resources/Enums/Native/BackedString.php');
include('tests/resources/Enums/Native/Base.php');
include('tests/resources/Enums/Native/BackedInt.php');
include_once('tests/resources/Enums/Status.php');
include_once('tests/resources/Enums/StringValue.php');
include_once('tests/resources/Enums/Sub/Type.php');
include_once('tests/resources/Enums/Native/BackedString.php');
include_once('tests/resources/Enums/Native/Base.php');
include_once('tests/resources/Enums/Native/BackedInt.php');
include_once('tests/resources/Enums/ArrayValue.php');

Artisan::call('enum-js:generate');

$generatedFiles = Storage::disk(config('laravel-enum-js.output_disk'))->allFiles();

$this->assertSame([
'ArrayValue.js',
'Native/BackedInt.js',
'Native/BackedString.js',
'Native/Base.js',
Expand Down Expand Up @@ -69,6 +71,10 @@ public function generatedContentDataProvider(): array
'filename' => 'Native/BackedString.php',
'expectedContent' => "export const Value1 = \"value-1\"\nexport const Value2 = \"value-2\"\n",
],
'array' => [
'filename' => 'ArrayValue.php',
'expectedContent' => "export const IntArray = [1,2]\nexport const StringArray = [\"value-1\",\"value-2\"]\n",
],
];
}

Expand All @@ -77,7 +83,7 @@ public function generatedContentDataProvider(): array
*/
public function testGeneratedContent(string $filename, string $expectedContent)
{
include('tests/resources/Enums/' . $filename);
include_once('tests/resources/Enums/' . $filename);

Artisan::call('enum-js:generate');

Expand Down
18 changes: 18 additions & 0 deletions tests/resources/Enums/ArrayValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Enums;

use App\Enums\Native\BackedInt;
use App\Enums\Native\BackedString;

final class ArrayValue
{
const IntArray = [
BackedInt::Value1,
BackedInt::Value2,
];
const StringArray = [
BackedString::Value1,
BackedString::Value2,
];
}

0 comments on commit 29bee46

Please sign in to comment.