Merge pull request #9686 from sminnee/pulls/9679-guard-sapphiretest

This commit is contained in:
Guy Marriott 2020-09-14 17:22:30 -07:00 committed by GitHub
commit 05e3417249
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -173,6 +173,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
*/ */
public static function tempDB() public static function tempDB()
{ {
if (!class_exists(TempDatabase::class)) {
return null;
}
if (!static::$tempDB) { if (!static::$tempDB) {
static::$tempDB = TempDatabase::create(); static::$tempDB = TempDatabase::create();
} }
@ -280,18 +284,28 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
} }
// i18n needs to be set to the defaults or tests fail // i18n needs to be set to the defaults or tests fail
i18n::set_locale(i18n::config()->uninherited('default_locale')); if (class_exists(i18n::class)) {
i18n::set_locale(i18n::config()->uninherited('default_locale'));
}
// Set default timezone consistently to avoid NZ-specific dependencies // Set default timezone consistently to avoid NZ-specific dependencies
date_default_timezone_set('UTC'); date_default_timezone_set('UTC');
Member::set_password_validator(null); if (class_exists(Member::class)) {
Cookie::config()->update('report_errors', false); Member::set_password_validator(null);
}
if (class_exists(Cookie::class)) {
Cookie::config()->update('report_errors', false);
}
if (class_exists(RootURLController::class)) { if (class_exists(RootURLController::class)) {
RootURLController::reset(); RootURLController::reset();
} }
Security::clear_database_is_ready(); if (class_exists(Security::class)) {
Security::clear_database_is_ready();
}
// Set up test routes // Set up test routes
$this->setUpRoutes(); $this->setUpRoutes();
@ -308,14 +322,21 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
} }
// turn off template debugging // turn off template debugging
SSViewer::config()->update('source_file_comments', false); if (class_exists(SSViewer::class)) {
SSViewer::config()->update('source_file_comments', false);
}
// Set up the test mailer // Set up the test mailer
Injector::inst()->registerService(new TestMailer(), Mailer::class); if (class_exists(TestMailer::class)) {
Email::config()->remove('send_all_emails_to'); Injector::inst()->registerService(new TestMailer(), Mailer::class);
Email::config()->remove('send_all_emails_from'); }
Email::config()->remove('cc_all_emails_to');
Email::config()->remove('bcc_all_emails_to'); if (class_exists(Email::class)) {
Email::config()->remove('send_all_emails_to');
Email::config()->remove('send_all_emails_from');
Email::config()->remove('cc_all_emails_to');
Email::config()->remove('bcc_all_emails_to');
}
} }
@ -392,7 +413,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
static::$state->setUpOnce(static::class); static::$state->setUpOnce(static::class);
// Build DB if we have objects // Build DB if we have objects
if (static::getExtraDataObjects()) { if (class_exists(DataObject::class) && static::getExtraDataObjects()) {
DataObject::reset(); DataObject::reset();
static::resetDBSchema(true, true); static::resetDBSchema(true, true);
} }
@ -574,15 +595,19 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
protected function tearDown() protected function tearDown()
{ {
// Reset mocked datetime // Reset mocked datetime
DBDatetime::clear_mock_now(); if (class_exists(DBDatetime::class)) {
DBDatetime::clear_mock_now();
}
// Stop the redirection that might have been requested in the test. // Stop the redirection that might have been requested in the test.
// Note: Ideally a clean Controller should be created for each test. // Note: Ideally a clean Controller should be created for each test.
// Now all tests executed in a batch share the same controller. // Now all tests executed in a batch share the same controller.
$controller = Controller::has_curr() ? Controller::curr() : null; if (class_exists(Controller::class)) {
if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) { $controller = Controller::has_curr() ? Controller::curr() : null;
$response->setStatusCode(200); if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) {
$response->removeHeader('Location'); $response->setStatusCode(200);
$response->removeHeader('Location');
}
} }
// Call state helpers // Call state helpers
@ -986,30 +1011,42 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
} }
static::set_is_running_test(true); static::set_is_running_test(true);
// Mock request
$_SERVER['argv'] = ['vendor/bin/phpunit', '/'];
$request = CLIRequestBuilder::createFromEnvironment();
// Test application // Test application
$kernel = new TestKernel(BASE_PATH); $kernel = new TestKernel(BASE_PATH);
$app = new HTTPApplication($kernel);
$flush = array_key_exists('flush', $request->getVars());
// Custom application if (class_exists(HTTPApplication::class)) {
$app->execute($request, function (HTTPRequest $request) { // Mock request
// Start session and execute $_SERVER['argv'] = ['vendor/bin/phpunit', '/'];
$request->getSession()->init($request); $request = CLIRequestBuilder::createFromEnvironment();
// Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class $app = new HTTPApplication($kernel);
// (e.g. Member will now have various subclasses of DataObjects that implement TestOnly) $flush = array_key_exists('flush', $request->getVars());
DataObject::reset();
// Custom application
// Set dummy controller; $app->execute($request, function (HTTPRequest $request) {
$controller = Controller::create(); // Start session and execute
$controller->setRequest($request); $request->getSession()->init($request);
$controller->pushCurrent();
$controller->doInit(); // Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class
}, $flush); // (e.g. Member will now have various subclasses of DataObjects that implement TestOnly)
DataObject::reset();
// Set dummy controller;
$controller = Controller::create();
$controller->setRequest($request);
$controller->pushCurrent();
$controller->doInit();
}, $flush);
} else {
// Allow flush from the command line in the absence of HTTPApplication's special sauce
$flush = false;
foreach ($_SERVER['argv'] as $arg) {
if (preg_match('/^(--)?flush(=1)?$/', $arg)) {
$flush = true;
}
}
$kernel->boot($flush);
}
// Register state // Register state
static::$state = SapphireTestState::singleton(); static::$state = SapphireTestState::singleton();
@ -1024,6 +1061,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
*/ */
public static function resetDBSchema($includeExtraDataObjects = false, $forceCreate = false) public static function resetDBSchema($includeExtraDataObjects = false, $forceCreate = false)
{ {
if (!static::$tempDB) {
return;
}
// Check if DB is active before reset // Check if DB is active before reset
if (!static::$tempDB->isUsed()) { if (!static::$tempDB->isUsed()) {
if (!$forceCreate) { if (!$forceCreate) {
@ -1234,6 +1275,10 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
protected function setUpRoutes() protected function setUpRoutes()
{ {
if (!class_exists(Director::class)) {
return;
}
// Get overridden routes // Get overridden routes
$rules = $this->getExtraRoutes(); $rules = $this->getExtraRoutes();