diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 56dab63..8addda1 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,13 +11,14 @@ jobs: services: mysql: - image: mysql:8.0 + image: mysql:8.4 env: MYSQL_ALLOW_EMPTY_PASSWORD: yes MYSQL_DATABASE: Phaster ports: - - 3306 - options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 + # map port 3306 on service container to the host + - 3306:3306 + options: --health-cmd "mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 5 steps: - name: Checkout @@ -43,5 +44,3 @@ jobs: - name: Run PHPUnit run: vendor/bin/phpunit - env: - DB_PORT: ${{ job.services.mysql.ports[3306] }} diff --git a/test/EntitiesDbTest.php b/test/EntitiesDbTest.php index 696c024..899f193 100644 --- a/test/EntitiesDbTest.php +++ b/test/EntitiesDbTest.php @@ -4,18 +4,13 @@ namespace theodorejb\Phaster\Test; -use PeachySQL\{Mysql, PeachySql, SqlServer}; +use PeachySQL\PeachySql; use PHPUnit\Framework\TestCase; use theodorejb\Phaster\Entities; use theodorejb\Phaster\Test\src\{DbConnector, Users, LegacyUsers, ModernUsers}; class EntitiesDbTest extends TestCase { - public static function tearDownAfterClass(): void - { - DbConnector::deleteTestTables(); - } - /** * @return list */ @@ -25,11 +20,11 @@ public static function dbProvider(): array $databases = []; if ($config->testMysql()) { - $databases[] = [new Mysql(DbConnector::getMysqlConn())]; + $databases[] = [DbConnector::getMysqlConn()]; } if ($config->testSqlsrv()) { - $databases[] = [new SqlServer(DbConnector::getSqlsrvConn())]; + $databases[] = [DbConnector::getSqlsrvConn()]; } return $databases; diff --git a/test/src/DbConnector.php b/test/src/DbConnector.php index 009de12..6cf6ce2 100644 --- a/test/src/DbConnector.php +++ b/test/src/DbConnector.php @@ -6,16 +6,16 @@ use Exception; use mysqli; +use PeachySQL\Mysql; +use PeachySQL\PeachySql; +use PeachySQL\SqlServer; class DbConnector { private static Config $config; - private static ?mysqli $mysqlConn = null; - /** - * @var resource|null - */ - private static $sqlsrvConn; + private static ?PeachySql $mssqlDb = null; + private static ?PeachySql $mysqlDb = null; public static function setConfig(Config $config): void { @@ -27,137 +27,98 @@ public static function getConfig(): Config return self::$config; } - public static function getMysqlConn(): mysqli + public static function getMysqlConn(): PeachySql { - if (!self::$mysqlConn) { + if (!self::$mysqlDb) { $c = self::getConfig(); - $dbPort = getenv('DB_PORT'); + $conn = new mysqli($c->getMysqlHost(), $c->getMysqlUser(), $c->getMysqlPassword(), $c->getMysqlDatabase()); - if ($dbPort === false) { - $dbPort = 3306; - } else { - $dbPort = (int) $dbPort; + if ($conn->connect_error !== null) { + throw new Exception('Failed to connect to MySQL: ' . $conn->connect_error); } - self::$mysqlConn = new mysqli($c->getMysqlHost(), $c->getMysqlUser(), $c->getMysqlPassword(), $c->getMysqlDatabase(), $dbPort); - - if (self::$mysqlConn->connect_error !== null) { - throw new Exception('Failed to connect to MySQL: ' . self::$mysqlConn->connect_error); - } - - self::createMysqlTestTable(self::$mysqlConn); + self::$mysqlDb = new Mysql($conn); + self::createMysqlTestTable(self::$mysqlDb); } - return self::$mysqlConn; + return self::$mysqlDb; } - /** - * @return resource - */ - public static function getSqlsrvConn() + public static function getSqlsrvConn(): PeachySql { - if (!self::$sqlsrvConn) { + if (!self::$mssqlDb) { $c = self::getConfig(); - self::$sqlsrvConn = sqlsrv_connect($c->getSqlsrvServer(), $c->getSqlsrvConnInfo()); + $conn = sqlsrv_connect($c->getSqlsrvServer(), $c->getSqlsrvConnInfo()); - if (!self::$sqlsrvConn) { + if (!$conn) { throw new Exception('Failed to connect to SQL server: ' . print_r(sqlsrv_errors(), true)); } - self::createSqlServerTestTable(self::$sqlsrvConn); + self::$mssqlDb = new SqlServer($conn); + self::createSqlServerTestTable(self::$mssqlDb); } - return self::$sqlsrvConn; + return self::$mssqlDb; } - /** - * @param resource $conn - */ - private static function createSqlServerTestTable($conn): void + private static function createSqlServerTestTable(PeachySql $db): void { - $sql = 'CREATE TABLE Users ( - user_id INT PRIMARY KEY IDENTITY NOT NULL, - name VARCHAR(50) NOT NULL UNIQUE, - dob DATE NOT NULL, - weight FLOAT NOT NULL, - isDisabled BIT NOT NULL - );'; - - if (!sqlsrv_query($conn, $sql)) { - throw new Exception('Failed to create SQL Server test table: ' . print_r(sqlsrv_errors(), true)); - } - - $sql = 'CREATE VIEW vUsers AS - SELECT user_id AS u_id, name, dob, weight, isDisabled - FROM Users;'; - - if (!sqlsrv_query($conn, $sql)) { - throw new Exception('Failed to create SQL Server test view: ' . print_r(sqlsrv_errors(), true)); - } - - $sql = 'CREATE TABLE UserThings ( - thing_id INT PRIMARY KEY IDENTITY NOT NULL, - user_id INT NOT NULL FOREIGN KEY REFERENCES Users(user_id) - );'; - - if (!sqlsrv_query($conn, $sql)) { - throw new Exception('Failed to create SQL Server UserThings table: ' . print_r(sqlsrv_errors(), true)); - } + self::deleteTestTables($db); + + $sql = " + 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 REFERENCES Users(user_id) + )"; + + $db->query($sql); } - private static function createMysqlTestTable(mysqli $conn): void + private static function createMysqlTestTable(PeachySql $db): void { - $sql = 'CREATE TABLE Users ( - user_id INT PRIMARY KEY AUTO_INCREMENT NOT NULL, - name VARCHAR(50) NOT NULL UNIQUE, - dob DATE NOT NULL, - weight FLOAT NOT NULL, - isDisabled BOOLEAN NOT NULL - );'; - - if (!$conn->query($sql)) { - throw new Exception('Failed to create MySQL test table: ' . print_r($conn->error_list, true)); - } - - $sql = 'CREATE VIEW vUsers AS - SELECT user_id AS u_id, name, dob, weight, isDisabled - FROM Users;'; - - if (!$conn->query($sql)) { - throw new Exception('Failed to create MySQL test view: ' . print_r($conn->error_list, true)); - } - - $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) - );'; - - if (!$conn->query($sql)) { - throw new Exception('Failed to create MySQL UserThings table: ' . print_r($conn->error_list, true)); - } + self::deleteTestTables($db); + + $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); } - public static function deleteTestTables(): void + public static function deleteTestTables(PeachySql $db): void { $sql = [ - 'DROP TABLE UserThings', - 'DROP VIEW vUsers', - 'DROP TABLE Users', + 'DROP TABLE IF EXISTS UserThings', + 'DROP TABLE IF EXISTS Users', ]; foreach ($sql as $query) { - if (self::$mysqlConn) { - if (!self::$mysqlConn->query($query)) { - throw new Exception('Failed to drop MySQL test table: ' . print_r(self::$mysqlConn->error_list, true)); - } - } - - if (self::$sqlsrvConn) { - if (!sqlsrv_query(self::$sqlsrvConn, $query)) { - throw new Exception('Failed to drop SQL Server test table: ' . print_r(sqlsrv_errors(), true)); - } - } + $db->query($query); } } } diff --git a/test/src/ModernUsers.php b/test/src/ModernUsers.php index 07258bc..c972c51 100644 --- a/test/src/ModernUsers.php +++ b/test/src/ModernUsers.php @@ -32,7 +32,7 @@ protected function getSelectProps(): array }; return [ - new Prop('id', 'u.u_id'), + new Prop('id', 'u.user_id'), new Prop('name', 'name', false, true, 'username'), new Prop('isDisabled', 'isDisabled', false, true, '', 'bool'), new Prop('computed', '', false, true, '', null, false, $getValue, ['weight']), @@ -49,8 +49,8 @@ protected function getDefaultSort(): array protected function getBaseQuery(QueryOptions $options): string { return "SELECT {$options->getColumns()} - FROM vUsers u - LEFT JOIN UserThings ut ON ut.user_id = u.u_id"; + FROM Users u + LEFT JOIN UserThings ut ON ut.user_id = u.user_id"; } protected function processValues(array $data, array $ids): array