diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d7fb8c20..b19740dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] Unreleased +### Fixed + +- Disable strict connection ID check for SQLite databases to avoid errors in tests. + ## [3.6.2] 2024-05-25; ### Changed diff --git a/src/Module/WPLoader.php b/src/Module/WPLoader.php index 1aa72b249..04c76fade 100644 --- a/src/Module/WPLoader.php +++ b/src/Module/WPLoader.php @@ -1044,7 +1044,11 @@ private function includeCorePHPUniteSuiteBootstrapFile(): void try { require $this->wpBootstrapFile; - WPTestCase::setWpdbConnectionId((string)$GLOBALS['wpdb']->get_var('SELECT CONNECTION_ID()')); + if ($GLOBALS['wpdb'] instanceof \WP_SQLite_DB) { + WPTestCase::beStrictAboutWpdbConnectionId(false); + } else { + WPTestCase::setWpdbConnectionId((string)$GLOBALS['wpdb']->get_var('SELECT CONNECTION_ID()')); + } } catch (Throwable $t) { // Not an early exit: Codeception will handle the Exception and print it. $this->earlyExit = false; diff --git a/tests/unit/lucatume/WPBrowser/Module/WPTestCaseStrictTest.php b/tests/unit/lucatume/WPBrowser/Module/WPTestCaseStrictTest.php index b280362a7..132db89c6 100644 --- a/tests/unit/lucatume/WPBrowser/Module/WPTestCaseStrictTest.php +++ b/tests/unit/lucatume/WPBrowser/Module/WPTestCaseStrictTest.php @@ -13,6 +13,7 @@ use lucatume\WPBrowser\Utils\Filesystem as FS; use lucatume\WPBrowser\Utils\Random; use lucatume\WPBrowser\WordPress\Database\MysqlDatabase; +use lucatume\WPBrowser\WordPress\Database\SQLiteDatabase; use lucatume\WPBrowser\WordPress\Installation; use PHPUnit\Framework\Assert; @@ -168,4 +169,104 @@ public function test_something():void{ Assert::assertNotSame($connectionId, $GLOBALS['wpdb']->get_var('SELECT CONNECTION_ID()')); }); } + + /** + * It should not be strict about wpdb connection id in SQLite database + * + * @test + */ + public function should_not_be_strict_about_wpdb_connection_id_in_sq_lite_database(): void + { + $wpRootDir = FS::tmpDir('wploader_'); + $db = new SQLiteDatabase($wpRootDir, 'db.sqlite', 'wp_'); + Installation::scaffold($wpRootDir); + $db->create(); + $testcaseFile = $wpRootDir . '/BreakingTest.php'; + $testCaseFileContents = <<close(); + + parent::set_up_before_class(); + } + + public function test_something():void{ + \$this->assertTrue(true); + } + } +PHP; + if(!file_put_contents($testcaseFile, $testCaseFileContents, LOCK_EX)) { + throw new \RuntimeException('Could not write BreakingTest.php.'); + } + + // Run a test using the default value, strict. + $this->config = [ + 'wpRootFolder' => $wpRootDir, + 'dbUrl' => $db->getDbUrl() + ]; + $wpLoader = $this->module(); + + // Run a test using the default value, strict. It will not fail since strict mode is disabled for SQLite. + $this->config = [ + 'wpRootFolder' => $wpRootDir, + 'dbUrl' => $db->getDbUrl() + ]; + $wpLoader = $this->module(); + + $this->assertInIsolation(static function () use ($wpLoader, $testcaseFile) { + $wpLoader->_initialize(); + $connectionId = WPTestCase::getWpdbConnectionId(); + Assert::assertNull($connectionId); + Assert::assertFalse(WPTestCase::isStrictAboutWpdbConnectionId()); + + require_once $testcaseFile; + + \BreakingTest::setUpBeforeClass(); + }); + + // Run a test in strict mode. It will fail since strict mode is disabled for SQLite. + $this->config = [ + 'wpRootFolder' => $wpRootDir, + 'dbUrl' => $db->getDbUrl(), + 'beStrictAboutWpdbConnectionId' => true + ]; + $wpLoader = $this->module(); + + $this->assertInIsolation(static function () use ($wpLoader, $testcaseFile) { + $wpLoader->_initialize(); + $connectionId = WPTestCase::getWpdbConnectionId(); + Assert::assertNull($connectionId); + Assert::assertFalse(WPTestCase::isStrictAboutWpdbConnectionId()); + + require_once $testcaseFile; + + \BreakingTest::setUpBeforeClass(); + }); + + // Run a test in non-strict mode. + $this->config = [ + 'wpRootFolder' => $wpRootDir, + 'dbUrl' => $db->getDbUrl(), + 'beStrictAboutWpdbConnectionId' => false + ]; + $wpLoader = $this->module(); + + $this->assertInIsolation(static function () use ($wpLoader, $testcaseFile) { + $wpLoader->_initialize(); + $connectionId = WPTestCase::getWpdbConnectionId(); + Assert::assertNull($connectionId); + Assert::assertFalse(WPTestCase::isStrictAboutWpdbConnectionId()); + + require_once $testcaseFile; + + \BreakingTest::setUpBeforeClass(); + }); + } }