From 746c0679ad1d6ceac03d2adf167367f0ca2259cd Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Wed, 23 Jan 2019 11:26:16 +0000 Subject: [PATCH] FIX: Injector may instantiate prototypes as if they're singletons (fixes #8567) --- control/injector/Injector.php | 2 +- tests/model/DataObjectTest.php | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/control/injector/Injector.php b/control/injector/Injector.php index 96c69854b..3ab429a29 100644 --- a/control/injector/Injector.php +++ b/control/injector/Injector.php @@ -866,7 +866,7 @@ class Injector { return $this->instantiate($spec, $name); } - return $this->instantiate($spec); + return $this->instantiate($spec, null, 'prototype'); } /** diff --git a/tests/model/DataObjectTest.php b/tests/model/DataObjectTest.php index 8855693eb..a9ed94cca 100644 --- a/tests/model/DataObjectTest.php +++ b/tests/model/DataObjectTest.php @@ -29,6 +29,7 @@ class DataObjectTest extends SapphireTest { 'DataObjectTest_Play', 'DataObjectTest_Ploy', 'DataObjectTest_Bogey', + 'DataObjectTest_Sortable', 'ManyManyListTest_Product', 'ManyManyListTest_Category', ); @@ -38,6 +39,13 @@ class DataObjectTest extends SapphireTest { */ public function testSingleton($inst, $defaultValue, $altDefaultValue) { + // Calls to scaffold the test database may have cached service specs for DataObjects + // with the incorrect 'type' set (singleton instead of prototype) + Injector::nest(); + $reflectionProp = new ReflectionProperty('Injector', 'specs'); + $reflectionProp->setAccessible(true); + $reflectionProp->setValue(Injector::inst(), array()); + $inst = $inst(); // Test that populateDefaults() isn't called on singletons // which can lead to SQL errors during build, and endless loops @@ -52,6 +60,8 @@ class DataObjectTest extends SapphireTest { } else { $this->assertEmpty($inst->MyFieldWithAltDefault); } + + Injector::unnest(); } public function provideSingletons()