From 23ed2215dd468a4d9d5f488ecefa66d0118efc50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Zieli=C5=84ski?= Date: Tue, 30 Apr 2024 03:55:08 +0200 Subject: [PATCH] CI: Run PHPUnit tests (#99) Co-authored-by: Ari Stathopoulos Co-authored-by: Brandon Payton --- .github/workflows/phpunit-tests-run.yml | 47 +++++++++++++++++++ .github/workflows/phpunit-tests.yml | 28 +++++++++++ composer.json | 10 ++-- tests/WP_SQLite_Metadata_Tests.php | 1 + ...QLite_PDO_User_Defined_Functions_Tests.php | 2 +- tests/WP_SQLite_Query_Tests.php | 1 + tests/WP_SQLite_Translator_Tests.php | 30 ++++++------ tests/bootstrap.php | 2 +- 8 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/phpunit-tests-run.yml create mode 100644 .github/workflows/phpunit-tests.yml diff --git a/.github/workflows/phpunit-tests-run.yml b/.github/workflows/phpunit-tests-run.yml new file mode 100644 index 0000000..f460a7b --- /dev/null +++ b/.github/workflows/phpunit-tests-run.yml @@ -0,0 +1,47 @@ +name: Run PHPUnit tests + +on: + workflow_call: + inputs: + os: + description: 'Operating system to run tests on' + required: false + type: 'string' + default: 'ubuntu-latest' + php: + description: 'The version of PHP to use, in the format of X.Y' + required: true + type: 'string' + phpunit-config: + description: 'The PHPUnit configuration file to use' + required: false + type: 'string' + default: 'phpunit.xml.dist' +env: + LOCAL_PHP: ${{ inputs.php }}-fpm + PHPUNIT_CONFIG: ${{ inputs.phpunit-config }} + +jobs: + phpunit-tests: + name: ${{ inputs.os }} + runs-on: ${{ inputs.os }} + timeout-minutes: 20 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '${{ inputs.php }}' + tools: phpunit-polyfills + + - name: Install Composer dependencies + uses: ramsey/composer-install@v3 + with: + ignore-cache: "yes" + composer-options: "--optimize-autoloader" + + - name: Run PHPUnit tests + run: php ./vendor/bin/phpunit -c ./phpunit.xml.dist diff --git a/.github/workflows/phpunit-tests.yml b/.github/workflows/phpunit-tests.yml new file mode 100644 index 0000000..19e770a --- /dev/null +++ b/.github/workflows/phpunit-tests.yml @@ -0,0 +1,28 @@ +name: PHPUnit Tests + +on: + push: + branches: + - main + pull_request: + +jobs: + test: + name: PHP ${{ matrix.php }} + uses: ./.github/workflows/phpunit-tests-run.yml + permissions: + contents: read + secrets: inherit + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest ] + # NOTE: There does not appear to be a single phpunit version that supports all + # PHP versions tested here. For now, we are removing PHP 7.0. and 7.1 tests + # in order to run a single phpunit version for PHP 7.2 and up. + php: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ] + + with: + os: ${{ matrix.os }} + php: ${{ matrix.php }} + phpunit-config: ${{ 'phpunit.xml.dist' }} diff --git a/composer.json b/composer.json index d889d0d..fb6b289 100644 --- a/composer.json +++ b/composer.json @@ -8,15 +8,16 @@ "issues": "https://github.com/wordpress/sqlite-database-integration/issues" }, "require": { - "php": ">=5.6" + "php": ">=7.0" }, "require-dev": { "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", "squizlabs/php_codesniffer": "^3.7", "wp-coding-standards/wpcs": "^3.1", - "yoast/phpunit-polyfills": "^1.0.1", "phpcompatibility/phpcompatibility-wp": "*", - "php-parallel-lint/php-parallel-lint": "^1.3" + "php-parallel-lint/php-parallel-lint": "^1.3", + "yoast/phpunit-polyfills": "2.0.0", + "phpunit/phpunit": "8.5.38" }, "config": { "allow-plugins": { @@ -30,6 +31,9 @@ ], "fix-cs": [ "@php ./vendor/bin/phpcbf" + ], + "test": [ + "phpunit" ] } } diff --git a/tests/WP_SQLite_Metadata_Tests.php b/tests/WP_SQLite_Metadata_Tests.php index dd1d3f1..535fd22 100644 --- a/tests/WP_SQLite_Metadata_Tests.php +++ b/tests/WP_SQLite_Metadata_Tests.php @@ -24,6 +24,7 @@ public static function setUpBeforeClass(): void { $GLOBALS['wpdb']->suppress_errors = false; $GLOBALS['wpdb']->show_errors = true; } + return; } // Before each test, we create a new database diff --git a/tests/WP_SQLite_PDO_User_Defined_Functions_Tests.php b/tests/WP_SQLite_PDO_User_Defined_Functions_Tests.php index d74715b..9824148 100644 --- a/tests/WP_SQLite_PDO_User_Defined_Functions_Tests.php +++ b/tests/WP_SQLite_PDO_User_Defined_Functions_Tests.php @@ -18,7 +18,7 @@ public function testFieldFunction( $expected, $args ) { ); } - public function dataProviderForTestFieldFunction() { + public static function dataProviderForTestFieldFunction() { return array( array( 1, array( 'a', 'a' ) ), array( 2, array( 'User 0000019', 'User 0000018', 'User 0000019', 'User 0000020' ) ), diff --git a/tests/WP_SQLite_Query_Tests.php b/tests/WP_SQLite_Query_Tests.php index 8da2e96..059fed9 100644 --- a/tests/WP_SQLite_Query_Tests.php +++ b/tests/WP_SQLite_Query_Tests.php @@ -27,6 +27,7 @@ public static function setUpBeforeClass(): void { $GLOBALS['wpdb']->suppress_errors = false; $GLOBALS['wpdb']->show_errors = true; } + return; } /** diff --git a/tests/WP_SQLite_Translator_Tests.php b/tests/WP_SQLite_Translator_Tests.php index f30c6f6..95b8db2 100644 --- a/tests/WP_SQLite_Translator_Tests.php +++ b/tests/WP_SQLite_Translator_Tests.php @@ -24,6 +24,7 @@ public static function setUpBeforeClass(): void { $GLOBALS['wpdb']->suppress_errors = false; $GLOBALS['wpdb']->show_errors = true; } + return; } // Before each test, we create a new database @@ -92,7 +93,7 @@ public function testRegexps( $operator, $regexp, $expected_result ) { ); } - public function regexpOperators() { + public static function regexpOperators() { $lowercase_rss = (object) array( 'ID' => '1', 'option_name' => 'rss_123', @@ -255,26 +256,27 @@ public function testSelectFromDual() { public function testShowCreateTable1() { $this->assertQuery( "CREATE TABLE _tmp_table ( - ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, - option_name VARCHAR(255) default '', - option_value TEXT NOT NULL, - UNIQUE KEY option_name (option_name), - KEY composite (option_name, option_value) - );" + ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, + option_name VARCHAR(255) default '', + option_value TEXT NOT NULL, + UNIQUE KEY option_name (option_name), + KEY composite (option_name, option_value) + );" ); $this->assertQuery( 'SHOW CREATE TABLE _tmp_table;' ); $results = $this->engine->get_query_results(); + # TODO: Should we fix mismatch with original `option_value` text NOT NULL,` without default? $this->assertEquals( "CREATE TABLE _tmp_table ( - `ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL, - `option_name` varchar(255) DEFAULT '', - `option_value` text NOT NULL, - KEY _tmp_table__composite (option_name, option_value), - UNIQUE KEY _tmp_table__option_name (option_name) - );", + `ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL, + `option_name` varchar(255) DEFAULT '', + `option_value` text NOT NULL DEFAULT '', + KEY _tmp_table__composite (option_name, option_value), + UNIQUE KEY _tmp_table__option_name (option_name) +);", $results[0]->{'Create Table'} ); } @@ -323,7 +325,7 @@ public function testShowCreateTableWithAlterAndCreateIndex() { 'CREATE TABLE _tmp_table ( `ID` bigint PRIMARY KEY AUTO_INCREMENT NOT NULL, `option_name` smallint NOT NULL DEFAULT 14, - `option_value` text NOT NULL, + `option_value` text NOT NULL DEFAULT \'\', KEY _tmp_table__option_name (option_name) );', $results[0]->{'Create Table'} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6b8068b..4aa4045 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -80,7 +80,7 @@ function str_contains( string $haystack, string $needle ) { * @return bool */ function str_ends_with( string $haystack, string $needle ) { - return empty( $needle ) || substr( $haystack, -strlen( $needle ) === $needle ); + return empty( $needle ) || substr( $haystack, -strlen( $needle ) ) === $needle; } } if ( extension_loaded( 'mbstring' ) ) {