Skip to content

Commit da4a85b

Browse files
committed
feat: Ability to run generator when Enums have just one case
1 parent 11e926c commit da4a85b

5 files changed

Lines changed: 482 additions & 1 deletion

File tree

src/FieldTypes/EnumField.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ public function generateCorrectValue(): string
139139

140140
public function generateDifferentCorrectValue(): string
141141
{
142-
return $this->hasEnumClass() ? $this->getEnumClassBasename() . '::' . $this->getEnumCases()[1]->name . '->value' : "'" . ($this->getOptions()[1] ?? $this->getOptions()[0]) . "'";
142+
if ($this->hasEnumClass()) {
143+
$cases = $this->getEnumCases();
144+
$secondCase = $cases[1] ?? $cases[0]; // Fallback to the first case if the second doesn't exist
145+
return $this->getEnumClassBasename() . '::' . $secondCase->name . '->value';
146+
}
147+
148+
return "'" . ($this->getOptions()[1] ?? $this->getOptions()[0]) . "'";
143149
}
144150

145151
public function getFormComponentAttributes(): array
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Javaabu\Generators\Tests\TestSupport\Enums;
4+
5+
enum SingleCaseOrderStatuses: string
6+
{
7+
case Pending = 'pending';
8+
9+
public static function getLabels(): array
10+
{
11+
return array_column(self::cases(), 'name', 'value');
12+
}
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
use Javaabu\Generators\Tests\TestSupport\Enums\SingleCaseOrderStatuses;
7+
8+
return new class extends Migration
9+
{
10+
/**
11+
* Run the migrations.
12+
*
13+
* @return void
14+
*/
15+
public function up()
16+
{
17+
Schema::create('single_value_enum_orders', function (Blueprint $table) {
18+
$table->id();
19+
$table->string('order_no', 4);
20+
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
21+
$table->string('product_slug');
22+
$table->nativeEnum('status', SingleCaseOrderStatuses::class)->index();
23+
24+
$table->foreign('product_slug')
25+
->references('slug')
26+
->on('products')
27+
->cascadeOnDelete();
28+
29+
$table->timestamps();
30+
});
31+
}
32+
/**
33+
* Reverse the migrations.
34+
*
35+
* @return void
36+
*/
37+
public function down()
38+
{
39+
Schema::dropIfExists('orders');
40+
}
41+
};

tests/Unit/Generators/TestGeneratorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,15 @@ public function it_can_generate_a_test_with_multiple_foreign_keys(): void
141141

142142
$this->assertEquals($expected_content, $actual_content);
143143
}
144+
145+
/** @test */
146+
public function it_can_generate_a_test_where_the_enum_has_only_one_case(): void
147+
{
148+
$test_generator = new TestGenerator('single_value_enum_orders');
149+
150+
$expected_content = $this->getTestStubContents('tests/SingleValueEnumOrdersControllerTest.stub');
151+
$actual_content = $test_generator->render();
152+
153+
$this->assertEquals($expected_content, $actual_content);
154+
}
144155
}

0 commit comments

Comments
 (0)