-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change EntitiesDbTest class to an abstract test case
This allows each database implementation to extend it with a data provider. Also use groups instead of config to exclude database-specific tests.
- Loading branch information
1 parent
23dff01
commit 987ee2a
Showing
9 changed files
with
163 additions
and
240 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,4 @@ jobs: | |
if: ${{ matrix.php == '8.3' }} | ||
|
||
- name: Run PHPUnit | ||
run: vendor/bin/phpunit | ||
run: composer test-mysql |
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,57 @@ | ||
<?php | ||
|
||
namespace theodorejb\Phaster\Test; | ||
|
||
use PeachySQL\PeachySql; | ||
use PeachySQL\SqlServer; | ||
use theodorejb\Phaster\Test\src\App; | ||
|
||
/** | ||
* @group mssql | ||
*/ | ||
class MssqlDbTest extends DbTestCase | ||
{ | ||
private static ?PeachySql $db = null; | ||
|
||
public static function dbProvider(): PeachySql | ||
{ | ||
if (!self::$db) { | ||
$c = App::$config; | ||
$conn = sqlsrv_connect($c->getSqlsrvServer(), $c->getSqlsrvConnInfo()); | ||
|
||
if (!$conn) { | ||
throw new \Exception('Failed to connect to SQL server: ' . print_r(sqlsrv_errors(), true)); | ||
} | ||
|
||
self::$db = new SqlServer($conn); | ||
self::createTestTable(self::$db); | ||
} | ||
|
||
return self::$db; | ||
} | ||
|
||
private static function createTestTable(PeachySql $db): void | ||
{ | ||
$sql = " | ||
DROP TABLE IF EXISTS UserThings; | ||
DROP TABLE IF EXISTS Users; | ||
CREATE TABLE Users ( | ||
user_id INT PRIMARY KEY IDENTITY NOT NULL, | ||
name NVARCHAR(50) NOT NULL UNIQUE, | ||
dob DATE NOT NULL, | ||
weight FLOAT NOT NULL, | ||
isDisabled BIT NOT NULL | ||
)"; | ||
|
||
$db->query($sql); | ||
|
||
$sql = " | ||
CREATE TABLE UserThings ( | ||
thing_id INT PRIMARY KEY IDENTITY NOT NULL, | ||
user_id INT NOT NULL, | ||
FOREIGN KEY (user_id) REFERENCES Users(user_id) | ||
)"; | ||
|
||
$db->query($sql); | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace theodorejb\Phaster\Test; | ||
|
||
use PeachySQL\Mysql; | ||
use PeachySQL\PeachySql; | ||
use theodorejb\Phaster\Test\src\App; | ||
|
||
/** | ||
* @group mysql | ||
*/ | ||
class MysqlDbTest extends DbTestCase | ||
{ | ||
private static ?PeachySql $db = null; | ||
|
||
public static function dbProvider(): PeachySql | ||
{ | ||
if (!self::$db) { | ||
$c = App::$config; | ||
$conn = new \mysqli($c->getMysqlHost(), $c->getMysqlUser(), $c->getMysqlPassword(), $c->getMysqlDatabase()); | ||
|
||
if ($conn->connect_error !== null) { | ||
throw new \Exception('Failed to connect to MySQL: ' . $conn->connect_error); | ||
} | ||
|
||
self::$db = new Mysql($conn); | ||
self::createTestTable(self::$db); | ||
} | ||
|
||
return self::$db; | ||
} | ||
|
||
private static function createTestTable(PeachySql $db): void | ||
{ | ||
$db->query("DROP TABLE IF EXISTS UserThings"); | ||
$db->query("DROP TABLE IF EXISTS Users"); | ||
|
||
$sql = " | ||
CREATE TABLE Users ( | ||
user_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL, | ||
name VARCHAR(50) NOT NULL UNIQUE, | ||
dob DATE NOT NULL, | ||
weight DOUBLE NOT NULL, | ||
isDisabled BOOLEAN NOT NULL | ||
)"; | ||
|
||
$db->query($sql); | ||
|
||
$sql = " | ||
CREATE TABLE UserThings ( | ||
thing_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL, | ||
user_id INT NOT NULL, | ||
FOREIGN KEY (user_id) REFERENCES Users(user_id) | ||
)"; | ||
|
||
$db->query($sql); | ||
} | ||
} |
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,15 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use theodorejb\Phaster\Test\src\{DbConnector, Config, LocalConfig}; | ||
use theodorejb\Phaster\Test\src\{App, Config, LocalConfig}; | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
if (class_exists(LocalConfig::class)) { | ||
// suppress error when LocalConfig doesn't exist | ||
/** @psalm-suppress MixedArgument */ | ||
DbConnector::setConfig(new LocalConfig()); | ||
/** @psalm-suppress MixedAssignment */ | ||
App::$config = new LocalConfig(); | ||
} else { | ||
DbConnector::setConfig(new Config()); | ||
App::$config = new Config(); | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace theodorejb\Phaster\Test\src; | ||
|
||
class App | ||
{ | ||
public static Config $config; | ||
} |
Oops, something went wrong.