diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ce2a2089..730b33ef 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,7 +3,28 @@ name: CI on: [push, pull_request] jobs: + phpcs: + name: Code style + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + tools: phpcs + + - name: Check production code style + run: composer cs-prod + + - name: Check test code style + run: composer cs-tests + tests: + name: Unit tests runs-on: ubuntu-latest services: @@ -53,7 +74,7 @@ jobs: run: /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P 'P@ssw0rd' -Q 'CREATE DATABASE codeception_test' - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/composer.json b/composer.json index 26c633a0..77eefd0e 100644 --- a/composer.json +++ b/composer.json @@ -21,12 +21,26 @@ "ext-pdo": "*", "codeception/codeception": "*@dev" }, + "require-dev": { + "squizlabs/php_codesniffer": "*" + }, "conflict": { "codeception/codeception": "<5.0" }, "autoload":{ "classmap": ["src/"] }, + "autoload-dev": { + "classmap": ["tests/"] + }, + "scripts": { + "cs-prod": "phpcs src/", + "cs-tests": "phpcs tests/ --standard=tests/phpcs.xml" + }, + "scripts-descriptions": { + "cs-prod": "Check production code style", + "cs-tests": "Check test code style" + }, "config": { "classmap-authoritative": true, "sort-packages": true diff --git a/docker-compose.amd64.yml b/docker-compose.amd64.yml index 3af4ab83..3fb698ba 100644 --- a/docker-compose.amd64.yml +++ b/docker-compose.amd64.yml @@ -1,5 +1,3 @@ -version: '3.9' - services: php: platform: linux/amd64 diff --git a/docker-compose.yml b/docker-compose.yml index 5bb5a742..d2052501 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: "3.9" - services: php: container_name: codeception-module-db diff --git a/phpcs.xml b/phpcs.xml new file mode 100644 index 00000000..0fd59960 --- /dev/null +++ b/phpcs.xml @@ -0,0 +1,8 @@ + + + Codeception code standard + + + + + diff --git a/src/Codeception/Lib/DbPopulator.php b/src/Codeception/Lib/DbPopulator.php index f57a8cd0..59d9b23b 100644 --- a/src/Codeception/Lib/DbPopulator.php +++ b/src/Codeception/Lib/DbPopulator.php @@ -74,7 +74,7 @@ protected function buildCommand(string $command, string $dumpFile = null): strin foreach ($vars as $key => $value) { if (!is_array($value)) { - $vars['$'.$key] = $value; + $vars['$' . $key] = $value; } unset($vars[$key]); diff --git a/src/Codeception/Lib/Driver/MySql.php b/src/Codeception/Lib/Driver/MySql.php index c2baa1ff..6fd79e2e 100644 --- a/src/Codeception/Lib/Driver/MySql.php +++ b/src/Codeception/Lib/Driver/MySql.php @@ -43,7 +43,7 @@ public function getPrimaryKey(string $tableName): array $columns = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($columns as $column) { - $primaryKey []= $column['Column_name']; + $primaryKey[] = $column['Column_name']; } $this->primaryKeys[$tableName] = $primaryKey; } diff --git a/src/Codeception/Lib/Driver/Oci.php b/src/Codeception/Lib/Driver/Oci.php index 7d3c58a5..54bf2c58 100644 --- a/src/Codeception/Lib/Driver/Oci.php +++ b/src/Codeception/Lib/Driver/Oci.php @@ -104,7 +104,7 @@ public function getPrimaryKey(string $tableName): array $columns = $stmt->fetchAll(\PDO::FETCH_ASSOC); foreach ($columns as $column) { - $primaryKey []= $column['COLUMN_NAME']; + $primaryKey[] = $column['COLUMN_NAME']; } $this->primaryKeys[$tableName] = $primaryKey; diff --git a/src/Codeception/Lib/Driver/PostgreSql.php b/src/Codeception/Lib/Driver/PostgreSql.php index 3f7336d5..45bed775 100644 --- a/src/Codeception/Lib/Driver/PostgreSql.php +++ b/src/Codeception/Lib/Driver/PostgreSql.php @@ -169,7 +169,7 @@ public function getPrimaryKey(string $tableName): array $stmt = $this->executeQuery($query, []); $columns = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($columns as $column) { - $primaryKey []= $column['attname']; + $primaryKey[] = $column['attname']; } $this->primaryKeys[$tableName] = $primaryKey; diff --git a/src/Codeception/Lib/Driver/SqlSrv.php b/src/Codeception/Lib/Driver/SqlSrv.php index f742fe41..d1826b1e 100644 --- a/src/Codeception/Lib/Driver/SqlSrv.php +++ b/src/Codeception/Lib/Driver/SqlSrv.php @@ -77,7 +77,7 @@ public function getPrimaryKey(string $tableName): array $columns = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($columns as $column) { - $primaryKey []= $column['Column_Name']; + $primaryKey[] = $column['Column_Name']; } $this->primaryKeys[$tableName] = $primaryKey; diff --git a/src/Codeception/Lib/Driver/Sqlite.php b/src/Codeception/Lib/Driver/Sqlite.php index 242d9306..f480c76d 100644 --- a/src/Codeception/Lib/Driver/Sqlite.php +++ b/src/Codeception/Lib/Driver/Sqlite.php @@ -11,7 +11,7 @@ class Sqlite extends Db { protected bool $hasSnapshot = false; - + protected string $filename = ''; public function __construct(string $dsn, string $user = null, string $password = null, array $options = null) @@ -47,7 +47,7 @@ public function load(array $sql): void if (file_exists($this->filename . '_snapshot')) { unlink($this->filename . '_snapshot'); } - + parent::load($sql); copy($this->filename, $this->filename . '_snapshot'); $this->hasSnapshot = true; @@ -71,7 +71,7 @@ public function getPrimaryKey(string $tableName): array foreach ($columns as $column) { if ($column['pk'] !== '0' && $column['pk'] !== 0) { - $primaryKey []= $column['name']; + $primaryKey[] = $column['name']; } } diff --git a/src/Codeception/Module/Db.php b/src/Codeception/Module/Db.php index db418bd3..a27299f2 100644 --- a/src/Codeception/Module/Db.php +++ b/src/Codeception/Module/Db.php @@ -565,38 +565,43 @@ private function connect($databaseKey, $databaseConfig): void $options = []; - if (array_key_exists('ssl_key', $databaseConfig) + if ( + array_key_exists('ssl_key', $databaseConfig) && !empty($databaseConfig['ssl_key']) && defined(PDO::class . '::MYSQL_ATTR_SSL_KEY') ) { $options[PDO::MYSQL_ATTR_SSL_KEY] = (string) $databaseConfig['ssl_key']; } - if (array_key_exists('ssl_cert', $databaseConfig) + if ( + array_key_exists('ssl_cert', $databaseConfig) && !empty($databaseConfig['ssl_cert']) && defined(PDO::class . '::MYSQL_ATTR_SSL_CERT') ) { $options[PDO::MYSQL_ATTR_SSL_CERT] = (string) $databaseConfig['ssl_cert']; } - if (array_key_exists('ssl_ca', $databaseConfig) + if ( + array_key_exists('ssl_ca', $databaseConfig) && !empty($databaseConfig['ssl_ca']) && defined(PDO::class . '::MYSQL_ATTR_SSL_CA') ) { $options[PDO::MYSQL_ATTR_SSL_CA] = (string) $databaseConfig['ssl_ca']; } - if (array_key_exists('ssl_cipher', $databaseConfig) + if ( + array_key_exists('ssl_cipher', $databaseConfig) && !empty($databaseConfig['ssl_cipher']) && defined(PDO::class . '::MYSQL_ATTR_SSL_CIPHER') ) { $options[PDO::MYSQL_ATTR_SSL_CIPHER] = (string) $databaseConfig['ssl_cipher']; } - if (array_key_exists('ssl_verify_server_cert', $databaseConfig) + if ( + array_key_exists('ssl_verify_server_cert', $databaseConfig) && defined(PDO::class . '::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT') ) { - $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = (boolean) $databaseConfig[ 'ssl_verify_server_cert' ]; + $options[PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT] = (bool) $databaseConfig[ 'ssl_verify_server_cert' ]; } try { @@ -672,7 +677,7 @@ protected function removeInserted($databaseKey = null): void try { $this->_getDriver()->deleteQueryByCriteria($row['table'], $row['primary']); } catch (Exception $e) { - $this->debug("Couldn't delete record " . json_encode($row['primary'], JSON_THROW_ON_ERROR) ." from {$row['table']}"); + $this->debug("Couldn't delete record " . json_encode($row['primary'], JSON_THROW_ON_ERROR) . " from {$row['table']}"); } } diff --git a/tests/phpcs.xml b/tests/phpcs.xml new file mode 100644 index 00000000..df0ae9c7 --- /dev/null +++ b/tests/phpcs.xml @@ -0,0 +1,9 @@ + + + Codeception code standard + + + + + + diff --git a/tests/support/UnitTester.php b/tests/support/UnitTester.php index e19544a5..b3472f63 100644 --- a/tests/support/UnitTester.php +++ b/tests/support/UnitTester.php @@ -1,6 +1,5 @@ assertSame($expectedResult, $result); } @@ -25,14 +21,14 @@ public function testGenerateWhereClause(array $criteria, string $expectedResult) public function getWhereCriteria(): array { return [ - 'like' => [['email like' => 'mail.ua'], 'WHERE "email" LIKE ? '], - '<=' => [['id <=' => '5'], 'WHERE "id" <= ? '], - '<' => [['id <' => '5'], 'WHERE "id" < ? '], - '>=' => [['id >=' => '5'], 'WHERE "id" >= ? '], - '>' => [['id >' => '5'], 'WHERE "id" > ? '], - '!=' => [['id !=' => '5'], 'WHERE "id" != ? '], - 'is null' => [['id' => null], 'WHERE "id" IS NULL '], - 'is not null' => [['id !=' => null], 'WHERE "id" IS NOT NULL '], + 'like' => [['email like' => 'mail.ua'], 'WHERE "email" LIKE ? '], + '<=' => [['id <=' => '5'], 'WHERE "id" <= ? '], + '<' => [['id <' => '5'], 'WHERE "id" < ? '], + '>=' => [['id >=' => '5'], 'WHERE "id" >= ? '], + '>' => [['id >' => '5'], 'WHERE "id" > ? '], + '!=' => [['id !=' => '5'], 'WHERE "id" != ? '], + 'is null' => [['id' => null], 'WHERE "id" IS NULL '], + 'is not null' => [['id !=' => null], 'WHERE "id" IS NOT NULL '], ]; } } diff --git a/tests/unit/Codeception/Lib/Driver/MysqlTest.php b/tests/unit/Codeception/Lib/Driver/MysqlTest.php index 94ea59f6..205ed230 100644 --- a/tests/unit/Codeception/Lib/Driver/MysqlTest.php +++ b/tests/unit/Codeception/Lib/Driver/MysqlTest.php @@ -7,10 +7,6 @@ use Codeception\Lib\Driver\MySql; use Codeception\Test\Unit; -/** - * @group appveyor - * @group db - */ final class MysqlTest extends Unit { protected static array $config = [ @@ -69,9 +65,6 @@ public function testCleanupDatabase() $this->assertEmpty($this->mysql->getDbh()->query("SHOW TABLES")->fetchAll()); } - /** - * @group appveyor - */ public function testLoadDump() { $res = $this->mysql->getDbh()->query("select * from users where name = 'davert'"); @@ -108,7 +101,7 @@ public function testInsertIntoBitField() { $res = $this->mysql->executeQuery( "insert into `users`(`id`,`name`,`email`,`is_active`,`created_at`) values (?,?,?,?,?)", - [5,'insert.test','insert.test@mail.ua',false,'2012-02-01 21:17:47'] + [5, 'insert.test', 'insert.test@mail.ua', false, '2012-02-01 21:17:47'] ); $this->assertSame(1, $res->rowCount()); } @@ -123,7 +116,7 @@ public function testLoadThrowsExceptionWhenDumpFileContainsSyntaxError() 'check the manual that corresponds to your MySQL server version for the right syntax to use near ' . "'VALS('')' at line 1\nSQL query being executed: \n" . $sql; $this->expectException(ModuleException::class); - $this->expectExceptionMessage( $expectedMessage); + $this->expectExceptionMessage($expectedMessage); $this->mysql->load([$sql]); } } diff --git a/tests/unit/Codeception/Lib/Driver/PostgresTest.php b/tests/unit/Codeception/Lib/Driver/PostgresTest.php index b0982021..8c15e863 100644 --- a/tests/unit/Codeception/Lib/Driver/PostgresTest.php +++ b/tests/unit/Codeception/Lib/Driver/PostgresTest.php @@ -6,10 +6,6 @@ use Codeception\Lib\Driver\PostgreSql; use Codeception\Test\Unit; -/** - * @group appveyor - * @group db - */ final class PostgresTest extends Unit { protected static array $config = [ @@ -141,7 +137,7 @@ public function testGetEmptyArrayIfTableHasNoPrimaryKey() public function testLastInsertIdReturnsSequenceValueWhenNonStandardSequenceNameIsUsed() { - $this->postgres->executeQuery('INSERT INTO seqnames(name) VALUES(?)',['test']); + $this->postgres->executeQuery('INSERT INTO seqnames(name) VALUES(?)', ['test']); $this->assertSame('1', $this->postgres->lastInsertId('seqnames')); } diff --git a/tests/unit/Codeception/Lib/Driver/SqliteTest.php b/tests/unit/Codeception/Lib/Driver/SqliteTest.php index e04dda80..680ea0bb 100644 --- a/tests/unit/Codeception/Lib/Driver/SqliteTest.php +++ b/tests/unit/Codeception/Lib/Driver/SqliteTest.php @@ -7,10 +7,6 @@ use Codeception\Lib\Driver\Sqlite; use Codeception\Test\Unit; -/** - * @group db - * Class SqliteTest - */ final class SqliteTest extends Unit { /** diff --git a/tests/unit/Codeception/Module/Db/MssqlDblibDbTest.php b/tests/unit/Codeception/Module/Db/MssqlDblibDbTest.php index e9b46fff..5b4a4ccd 100644 --- a/tests/unit/Codeception/Module/Db/MssqlDblibDbTest.php +++ b/tests/unit/Codeception/Module/Db/MssqlDblibDbTest.php @@ -2,13 +2,6 @@ declare(strict_types=1); -use Codeception\Configuration; - -require_once Configuration::testsDir() . 'unit/Codeception/Module/Db/AbstractDbTest.php'; - -/** - * @group db - */ final class MssqlDblibDbTest extends AbstractDbTest { public function getPopulator(): string diff --git a/tests/unit/Codeception/Module/Db/MssqlSqlSrvDbTest.php b/tests/unit/Codeception/Module/Db/MssqlSqlSrvDbTest.php index b758a26b..b54641e8 100644 --- a/tests/unit/Codeception/Module/Db/MssqlSqlSrvDbTest.php +++ b/tests/unit/Codeception/Module/Db/MssqlSqlSrvDbTest.php @@ -2,13 +2,6 @@ declare(strict_types=1); -use Codeception\Configuration; - -require_once Configuration::testsDir() . 'unit/Codeception/Module/Db/AbstractDbTest.php'; - -/** - * @group db - */ final class MssqlSqlSrvDbTest extends AbstractDbTest { public function getPopulator(): string diff --git a/tests/unit/Codeception/Module/Db/MySqlDbTest.php b/tests/unit/Codeception/Module/Db/MySqlDbTest.php index 11dcfee0..8bc10015 100644 --- a/tests/unit/Codeception/Module/Db/MySqlDbTest.php +++ b/tests/unit/Codeception/Module/Db/MySqlDbTest.php @@ -2,21 +2,15 @@ declare(strict_types=1); -use Codeception\Configuration; use Codeception\Stub; use Codeception\TestInterface; -require_once Configuration::testsDir().'unit/Codeception/Module/Db/AbstractDbTest.php'; - -/** - * @group db - */ final class MySqlDbTest extends AbstractDbTest { public function getPopulator(): string { $config = $this->getConfig(); - $password = $config['password'] ? '-p'.$config['password'] : ''; + $password = $config['password'] ? '-p' . $config['password'] : ''; return sprintf('mysql -u $user %s $dbname < %s', $password, $config['dump']); } @@ -103,7 +97,8 @@ public function testGrabColumnFromDatabase() 'miles@davis.com', 'charlie@parker.com', ], - $emails); + $emails + ); } public function testGrabEntryFromDatabaseShouldFailIfNotFound() diff --git a/tests/unit/Codeception/Module/Db/Populator/DbPopulatorTest.php b/tests/unit/Codeception/Module/Db/Populator/DbPopulatorTest.php index eb703f48..6490bf5c 100644 --- a/tests/unit/Codeception/Module/Db/Populator/DbPopulatorTest.php +++ b/tests/unit/Codeception/Module/Db/Populator/DbPopulatorTest.php @@ -5,10 +5,6 @@ use Codeception\Lib\DbPopulator; use Codeception\Test\Unit; -/** - * @group db - * Class DbPopulatorTest - */ final class DbPopulatorTest extends Unit { public function testCommandBuilderInterpolatesVariables() diff --git a/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php b/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php index 9ebf1dd3..0f2c4900 100644 --- a/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php +++ b/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php @@ -2,13 +2,6 @@ declare(strict_types=1); -use Codeception\Configuration; - -require_once Configuration::testsDir().'unit/Codeception/Module/Db/AbstractDbTest.php'; - -/** - * @group db - */ final class PostgreSqlDbTest extends AbstractDbTest { public function getPopulator(): string diff --git a/tests/unit/Codeception/Module/Db/SqliteDbTest.php b/tests/unit/Codeception/Module/Db/SqliteDbTest.php index 82871f49..cdbeb97d 100644 --- a/tests/unit/Codeception/Module/Db/SqliteDbTest.php +++ b/tests/unit/Codeception/Module/Db/SqliteDbTest.php @@ -2,31 +2,17 @@ declare(strict_types=1); -use Codeception\Configuration; use Codeception\Stub; use Codeception\TestInterface; use Codeception\Util\ActionSequence; -require_once Configuration::testsDir().'unit/Codeception/Module/Db/AbstractDbTest.php'; - -/** - * @group appveyor - * @group db - * Class SqliteDbTest - */ final class SqliteDbTest extends AbstractDbTest { public function getPopulator() { - if (getenv('APPVEYOR')) { - $this->markTestSkipped('Disabled on Appveyor'); - } - - $this->markTestSkipped('Currently Travis CI uses old SQLite :('); - $config = $this->getConfig(); @chmod('tests/data/sqlite.db', 0777); - return 'cat '. $config['dump'] .' | sqlite3 tests/data/sqlite.db'; + return 'cat ' . $config['dump'] . ' | sqlite3 tests/data/sqlite.db'; } public function getConfig(): array @@ -83,7 +69,7 @@ public function testMultiDatabase() ]); $this->module->_reconfigure( [ - 'databases' => ['db2' => $config], + 'databases' => ['db2' => $config], ] ); $this->module->_beforeSuite(); @@ -107,7 +93,7 @@ public function testDatabaseIsAlwaysDefaultBeforeTest() $this->module->_reconfigure( [ 'cleanup' => false, - 'databases' => ['db2' => $config], + 'databases' => ['db2' => $config], ] ); $this->module->_beforeSuite(); @@ -142,7 +128,7 @@ public function testMultiDatabaseWithArray() ]); $this->module->_reconfigure( [ - 'databases' => ['db2' => $config], + 'databases' => ['db2' => $config], ] ); $this->module->_beforeSuite(); @@ -167,7 +153,7 @@ public function testMultiDatabaseWithActionSequence() ]); $this->module->_reconfigure( [ - 'databases' => ['db2' => $config], + 'databases' => ['db2' => $config], ] ); $this->module->_beforeSuite(); @@ -176,9 +162,11 @@ public function testMultiDatabaseWithActionSequence() $testDataInDb2 = ['name' => 'userdb2', 'email' => 'userdb2@example.org']; $this->module->_insertInDatabase('users', $testDataInDb1); - $this->module->performInDatabase('db2', ActionSequence::build() - ->haveInDatabase('users', $testDataInDb2) - ->seeInDatabase('users', $testDataInDb2) + $this->module->performInDatabase( + 'db2', + ActionSequence::build() + ->haveInDatabase('users', $testDataInDb2) + ->seeInDatabase('users', $testDataInDb2) ); $this->module->seeInDatabase('users', $testDataInDb1); $this->module->dontSeeInDatabase('users', $testDataInDb2); @@ -192,7 +180,7 @@ public function testMultiDatabaseWithAnonymousFunction() ]); $this->module->_reconfigure( [ - 'databases' => ['db2' => $config], + 'databases' => ['db2' => $config], ] ); $this->module->_beforeSuite(); @@ -225,7 +213,7 @@ public function testMultiDatabaseWithRemoveInserted() ]); $this->module->_reconfigure( [ - 'databases' => ['db2' => $config], + 'databases' => ['db2' => $config], ] ); $this->module->_beforeSuite();