Skip to content

Commit

Permalink
Simplify DbConnector and remove unnecessary test view
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Oct 22, 2024
1 parent 411b450 commit 23dff01
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 122 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,5 +44,3 @@ jobs:

- name: Run PHPUnit
run: vendor/bin/phpunit
env:
DB_PORT: ${{ job.services.mysql.ports[3306] }}
11 changes: 3 additions & 8 deletions test/EntitiesDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{0: PeachySql}>
*/
Expand All @@ -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;
Expand Down
173 changes: 67 additions & 106 deletions test/src/DbConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions test/src/ModernUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand All @@ -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
Expand Down

0 comments on commit 23dff01

Please sign in to comment.