Skip to content

Commit b11b3c7

Browse files
authored
[12.x] Handle Enum::cases() for enum column type (#56565)
* [12.x] Handle Enum::cases() for enum column type * cleanup tests * import function
1 parent 44f8553 commit b11b3c7

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Illuminate\Support\Fluent;
1414
use Illuminate\Support\Traits\Macroable;
1515

16+
use function Illuminate\Support\enum_value;
17+
1618
class Blueprint
1719
{
1820
use Macroable;
@@ -1102,6 +1104,8 @@ public function boolean($column)
11021104
*/
11031105
public function enum($column, array $allowed)
11041106
{
1107+
$allowed = array_map(fn ($value) => enum_value($value), $allowed);
1108+
11051109
return $this->addColumn('enum', $column, compact('allowed'));
11061110
}
11071111

tests/Database/DatabaseMariaDbSchemaGrammarTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,10 +835,12 @@ public function testAddingEnum()
835835
{
836836
$blueprint = new Blueprint($this->getConnection(), 'users');
837837
$blueprint->enum('role', ['member', 'admin']);
838+
$blueprint->enum('status', Foo::cases());
838839
$statements = $blueprint->toSql();
839840

840-
$this->assertCount(1, $statements);
841+
$this->assertCount(2, $statements);
841842
$this->assertSame('alter table `users` add `role` enum(\'member\', \'admin\') not null', $statements[0]);
843+
$this->assertSame('alter table `users` add `status` enum(\'bar\') not null', $statements[1]);
842844
}
843845

844846
public function testAddingSet()

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,12 @@ public function testAddingEnum()
834834
{
835835
$blueprint = new Blueprint($this->getConnection(), 'users');
836836
$blueprint->enum('role', ['member', 'admin']);
837+
$blueprint->enum('status', Foo::cases());
837838
$statements = $blueprint->toSql();
838839

839-
$this->assertCount(1, $statements);
840+
$this->assertCount(2, $statements);
840841
$this->assertSame('alter table `users` add `role` enum(\'member\', \'admin\') not null', $statements[0]);
842+
$this->assertSame('alter table `users` add `status` enum(\'bar\') not null', $statements[1]);
841843
}
842844

843845
public function testAddingSet()

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
1010
use Illuminate\Database\Schema\Grammars\PostgresGrammar;
1111
use Illuminate\Database\Schema\PostgresBuilder;
12+
use Illuminate\Tests\Database\Fixtures\Enums\Foo;
1213
use Mockery as m;
1314
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\Attributes\TestWith;
@@ -718,10 +719,12 @@ public function testAddingEnum()
718719
{
719720
$blueprint = new Blueprint($this->getConnection(), 'users');
720721
$blueprint->enum('role', ['member', 'admin']);
722+
$blueprint->enum('status', Foo::cases());
721723
$statements = $blueprint->toSql();
722724

723-
$this->assertCount(1, $statements);
725+
$this->assertCount(2, $statements);
724726
$this->assertSame('alter table "users" add column "role" varchar(255) check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
727+
$this->assertSame('alter table "users" add column "status" varchar(255) check ("status" in (\'bar\')) not null', $statements[1]);
725728
}
726729

727730
public function testAddingDate()

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
1111
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
1212
use Illuminate\Database\Schema\SQLiteBuilder;
13+
use Illuminate\Tests\Database\Fixtures\Enums\Foo;
1314
use Mockery as m;
1415
use PHPUnit\Framework\TestCase;
1516
use RuntimeException;
@@ -566,10 +567,12 @@ public function testAddingEnum()
566567
{
567568
$blueprint = new Blueprint($this->getConnection(), 'users');
568569
$blueprint->enum('role', ['member', 'admin']);
570+
$blueprint->enum('status', Foo::cases());
569571
$statements = $blueprint->toSql();
570572

571-
$this->assertCount(1, $statements);
573+
$this->assertCount(2, $statements);
572574
$this->assertSame('alter table "users" add column "role" varchar check ("role" in (\'member\', \'admin\')) not null', $statements[0]);
575+
$this->assertSame('alter table "users" add column "status" varchar check ("status" in (\'bar\')) not null', $statements[1]);
573576
}
574577

575578
public function testAddingJson()

tests/Database/DatabaseSqlServerSchemaGrammarTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Schema\ForeignIdColumnDefinition;
99
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
1010
use Illuminate\Database\Schema\SqlServerBuilder;
11+
use Illuminate\Tests\Database\Fixtures\Enums\Foo;
1112
use Mockery as m;
1213
use PHPUnit\Framework\TestCase;
1314

@@ -578,10 +579,12 @@ public function testAddingEnum()
578579
{
579580
$blueprint = new Blueprint($this->getConnection(), 'users');
580581
$blueprint->enum('role', ['member', 'admin']);
582+
$blueprint->enum('status', Foo::cases());
581583
$statements = $blueprint->toSql();
582584

583-
$this->assertCount(1, $statements);
585+
$this->assertCount(2, $statements);
584586
$this->assertSame('alter table "users" add "role" nvarchar(255) check ("role" in (N\'member\', N\'admin\')) not null', $statements[0]);
587+
$this->assertSame('alter table "users" add "status" nvarchar(255) check ("status" in (N\'bar\')) not null', $statements[1]);
585588
}
586589

587590
public function testAddingJson()

0 commit comments

Comments
 (0)