Skip to content

Commit

Permalink
Merge pull request #731 from lucatume/v4-fix-connection-id-on-sqlite
Browse files Browse the repository at this point in the history
v4 fix connection id on sqlite
  • Loading branch information
lucatume authored Jun 3, 2024
2 parents a37d494 + b4ec80b commit cf14c67
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ 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.

## [4.2.2] 2024-05-25;

### Changed

- Better messaging when throwing due to disconnected database.
Expand Down
6 changes: 5 additions & 1 deletion src/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,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 cf14c67

Please sign in to comment.