-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(abstract-testcase) fail on dropped db connection
- Loading branch information
Showing
5 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/unit/lucatume/WPBrowser/Module/WPLoaderArbitraryPluginLocationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/unit/lucatume/WPBrowser/Module/WPLoaderArbitraryThemeLocationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
tests/unit/lucatume/WPBrowser/Module/WPLoaderDbConnectionClosedTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace lucatume\WPBrowser\Module; | ||
|
||
use Codeception\Lib\Di; | ||
use Codeception\Lib\ModuleContainer; | ||
use Codeception\Test\Unit; | ||
use lucatume\WPBrowser\Tests\Traits\LoopIsolation; | ||
use lucatume\WPBrowser\Tests\Traits\TmpFilesCleanup; | ||
use lucatume\WPBrowser\Utils\Env; | ||
use lucatume\WPBrowser\Utils\Filesystem as FS; | ||
use lucatume\WPBrowser\Utils\Random; | ||
use lucatume\WPBrowser\WordPress\Database\MysqlDatabase; | ||
use lucatume\WPBrowser\WordPress\Installation; | ||
use PHPUnit\Framework\Assert; | ||
use PHPUnit\Framework\TestResult; | ||
|
||
class WPLoaderDbConnectionClosedTest extends Unit | ||
{ | ||
use LoopIsolation; | ||
use TmpFilesCleanup; | ||
|
||
private function module(array $moduleContainerConfig = [], ?array $moduleConfig = null): WPLoader | ||
{ | ||
$this->mockModuleContainer = new ModuleContainer(new Di(), $moduleContainerConfig); | ||
return new WPLoader($this->mockModuleContainer, ($moduleConfig ?? $this->config)); | ||
} | ||
|
||
public function test_will_fail_if_db_connection_closed_during_setup_before_class(): void | ||
{ | ||
$wpRootDir = FS::tmpDir('wploader_'); | ||
$dbName = Random::dbName(); | ||
$dbHost = Env::get('WORDPRESS_DB_HOST'); | ||
$dbUser = Env::get('WORDPRESS_DB_USER'); | ||
$dbPassword = Env::get('WORDPRESS_DB_PASSWORD'); | ||
$db = new MysqlDatabase($dbName, $dbUser, $dbPassword, $dbHost); | ||
Installation::scaffold($wpRootDir); | ||
$db->create(); | ||
$this->config = [ | ||
'wpRootFolder' => $wpRootDir, | ||
'dbUrl' => $db->getDbUrl() | ||
]; | ||
$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.'); | ||
} | ||
|
||
$wpLoader = $this->module(); | ||
|
||
$this->assertInIsolation(static function () use ($wpLoader, $testcaseFile) { | ||
$wpLoader->_initialize(); | ||
|
||
require_once $testcaseFile; | ||
|
||
try { | ||
\BreakingTest::setUpBeforeClass(); | ||
} catch (\Throwable $e) { | ||
Assert::assertStringContainsString( | ||
'The database connection went away. A `setUpBeforeClassMethod` likely closed the connection', | ||
$e->getMessage() | ||
); | ||
return; | ||
} | ||
|
||
Assert::fail('The test should have failed.'); | ||
}); | ||
} | ||
} |