-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing issue with Schema functions and writing tests
Signed-off-by: Dusan Malusev <[email protected]>
- Loading branch information
1 parent
b38c0ec
commit d57817e
Showing
6 changed files
with
97 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use CodeLieutenant\LaravelPgEnum\Enums\Direction; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
afterEach(function () { | ||
try { | ||
DB::statement('DROP TYPE test_enum;'); | ||
} catch (Exception$e) { | ||
} | ||
}); | ||
|
||
test('create new enum with values', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
assertEnumExists('test_enum'); | ||
assertEnumHasValues('test_enum', ['one', 'two', 'three']); | ||
Schema::dropEnum('test_enum'); | ||
assertEnumNotExists('test_enum'); | ||
}); | ||
|
||
test('create same enum twice', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
})->expectExceptionObject(new PDOException('SQLSTATE[42710]: Duplicate object: 7 ERROR: type "test_enum" already exists', 42710)); | ||
|
||
test('create same enum twice using createIfNotExists', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
Schema::createEnumIfNotExists('test_enum', ['one', 'two', 'three']); | ||
})->throwsNoExceptions(); | ||
|
||
test('add new value to existing enum', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
assertEnumExists('test_enum'); | ||
assertEnumHasValues('test_enum', ['one', 'two', 'three']); | ||
Schema::addEnumValue('test_enum', 'four'); | ||
assertEnumHasValues('test_enum', ['one', 'two', 'three', 'four']); | ||
}); | ||
|
||
test('add new value to existing enum before', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
Schema::addEnumValue('test_enum', 'four', Direction::BEFORE, 'one'); | ||
|
||
assertEnumHasValues('test_enum', ['four', 'one', 'two', 'three']); | ||
}); | ||
|
||
test('add new value to existing enum after', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
Schema::addEnumValue('test_enum', 'four', Direction::AFTER, 'one'); | ||
|
||
assertEnumHasValues('test_enum', ['one', 'four', 'two', 'three']); | ||
}); | ||
|
||
test('rename enum value', function () { | ||
Schema::createEnum('test_enum', ['one', 'two', 'three']); | ||
Schema::renameEnumValue('test_enum', 'one', 'newName'); | ||
assertEnumHasValues('test_enum', ['newName', 'two', 'three']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use CodeLieutenant\LaravelPgEnum\Tests\TestCase; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
use function PHPUnit\Framework\assertEmpty; | ||
use function PHPUnit\Framework\assertEquals; | ||
use function PHPUnit\Framework\assertNotEmpty; | ||
|
||
uses(TestCase::class)->in(__DIR__); | ||
|
||
function assertEnumExists(string $name) | ||
{ | ||
$values = DB::select('SELECT * FROM pg_type where typname = ?', [$name]); | ||
assertNotEmpty($values); | ||
} | ||
|
||
function assertEnumNotExists(string $name) | ||
{ | ||
$values = DB::select('SELECT * FROM pg_type where typname = ?', [$name]); | ||
assertEmpty($values); | ||
} | ||
|
||
function assertEnumHasValues(string $name, array $values) | ||
{ | ||
$vals = Str::of(DB::select("SELECT enum_range(null::$name)")[0]->enum_range) | ||
->ltrim('{') | ||
->rtrim('}') | ||
->explode(',') | ||
->toArray(); | ||
|
||
assertEquals($values, $vals); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters