diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 8addda1..eb60556 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -43,4 +43,4 @@ jobs: if: ${{ matrix.php == '8.3' }} - name: Run PHPUnit - run: vendor/bin/phpunit + run: composer test-mysql diff --git a/composer.json b/composer.json index c0ae879..0ebf855 100644 --- a/composer.json +++ b/composer.json @@ -32,6 +32,8 @@ }, "scripts": { "analyze": "psalm", - "test": "phpunit" + "test": "phpunit", + "test-mssql": "phpunit --exclude-group mysql", + "test-mysql": "phpunit --exclude-group mssql" } } diff --git a/test/EntitiesDbTest.php b/test/DbTestCase.php similarity index 75% rename from test/EntitiesDbTest.php rename to test/DbTestCase.php index 899f193..36496af 100644 --- a/test/EntitiesDbTest.php +++ b/test/DbTestCase.php @@ -6,77 +6,26 @@ use PeachySQL\PeachySql; use PHPUnit\Framework\TestCase; -use theodorejb\Phaster\Entities; -use theodorejb\Phaster\Test\src\{DbConnector, Users, LegacyUsers, ModernUsers}; +use theodorejb\Phaster\Test\src\{Users, LegacyUsers, ModernUsers}; -class EntitiesDbTest extends TestCase +abstract class DbTestCase extends TestCase { - /** - * @return list - */ - public static function dbProvider(): array - { - $config = DbConnector::getConfig(); - $databases = []; - - if ($config->testMysql()) { - $databases[] = [DbConnector::getMysqlConn()]; - } + abstract public static function dbProvider(): PeachySql; - if ($config->testSqlsrv()) { - $databases[] = [DbConnector::getSqlsrvConn()]; - } - - return $databases; - } - - /** - * @return list - */ - public static function entitiesProvider(): array + public static function entitiesProvider(): Users { - $list = []; - - foreach (self::dbProvider() as $db) { - $list[] = [new Users($db[0])]; - } - - return $list; + return new Users(static::dbProvider()); } - /** - * @return list - */ - public static function legacyUsersProvider(): array + public static function legacyUsersProvider(): LegacyUsers { - $list = []; - - foreach (self::dbProvider() as $db) { - $list[] = [new LegacyUsers($db[0])]; - } - - return $list; + return new LegacyUsers(static::dbProvider()); } - /** - * @return list - */ - public static function modernUsersProvider(): array + public function testAddEntities(): void { - $list = []; - - foreach (self::dbProvider() as $db) { - $list[] = [new ModernUsers($db[0]), $db[0]]; - } - - return $list; - } + $entities = static::entitiesProvider(); - /** - * @dataProvider entitiesProvider - */ - public function testAddEntities(Entities $entities): void - { try { $entities->addEntities([[ 'name' => 'My name', @@ -111,11 +60,10 @@ public function testAddEntities(Entities $entities): void $this->assertSame($users, $entities->getEntitiesByIds($ids)); } - /** - * @dataProvider entitiesProvider - */ - public function testUpdateById(Entities $entities): void + public function testUpdateById(): void { + $entities = static::entitiesProvider(); + try { // optional properties should still be required when replacing an entity $entities->updateById(0, [ @@ -150,11 +98,10 @@ public function testUpdateById(Entities $entities): void $this->assertSame($newUser, $entities->getEntityById($id)); } - /** - * @dataProvider entitiesProvider - */ - public function testPatchByIds(Entities $entities): void + public function testPatchByIds(): void { + $entities = static::entitiesProvider(); + $users = [ [ 'name' => 'Name 1', @@ -190,22 +137,19 @@ public function testPatchByIds(Entities $entities): void $this->assertSame([], $entities->getEntitiesByIds($ids)); } - /** - * @dataProvider entitiesProvider - */ - public function testEmptyQueries(Entities $entities): void + public function testEmptyQueries(): void { + $entities = static::entitiesProvider(); $ids = $entities->addEntities([]); $this->assertSame([], $entities->getEntitiesByIds($ids)); $this->assertSame(0, $entities->patchByIds($ids, ['weight' => 10])); $this->assertSame(0, $entities->deleteByIds($ids)); } - /** - * @dataProvider entitiesProvider - */ - public function testDuplicateError(Entities $entities): void + public function testDuplicateError(): void { + $entities = static::entitiesProvider(); + $users = [ [ 'name' => 'Conflicting Name', @@ -227,11 +171,10 @@ public function testDuplicateError(Entities $entities): void } } - /** - * @dataProvider dbProvider - */ - public function testConstraintError(PeachySql $db): void + public function testConstraintError(): void { + $db = static::dbProvider(); + $user = [ 'name' => 'Some Name', 'birthday' => '2001-06-17', @@ -250,11 +193,9 @@ public function testConstraintError(PeachySql $db): void } } - /** - * @dataProvider entitiesProvider - */ - public function testGetEntities(Entities $entities): void + public function testGetEntities(): void { + $entities = static::entitiesProvider(); $users = []; for ($i = 1; $i <= 50; $i++) { @@ -286,11 +227,9 @@ public function testGetEntities(Entities $entities): void $this->assertSame($expected, $actual); } - /** - * @dataProvider entitiesProvider - */ - public function testCountEntities(Entities $entities): void + public function testCountEntities(): void { + $entities = static::entitiesProvider(); $users = []; for ($i = 1; $i <= 20; $i++) { @@ -306,11 +245,9 @@ public function testCountEntities(Entities $entities): void $this->assertSame(10, $actual); } - /** - * @dataProvider legacyUsersProvider - */ - public function testLegacyUsers(Entities $entities): void + public function testLegacyUsers(): void { + $entities = static::legacyUsersProvider(); $users = []; for ($i = 1; $i <= 20; $i++) { @@ -340,11 +277,10 @@ public function testLegacyUsers(Entities $entities): void $this->assertSame([['name' => 'Legacy user 5']], $actual); } - /** - * @dataProvider modernUsersProvider - */ - public function testModernUsers(Entities $entities, PeachySql $db): void + public function testModernUsers(): void { + $db = static::dbProvider(); + $entities = new ModernUsers($db); $users = []; for ($i = 1; $i <= 10; $i++) { diff --git a/test/MssqlDbTest.php b/test/MssqlDbTest.php new file mode 100644 index 0000000..9fafaf2 --- /dev/null +++ b/test/MssqlDbTest.php @@ -0,0 +1,57 @@ +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); + } +} diff --git a/test/MysqlDbTest.php b/test/MysqlDbTest.php new file mode 100644 index 0000000..7bb9266 --- /dev/null +++ b/test/MysqlDbTest.php @@ -0,0 +1,58 @@ +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); + } +} diff --git a/test/bootstrap.php b/test/bootstrap.php index c8ba0a3..54b1b92 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -1,15 +1,13 @@ getMysqlHost(), $c->getMysqlUser(), $c->getMysqlPassword(), $c->getMysqlDatabase()); - - if ($conn->connect_error !== null) { - throw new Exception('Failed to connect to MySQL: ' . $conn->connect_error); - } - - self::$mysqlDb = new Mysql($conn); - self::createMysqlTestTable(self::$mysqlDb); - } - - return self::$mysqlDb; - } - - public static function getSqlsrvConn(): PeachySql - { - if (!self::$mssqlDb) { - $c = self::getConfig(); - $conn = sqlsrv_connect($c->getSqlsrvServer(), $c->getSqlsrvConnInfo()); - - if (!$conn) { - throw new Exception('Failed to connect to SQL server: ' . print_r(sqlsrv_errors(), true)); - } - - self::$mssqlDb = new SqlServer($conn); - self::createSqlServerTestTable(self::$mssqlDb); - } - - return self::$mssqlDb; - } - - private static function createSqlServerTestTable(PeachySql $db): void - { - 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(PeachySql $db): void - { - 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(PeachySql $db): void - { - $sql = [ - 'DROP TABLE IF EXISTS UserThings', - 'DROP TABLE IF EXISTS Users', - ]; - - foreach ($sql as $query) { - $db->query($query); - } - } -}