Merge pull request #7850 from jonom/singleton-fix-3x

FIX Singleton creation
This commit is contained in:
Daniel Hensby 2018-02-13 08:29:47 +00:00 committed by GitHub
commit 1bec8a5d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 0 deletions

View File

@ -551,6 +551,11 @@ class Injector {
$constructorParams = $spec['constructor'];
}
// If we're dealing with a DataObject, pass through Singleton flag as second argument
if ($type != 'prototype' && empty($constructorParams) && is_subclass_of($class, 'DataObject')) {
$constructorParams = array(null, true);
}
$factory = isset($spec['factory']) ? $this->get($spec['factory']) : $this->getObjectCreator();
$object = $factory->create($class, $constructorParams);

View File

@ -33,6 +33,51 @@ class DataObjectTest extends SapphireTest {
'ManyManyListTest_Category',
);
/**
* @dataProvider provideSingletons
*/
public function testSingleton($inst, $defaultValue, $altDefaultValue)
{
$inst = $inst();
// Test that populateDefaults() isn't called on singletons
// which can lead to SQL errors during build, and endless loops
if ($defaultValue) {
$this->assertEquals($defaultValue, $inst->MyFieldWithDefault);
} else {
$this->assertEmpty($inst->MyFieldWithDefault);
}
if ($altDefaultValue) {
$this->assertEquals($altDefaultValue, $inst->MyFieldWithAltDefault);
} else {
$this->assertEmpty($inst->MyFieldWithAltDefault);
}
}
public function provideSingletons()
{
// because PHPUnit evalutes test providers *before* setUp methods
// any extensions added in the setUp methods won't be available
// we must return closures to generate the arguments at run time
return array(
array(function () {
return DataObjectTest_Fixture::create();
}, 'Default Value', 'Default Value'),
array(function () {
return new DataObjectTest_Fixture();
}, 'Default Value', 'Default Value'),
array(function () {
return singleton('DataObjectTest_Fixture');
}, null, null),
array(function () {
return DataObjectTest_Fixture::singleton();
}, null, null),
array(function () {
return new DataObjectTest_Fixture(null, true);
}, null, null),
);
}
public function testDb() {
$obj = new DataObjectTest_TeamComment();
$dbFields = $obj->db();