mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #7850 from jonom/singleton-fix-3x
FIX Singleton creation
This commit is contained in:
commit
1bec8a5d0c
@ -551,6 +551,11 @@ class Injector {
|
|||||||
$constructorParams = $spec['constructor'];
|
$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();
|
$factory = isset($spec['factory']) ? $this->get($spec['factory']) : $this->getObjectCreator();
|
||||||
$object = $factory->create($class, $constructorParams);
|
$object = $factory->create($class, $constructorParams);
|
||||||
|
|
||||||
|
@ -33,6 +33,51 @@ class DataObjectTest extends SapphireTest {
|
|||||||
'ManyManyListTest_Category',
|
'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() {
|
public function testDb() {
|
||||||
$obj = new DataObjectTest_TeamComment();
|
$obj = new DataObjectTest_TeamComment();
|
||||||
$dbFields = $obj->db();
|
$dbFields = $obj->db();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user