-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better enum support when interacting with SQL databases
- Loading branch information
Showing
8 changed files
with
177 additions
and
66 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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Frederic G. Østby | ||
* @license http://www.makoframework.com/license | ||
*/ | ||
|
||
namespace mako\tests\integration\database; | ||
|
||
use mako\tests\integration\InMemoryDbTestCase; | ||
|
||
// -------------------------------------------------------------------------- | ||
// START CLASSES | ||
// -------------------------------------------------------------------------- | ||
|
||
enum BackedUserEmailEnum: string | ||
{ | ||
case FOO = 'foo@example.org'; | ||
case BAR = 'bar@example.org'; | ||
} | ||
|
||
enum BackedUserIdEnum: int | ||
{ | ||
case FOO = 1; | ||
case BAR = 2; | ||
} | ||
|
||
enum UsernameEnum | ||
{ | ||
case foo; | ||
case bar; | ||
} | ||
|
||
// -------------------------------------------------------------------------- | ||
// END CLASSES | ||
// -------------------------------------------------------------------------- | ||
|
||
/** | ||
* @group integration | ||
* @group integration:database | ||
* @requires extension PDO | ||
* @requires extension pdo_sqlite | ||
*/ | ||
class EnumTest extends InMemoryDbTestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
public function testBackedStringEnum(): void | ||
{ | ||
$user = $this->connectionManager->getConnection()->first(<<<'SQL' | ||
SELECT * FROM users WHERE email = ? | ||
SQL, [BackedUserEmailEnum::FOO]); | ||
|
||
$this->assertSame(BackedUserEmailEnum::FOO->value, $user->email); | ||
|
||
$user = $this->connectionManager->getConnection()->first(<<<'SQL' | ||
SELECT * FROM users WHERE email = ? | ||
SQL, [BackedUserEmailEnum::BAR]); | ||
|
||
$this->assertSame(BackedUserEmailEnum::BAR->value, $user->email); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testBackedIntEnum(): void | ||
{ | ||
$user = $this->connectionManager->getConnection()->first(<<<'SQL' | ||
SELECT * FROM users WHERE id = ? | ||
SQL, [BackedUserIdEnum::FOO]); | ||
|
||
$this->assertSame(BackedUserIdEnum::FOO->value, $user->id); | ||
|
||
$user = $this->connectionManager->getConnection()->first(<<<'SQL' | ||
SELECT * FROM users WHERE id = ? | ||
SQL, [BackedUserIdEnum::BAR]); | ||
|
||
$this->assertSame(BackedUserIdEnum::BAR->value, $user->id); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testEnum(): void | ||
{ | ||
$user = $this->connectionManager->getConnection()->first(<<<'SQL' | ||
SELECT * FROM users WHERE username = ? | ||
SQL, [UsernameEnum::foo]); | ||
|
||
$this->assertSame(UsernameEnum::foo->name, $user->username); | ||
|
||
$user = $this->connectionManager->getConnection()->first(<<<'SQL' | ||
SELECT * FROM users WHERE username = ? | ||
SQL, [UsernameEnum::bar]); | ||
|
||
$this->assertSame(UsernameEnum::bar->name, $user->username); | ||
} | ||
} |
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