forked from silverstripe/silverstripe-framework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Move extension management into test state
- Loading branch information
Damian Mooyman
committed
Jun 20, 2017
1 parent
c66d433
commit ca03395
Showing
19 changed files
with
185 additions
and
130 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Dev; | ||
|
||
use LogicException; | ||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
/** | ||
* Manages illegal and required extensions for sapphiretest | ||
*/ | ||
class ExtensionTestState implements TestState | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $extensionsToReapply = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $extensionsToRemove = []; | ||
|
||
/** | ||
* Called on setup | ||
* | ||
* @param SapphireTest $test | ||
*/ | ||
public function setUp(SapphireTest $test) | ||
{ | ||
} | ||
|
||
public function tearDown(SapphireTest $test) | ||
{ | ||
} | ||
|
||
public function setUpOnce($class) | ||
{ | ||
$isAltered = false; | ||
/** @var string|SapphireTest $class */ | ||
/** @var string|DataObject $dataClass */ | ||
// Remove any illegal extensions that are present | ||
foreach ($class::getIllegalExtensions() as $dataClass => $extensions) { | ||
if (!class_exists($dataClass)) { | ||
continue; | ||
} | ||
if ($extensions === '*') { | ||
$extensions = $dataClass::get_extensions(); | ||
} | ||
foreach ($extensions as $extension) { | ||
if (!class_exists($extension) || !$dataClass::has_extension($extension)) { | ||
continue; | ||
} | ||
if (!isset($this->extensionsToReapply[$dataClass])) { | ||
$this->extensionsToReapply[$dataClass] = array(); | ||
} | ||
$this->extensionsToReapply[$dataClass][] = $extension; | ||
$dataClass::remove_extension($extension); | ||
$isAltered = true; | ||
} | ||
} | ||
|
||
// Add any required extensions that aren't present | ||
foreach ($class::getRequiredExtensions() as $dataClass => $extensions) { | ||
if (!class_exists($dataClass)) { | ||
throw new LogicException("Test {$class} requires dataClass {$dataClass} which doesn't exist"); | ||
} | ||
$this->extensionsToRemove[$dataClass] = array(); | ||
foreach ($extensions as $extension) { | ||
$dataClass = Extension::get_classname_without_arguments($extension); | ||
if (!class_exists($dataClass)) { | ||
$self = static::class; | ||
throw new LogicException("Test {$self} requires extension {$extension} which doesn't exist"); | ||
} | ||
if (!$dataClass::has_extension($extension)) { | ||
if (!isset($this->extensionsToRemove[$dataClass])) { | ||
$this->extensionsToReapply[$dataClass] = array(); | ||
} | ||
$this->extensionsToRemove[$dataClass][] = $extension; | ||
$dataClass::add_extension($extension); | ||
$isAltered = true; | ||
} | ||
} | ||
} | ||
|
||
// If we have made changes to the extensions present, then migrate the database schema. | ||
if ($isAltered || $this->extensionsToReapply || $this->extensionsToRemove || $class::getExtraDataObjects()) { | ||
DataObject::reset(); | ||
if (!SapphireTest::using_temp_db()) { | ||
SapphireTest::create_temp_db(); | ||
} | ||
SapphireTest::resetDBSchema(true); | ||
} | ||
|
||
// clear singletons, they're caching old extension info | ||
// which is used in DatabaseAdmin->doBuild() | ||
Injector::inst()->unregisterObjects(DataObject::class); | ||
} | ||
|
||
public function tearDownOnce($class) | ||
{ | ||
// @todo: This isn't strictly necessary to restore extensions, but only to ensure that | ||
// Object::$extra_methods is properly flushed. This should be replaced with a simple | ||
// flush mechanism for each $class. | ||
/** @var string|DataObject $dataClass */ | ||
|
||
// Remove extensions added for testing | ||
foreach ($this->extensionsToRemove as $dataClass => $extensions) { | ||
foreach ($extensions as $extension) { | ||
$dataClass::remove_extension($extension); | ||
} | ||
} | ||
|
||
// Reapply ones removed | ||
foreach ($this->extensionsToReapply as $dataClass => $extensions) { | ||
foreach ($extensions as $extension) { | ||
$dataClass::add_extension($extension); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.