Skip to content

Commit

Permalink
Merge pull request #732 from lucatume/v35-fix-connection-id-on-sqlite
Browse files Browse the repository at this point in the history
fix(WPLoader) disable connection ID check on SQLite
  • Loading branch information
lucatume authored Jun 3, 2024
2 parents 1934125 + 5a8f89e commit 8cc2a97
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
101 changes: 101 additions & 0 deletions tests/unit/lucatume/WPBrowser/Module/WPTestCaseStrictTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = <<<PHP
<?php
use lucatume\\WPBrowser\\TestCase\\WPTestCase;
class BreakingTest extends WPTestCase
{
public static function setUpBeforeClass():void
{
global \$wpdb;
\$wpdb->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();
});
}
}

0 comments on commit 8cc2a97

Please sign in to comment.