2012-05-09 14:26:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
define('TEST_SERVICES', dirname(__FILE__) . '/testservices');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the dependency injector
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-09 14:26:29 +02:00
|
|
|
* Note that these are SS conversions of the existing Simpletest unit tests
|
|
|
|
*
|
|
|
|
* @author marcus@silverstripe.com.au
|
|
|
|
* @license BSD License http://silverstripe.org/bsd-license/
|
|
|
|
*/
|
|
|
|
class InjectorTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
protected $nestingLevel = 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
$this->nestingLevel = 0;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
public function tearDown() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
while($this->nestingLevel > 0) {
|
|
|
|
$this->nestingLevel--;
|
|
|
|
Config::unnest();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 12:27:22 +02:00
|
|
|
public function testCorrectlyInitialised() {
|
|
|
|
$injector = Injector::inst();
|
|
|
|
$this->assertTrue($injector->getConfigLocator() instanceof SilverStripeServiceConfigurationLocator,
|
2012-09-26 23:34:00 +02:00
|
|
|
'Failure most likely because the injector has been referenced BEFORE being initialised in Core.php');
|
2012-06-14 12:27:22 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
public function testBasicInjector() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
$config = array(array('src' => TEST_SERVICES . '/SampleService.php',));
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$this->assertTrue($injector->hasService('SampleService') == 'SampleService');
|
|
|
|
|
|
|
|
$myObject = new TestObject();
|
|
|
|
$injector->inject($myObject);
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($myObject->sampleService), 'SampleService');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testConfiguredInjector() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$services = array(
|
|
|
|
array(
|
|
|
|
'src' => TEST_SERVICES . '/AnotherService.php',
|
|
|
|
'properties' => array('config_property' => 'Value'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'src' => TEST_SERVICES . '/SampleService.php',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($services);
|
|
|
|
$this->assertTrue($injector->hasService('SampleService') == 'SampleService');
|
|
|
|
// We expect a false because the 'AnotherService' is actually
|
|
|
|
// just a replacement of the SampleService
|
|
|
|
$this->assertTrue($injector->hasService('AnotherService') == 'AnotherService');
|
|
|
|
|
|
|
|
$item = $injector->get('AnotherService');
|
|
|
|
|
|
|
|
$this->assertEquals('Value', $item->config_property);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIdToNameMap() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$services = array(
|
|
|
|
'FirstId' => 'AnotherService',
|
|
|
|
'SecondId' => 'SampleService',
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($services);
|
|
|
|
|
|
|
|
$this->assertTrue($injector->hasService('FirstId') == 'FirstId');
|
|
|
|
$this->assertTrue($injector->hasService('SecondId') == 'SecondId');
|
|
|
|
|
|
|
|
$this->assertTrue($injector->get('FirstId') instanceof AnotherService);
|
|
|
|
$this->assertTrue($injector->get('SecondId') instanceof SampleService);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReplaceService() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
|
|
|
|
$config = array(array('src' => TEST_SERVICES . '/SampleService.php'));
|
|
|
|
|
|
|
|
// load
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
// inject
|
|
|
|
$myObject = new TestObject();
|
|
|
|
$injector->inject($myObject);
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($myObject->sampleService), 'SampleService');
|
|
|
|
|
|
|
|
// also tests that ID can be the key in the array
|
2014-08-15 08:53:05 +02:00
|
|
|
$config = array('SampleService' => array('src' => TEST_SERVICES . '/AnotherService.php'));
|
2012-09-26 23:34:00 +02:00
|
|
|
// , 'id' => 'SampleService'));
|
2012-05-09 14:26:29 +02:00
|
|
|
// load
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$injector->inject($myObject);
|
|
|
|
$this->assertEquals('AnotherService', get_class($myObject->sampleService));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
public function testUpdateSpec() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$services = array(
|
|
|
|
'AnotherService' => array(
|
|
|
|
'src' => TEST_SERVICES . '/AnotherService.php',
|
|
|
|
'properties' => array(
|
|
|
|
'filters' => array(
|
|
|
|
'One',
|
|
|
|
'Two',
|
|
|
|
)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($services);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$injector->updateSpec('AnotherService', 'filters', 'Three');
|
|
|
|
$another = $injector->get('AnotherService');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->assertEquals(3, count($another->filters));
|
|
|
|
$this->assertEquals('Three', $another->filters[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAutoSetInjector() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
$injector->addAutoProperty('auto', 'somevalue');
|
|
|
|
$config = array(array('src' => TEST_SERVICES . '/SampleService.php',));
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$this->assertTrue($injector->hasService('SampleService') == 'SampleService');
|
|
|
|
// We expect a false because the 'AnotherService' is actually
|
|
|
|
// just a replacement of the SampleService
|
|
|
|
|
|
|
|
$myObject = new TestObject();
|
|
|
|
|
|
|
|
$injector->inject($myObject);
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($myObject->sampleService), 'SampleService');
|
|
|
|
$this->assertEquals($myObject->auto, 'somevalue');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSettingSpecificProperty() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array('AnotherService');
|
|
|
|
$injector->load($config);
|
|
|
|
$injector->setInjectMapping('TestObject', 'sampleService', 'AnotherService');
|
|
|
|
$testObject = $injector->get('TestObject');
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($testObject->sampleService), 'AnotherService');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSettingSpecificMethod() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array('AnotherService');
|
|
|
|
$injector->load($config);
|
|
|
|
$injector->setInjectMapping('TestObject', 'setSomething', 'AnotherService', 'method');
|
|
|
|
|
|
|
|
$testObject = $injector->get('TestObject');
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($testObject->sampleService), 'AnotherService');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
public function testInjectingScopedService() {
|
|
|
|
$injector = new Injector();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$config = array(
|
|
|
|
'AnotherService',
|
|
|
|
'AnotherService.DottedChild' => 'SampleService',
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$injector->load($config);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$service = $injector->get('AnotherService.DottedChild');
|
|
|
|
$this->assertEquals(get_class($service), 'SampleService');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$service = $injector->get('AnotherService.Subset');
|
|
|
|
$this->assertEquals(get_class($service), 'AnotherService');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$injector->setInjectMapping('TestObject', 'sampleService', 'AnotherService.Geronimo');
|
|
|
|
$testObject = $injector->create('TestObject');
|
|
|
|
$this->assertEquals(get_class($testObject->sampleService), 'AnotherService');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$injector->setInjectMapping('TestObject', 'sampleService', 'AnotherService.DottedChild.AnotherDown');
|
|
|
|
$testObject = $injector->create('TestObject');
|
|
|
|
$this->assertEquals(get_class($testObject->sampleService), 'SampleService');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInjectUsingConstructor() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(array(
|
|
|
|
'src' => TEST_SERVICES . '/SampleService.php',
|
|
|
|
'constructor' => array(
|
|
|
|
'val1',
|
|
|
|
'val2',
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$sample = $injector->get('SampleService');
|
|
|
|
$this->assertEquals($sample->constructorVarOne, 'val1');
|
|
|
|
$this->assertEquals($sample->constructorVarTwo, 'val2');
|
|
|
|
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(
|
|
|
|
'AnotherService',
|
|
|
|
array(
|
|
|
|
'src' => TEST_SERVICES . '/SampleService.php',
|
|
|
|
'constructor' => array(
|
|
|
|
'val1',
|
|
|
|
'%$AnotherService',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$sample = $injector->get('SampleService');
|
|
|
|
$this->assertEquals($sample->constructorVarOne, 'val1');
|
|
|
|
$this->assertEquals(get_class($sample->constructorVarTwo), 'AnotherService');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(array(
|
|
|
|
'src' => TEST_SERVICES . '/SampleService.php',
|
|
|
|
'constructor' => array(
|
|
|
|
'val1',
|
|
|
|
'val2',
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$sample = $injector->get('SampleService');
|
|
|
|
$this->assertEquals($sample->constructorVarOne, 'val1');
|
|
|
|
$this->assertEquals($sample->constructorVarTwo, 'val2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
// test constructors on prototype
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(array(
|
|
|
|
'type' => 'prototype',
|
|
|
|
'src' => TEST_SERVICES . '/SampleService.php',
|
|
|
|
'constructor' => array(
|
|
|
|
'val1',
|
|
|
|
'val2',
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$sample = $injector->get('SampleService');
|
|
|
|
$this->assertEquals($sample->constructorVarOne, 'val1');
|
|
|
|
$this->assertEquals($sample->constructorVarTwo, 'val2');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
$again = $injector->get('SampleService');
|
|
|
|
$this->assertFalse($sample === $again);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
$this->assertEquals($sample->constructorVarOne, 'val1');
|
|
|
|
$this->assertEquals($sample->constructorVarTwo, 'val2');
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testInjectUsingSetter() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
$config = array(array('src' => TEST_SERVICES . '/SampleService.php',));
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$this->assertTrue($injector->hasService('SampleService') == 'SampleService');
|
|
|
|
|
|
|
|
$myObject = new OtherTestObject();
|
|
|
|
$injector->inject($myObject);
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($myObject->s()), 'SampleService');
|
|
|
|
|
|
|
|
// and again because it goes down a different code path when setting things
|
|
|
|
// based on the inject map
|
|
|
|
$myObject = new OtherTestObject();
|
|
|
|
$injector->inject($myObject);
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($myObject->s()), 'SampleService');
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure we can just get any arbitrary object - it should be created for us
|
|
|
|
public function testInstantiateAnObjectViaGet() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
$config = array(array('src' => TEST_SERVICES . '/SampleService.php',));
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$this->assertTrue($injector->hasService('SampleService') == 'SampleService');
|
|
|
|
|
|
|
|
$myObject = $injector->get('OtherTestObject');
|
|
|
|
$this->assertEquals(get_class($myObject->s()), 'SampleService');
|
|
|
|
|
|
|
|
// and again because it goes down a different code path when setting things
|
|
|
|
// based on the inject map
|
|
|
|
$myObject = $injector->get('OtherTestObject');
|
|
|
|
$this->assertEquals(get_class($myObject->s()), 'SampleService');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCircularReference() {
|
|
|
|
$services = array('CircularOne', 'CircularTwo');
|
|
|
|
$injector = new Injector($services);
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
|
|
|
|
$obj = $injector->get('NeedsBothCirculars');
|
|
|
|
|
|
|
|
$this->assertTrue($obj->circularOne instanceof CircularOne);
|
|
|
|
$this->assertTrue($obj->circularTwo instanceof CircularTwo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrototypeObjects() {
|
|
|
|
$services = array('CircularOne', 'CircularTwo', array('class' => 'NeedsBothCirculars', 'type' => 'prototype'));
|
|
|
|
$injector = new Injector($services);
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
$obj1 = $injector->get('NeedsBothCirculars');
|
|
|
|
$obj2 = $injector->get('NeedsBothCirculars');
|
|
|
|
|
|
|
|
// if this was the same object, then $obj1->var would now be two
|
|
|
|
$obj1->var = 'one';
|
|
|
|
$obj2->var = 'two';
|
|
|
|
|
|
|
|
$this->assertTrue($obj1->circularOne instanceof CircularOne);
|
|
|
|
$this->assertTrue($obj1->circularTwo instanceof CircularTwo);
|
|
|
|
|
|
|
|
$this->assertEquals($obj1->circularOne, $obj2->circularOne);
|
|
|
|
$this->assertNotEquals($obj1, $obj2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimpleInstantiation() {
|
|
|
|
$services = array('CircularOne', 'CircularTwo');
|
|
|
|
$injector = new Injector($services);
|
|
|
|
|
|
|
|
// similar to the above, but explicitly instantiating this object here
|
|
|
|
$obj1 = $injector->create('NeedsBothCirculars');
|
|
|
|
$obj2 = $injector->create('NeedsBothCirculars');
|
|
|
|
|
|
|
|
// if this was the same object, then $obj1->var would now be two
|
|
|
|
$obj1->var = 'one';
|
|
|
|
$obj2->var = 'two';
|
|
|
|
|
|
|
|
$this->assertEquals($obj1->circularOne, $obj2->circularOne);
|
|
|
|
$this->assertNotEquals($obj1, $obj2);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
public function testCreateWithConstructor() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$obj = $injector->create('CircularTwo', 'param');
|
|
|
|
$this->assertEquals($obj->otherVar, 'param');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
public function testSimpleSingleton() {
|
|
|
|
$injector = new Injector();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$one = $injector->create('CircularOne');
|
|
|
|
$two = $injector->create('CircularOne');
|
|
|
|
|
|
|
|
$this->assertFalse($one === $two);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$one = $injector->get('CircularTwo');
|
|
|
|
$two = $injector->get('CircularTwo');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$this->assertTrue($one === $two);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOverridePriority() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$injector->setAutoScanProperties(true);
|
|
|
|
$config = array(
|
|
|
|
array(
|
|
|
|
'src' => TEST_SERVICES . '/SampleService.php',
|
|
|
|
'priority' => 10,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// load
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
// inject
|
|
|
|
$myObject = new TestObject();
|
|
|
|
$injector->inject($myObject);
|
|
|
|
|
|
|
|
$this->assertEquals(get_class($myObject->sampleService), 'SampleService');
|
|
|
|
|
|
|
|
$config = array(
|
|
|
|
array(
|
|
|
|
'src' => TEST_SERVICES . '/AnotherService.php',
|
|
|
|
'id' => 'SampleService',
|
|
|
|
'priority' => 1,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
// load
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$injector->inject($myObject);
|
|
|
|
$this->assertEquals('SampleService', get_class($myObject->sampleService));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specific test method to illustrate various ways of setting a requirements backend
|
|
|
|
*/
|
|
|
|
public function testRequirementsSettingOptions() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(
|
|
|
|
'OriginalRequirementsBackend',
|
|
|
|
'NewRequirementsBackend',
|
|
|
|
'DummyRequirements' => array(
|
|
|
|
'constructor' => array(
|
|
|
|
'%$OriginalRequirementsBackend'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$requirements = $injector->get('DummyRequirements');
|
|
|
|
$this->assertEquals('OriginalRequirementsBackend', get_class($requirements->backend));
|
|
|
|
|
|
|
|
// just overriding the definition here
|
|
|
|
$injector->load(array(
|
|
|
|
'DummyRequirements' => array(
|
|
|
|
'constructor' => array(
|
|
|
|
'%$NewRequirementsBackend'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
// requirements should have been reinstantiated with the new bean setting
|
|
|
|
$requirements = $injector->get('DummyRequirements');
|
|
|
|
$this->assertEquals('NewRequirementsBackend', get_class($requirements->backend));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disabled for now
|
|
|
|
*/
|
|
|
|
public function testStaticInjections() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(
|
|
|
|
'NewRequirementsBackend',
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$si = $injector->get('TestStaticInjections');
|
|
|
|
$this->assertEquals('NewRequirementsBackend', get_class($si->backend));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
public function testSetterInjections() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(
|
|
|
|
'NewRequirementsBackend',
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$si = $injector->get('TestSetterInjections');
|
|
|
|
$this->assertEquals('NewRequirementsBackend', get_class($si->getBackend()));
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public function testCustomObjectCreator() {
|
|
|
|
$injector = new Injector();
|
2012-12-07 03:16:33 +01:00
|
|
|
$injector->setObjectCreator(new SSObjectCreator($injector));
|
2012-05-09 14:26:29 +02:00
|
|
|
$config = array(
|
|
|
|
'OriginalRequirementsBackend',
|
|
|
|
'DummyRequirements' => array(
|
|
|
|
'class' => 'DummyRequirements(\'%$OriginalRequirementsBackend\')'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$injector->load($config);
|
|
|
|
|
|
|
|
$requirements = $injector->get('DummyRequirements');
|
|
|
|
$this->assertEquals('OriginalRequirementsBackend', get_class($requirements->backend));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInheritedConfig() {
|
|
|
|
$injector = new Injector(array('locator' => 'SilverStripeServiceConfigurationLocator'));
|
|
|
|
Config::inst()->update('Injector', 'MyParentClass', array('properties' => array('one' => 'the one')));
|
|
|
|
$obj = $injector->get('MyParentClass');
|
|
|
|
$this->assertEquals($obj->one, 'the one');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
$obj = $injector->get('MyChildClass');
|
|
|
|
$this->assertEquals($obj->one, 'the one');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
public function testSameNamedSingeltonPrototype() {
|
|
|
|
$injector = new Injector();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
// get a singleton object
|
|
|
|
$object = $injector->get('NeedsBothCirculars');
|
|
|
|
$object->var = 'One';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
$again = $injector->get('NeedsBothCirculars');
|
|
|
|
$this->assertEquals($again->var, 'One');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
// create a NEW instance object
|
|
|
|
$new = $injector->create('NeedsBothCirculars');
|
|
|
|
$this->assertNull($new->var);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
// this will trigger a problem below
|
|
|
|
$new->var = 'Two';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-14 09:11:36 +02:00
|
|
|
$again = $injector->get('NeedsBothCirculars');
|
|
|
|
$this->assertEquals($again->var, 'One');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
public function testConvertServicePropertyOnCreate() {
|
2014-08-15 08:53:05 +02:00
|
|
|
// make sure convert service property is not called on direct calls to create, only on configured
|
2012-12-07 03:16:33 +01:00
|
|
|
// declarations to avoid un-needed function calls
|
|
|
|
$injector = new Injector();
|
|
|
|
$item = $injector->create('ConstructableObject', '%$TestObject');
|
|
|
|
$this->assertEquals('%$TestObject', $item->property);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
// do it again but have test object configured as a constructor dependency
|
|
|
|
$injector = new Injector();
|
|
|
|
$config = array(
|
|
|
|
'ConstructableObject' => array(
|
|
|
|
'constructor' => array(
|
|
|
|
'%$TestObject'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$item = $injector->get('ConstructableObject');
|
|
|
|
$this->assertTrue($item->property instanceof TestObject);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
// and with a configured object defining TestObject to be something else!
|
|
|
|
$injector = new Injector(array('locator' => 'InjectorTestConfigLocator'));
|
|
|
|
$config = array(
|
|
|
|
'ConstructableObject' => array(
|
|
|
|
'constructor' => array(
|
|
|
|
'%$TestObject'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$injector->load($config);
|
|
|
|
$item = $injector->get('ConstructableObject');
|
|
|
|
$this->assertTrue($item->property instanceof ConstructableObject);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
$this->assertInstanceOf('OtherTestObject', $item->property->property);
|
|
|
|
}
|
2013-02-13 13:06:15 +01:00
|
|
|
|
|
|
|
public function testNamedServices() {
|
|
|
|
$injector = new Injector();
|
|
|
|
$service = new stdClass();
|
|
|
|
|
|
|
|
$injector->registerNamedService('NamedService', $service);
|
|
|
|
$this->assertEquals($service, $injector->get('NamedService'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-15 00:24:47 +01:00
|
|
|
public function testCreateConfiggedObjectWithCustomConstructorArgs() {
|
2014-08-15 08:53:05 +02:00
|
|
|
// need to make sure that even if the config defines some constructor params,
|
2013-02-15 00:24:47 +01:00
|
|
|
// that we take our passed in constructor args instead
|
|
|
|
$injector = new Injector(array('locator' => 'InjectorTestConfigLocator'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-15 00:24:47 +01:00
|
|
|
$item = $injector->create('ConfigConstructor', 'othervalue');
|
|
|
|
$this->assertEquals($item->property, 'othervalue');
|
|
|
|
}
|
2013-02-13 13:06:15 +01:00
|
|
|
|
2014-02-03 01:30:22 +01:00
|
|
|
/**
|
|
|
|
* Tests creating a service with a custom factory.
|
|
|
|
*/
|
|
|
|
public function testCustomFactory() {
|
|
|
|
$injector = new Injector(array(
|
|
|
|
'service' => array('factory' => 'factory', 'constructor' => array(1, 2, 3))
|
|
|
|
));
|
|
|
|
|
|
|
|
$factory = $this->getMock('SilverStripe\\Framework\\Injector\\Factory');
|
|
|
|
$factory
|
|
|
|
->expects($this->once())
|
|
|
|
->method('create')
|
|
|
|
->with($this->equalTo('service'), $this->equalTo(array(1, 2, 3)))
|
|
|
|
->will($this->returnCallback(function($args) {
|
|
|
|
return new TestObject();
|
|
|
|
}));
|
|
|
|
|
|
|
|
$injector->registerService($factory, 'factory');
|
|
|
|
|
|
|
|
$this->assertInstanceOf('TestObject', $injector->get('service'));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
/**
|
|
|
|
* Test nesting of injector
|
|
|
|
*/
|
|
|
|
public function testNest() {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
// Outer nest to avoid interference with other
|
2014-04-22 05:21:12 +02:00
|
|
|
Injector::nest();
|
|
|
|
$this->nestingLevel++;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
// Test services
|
|
|
|
$config = array(
|
|
|
|
'NewRequirementsBackend',
|
|
|
|
);
|
|
|
|
Injector::inst()->load($config);
|
|
|
|
$si = Injector::inst()->get('TestStaticInjections');
|
|
|
|
$this->assertInstanceOf('TestStaticInjections', $si);
|
|
|
|
$this->assertInstanceOf('NewRequirementsBackend', $si->backend);
|
|
|
|
$this->assertInstanceOf('MyParentClass', Injector::inst()->get('MyParentClass'));
|
|
|
|
$this->assertInstanceOf('MyChildClass', Injector::inst()->get('MyChildClass'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
// Test that nested injector values can be overridden
|
|
|
|
Injector::nest();
|
|
|
|
$this->nestingLevel++;
|
|
|
|
Injector::inst()->unregisterAllObjects();
|
|
|
|
$newsi = Injector::inst()->get('TestStaticInjections');
|
|
|
|
$newsi->backend = new OriginalRequirementsBackend();
|
|
|
|
Injector::inst()->registerService($newsi, 'TestStaticInjections');
|
|
|
|
Injector::inst()->registerService(new MyChildClass(), 'MyParentClass');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
// Check that these overridden values are retrievable
|
|
|
|
$si = Injector::inst()->get('TestStaticInjections');
|
|
|
|
$this->assertInstanceOf('TestStaticInjections', $si);
|
|
|
|
$this->assertInstanceOf('OriginalRequirementsBackend', $si->backend);
|
|
|
|
$this->assertInstanceOf('MyParentClass', Injector::inst()->get('MyParentClass'));
|
|
|
|
$this->assertInstanceOf('MyParentClass', Injector::inst()->get('MyChildClass'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
// Test that unnesting restores expected behaviour
|
|
|
|
Injector::unnest();
|
|
|
|
$this->nestingLevel--;
|
|
|
|
$si = Injector::inst()->get('TestStaticInjections');
|
|
|
|
$this->assertInstanceOf('TestStaticInjections', $si);
|
|
|
|
$this->assertInstanceOf('NewRequirementsBackend', $si->backend);
|
|
|
|
$this->assertInstanceOf('MyParentClass', Injector::inst()->get('MyParentClass'));
|
|
|
|
$this->assertInstanceOf('MyChildClass', Injector::inst()->get('MyChildClass'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
// Test reset of cache
|
|
|
|
Injector::inst()->unregisterAllObjects();
|
|
|
|
$si = Injector::inst()->get('TestStaticInjections');
|
|
|
|
$this->assertInstanceOf('TestStaticInjections', $si);
|
|
|
|
$this->assertInstanceOf('NewRequirementsBackend', $si->backend);
|
|
|
|
$this->assertInstanceOf('MyParentClass', Injector::inst()->get('MyParentClass'));
|
|
|
|
$this->assertInstanceOf('MyChildClass', Injector::inst()->get('MyChildClass'));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-22 05:21:12 +02:00
|
|
|
// Return to nestingLevel 0
|
|
|
|
Injector::unnest();
|
|
|
|
$this->nestingLevel--;
|
|
|
|
}
|
2014-02-03 01:30:22 +01:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator implements TestOnly {
|
|
|
|
public function locateConfigFor($name) {
|
|
|
|
if ($name == 'TestObject') {
|
|
|
|
return array('class' => 'ConstructableObject', 'constructor' => array('%$OtherTestObject'));
|
|
|
|
}
|
|
|
|
|
2013-02-15 00:24:47 +01:00
|
|
|
if ($name == 'ConfigConstructor') {
|
|
|
|
return array('class' => 'ConstructableObject', 'constructor' => array('value'));
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
return parent::locateConfigFor($name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ConstructableObject implements TestOnly {
|
|
|
|
public $property;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
public function __construct($prop) {
|
|
|
|
$this->property = $prop;
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class TestObject implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public $sampleService;
|
|
|
|
|
|
|
|
public function setSomething($v) {
|
|
|
|
$this->sampleService = $v;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class OtherTestObject implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
private $sampleService;
|
|
|
|
|
|
|
|
public function setSampleService($s) {
|
|
|
|
$this->sampleService = $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function s() {
|
|
|
|
return $this->sampleService;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class CircularOne implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public $circularTwo;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class CircularTwo implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public $circularOne;
|
|
|
|
|
|
|
|
public $otherVar;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
public function __construct($value = null) {
|
|
|
|
$this->otherVar = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class NeedsBothCirculars implements TestOnly{
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public $circularOne;
|
|
|
|
public $circularTwo;
|
|
|
|
public $var;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class MyParentClass implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
public $one;
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class MyChildClass extends MyParentClass implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class DummyRequirements implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public $backend;
|
|
|
|
|
|
|
|
public function __construct($backend) {
|
|
|
|
$this->backend = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBackend($backend) {
|
|
|
|
$this->backend = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class OriginalRequirementsBackend implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class NewRequirementsBackend implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
class TestStaticInjections implements TestOnly {
|
2012-05-09 14:26:29 +02:00
|
|
|
|
|
|
|
public $backend;
|
2013-03-21 19:48:54 +01:00
|
|
|
/** @config */
|
|
|
|
private static $dependencies = array(
|
2012-05-09 14:26:29 +02:00
|
|
|
'backend' => '%$NewRequirementsBackend'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
/**
|
|
|
|
* Make sure DI works with ViewableData's implementation of __isset
|
|
|
|
*/
|
|
|
|
class TestSetterInjections extends ViewableData implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
protected $backend;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
/** @config */
|
|
|
|
private static $dependencies = array(
|
|
|
|
'backend' => '%$NewRequirementsBackend'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
public function getBackend() {
|
|
|
|
return $this->backend;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
public function setBackend($backend) {
|
|
|
|
$this->backend = $backend;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-06-12 00:07:04 +02:00
|
|
|
}
|
|
|
|
|
2012-05-09 14:26:29 +02:00
|
|
|
/**
|
|
|
|
* An example object creator that uses the SilverStripe class(arguments) mechanism for
|
|
|
|
* creating new objects
|
|
|
|
*
|
|
|
|
* @see https://github.com/silverstripe/sapphire
|
|
|
|
*/
|
|
|
|
class SSObjectCreator extends InjectionCreator {
|
2012-12-07 03:16:33 +01:00
|
|
|
private $injector;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-07 03:16:33 +01:00
|
|
|
public function __construct($injector) {
|
|
|
|
$this->injector = $injector;
|
|
|
|
}
|
2012-05-09 14:26:29 +02:00
|
|
|
|
2014-02-03 01:30:22 +01:00
|
|
|
public function create($class, array $params = array()) {
|
2012-05-09 14:26:29 +02:00
|
|
|
if (strpos($class, '(') === false) {
|
2012-12-07 03:16:33 +01:00
|
|
|
return parent::create($class, $params);
|
2012-05-09 14:26:29 +02:00
|
|
|
} else {
|
2013-05-05 01:43:56 +02:00
|
|
|
list($class, $params) = Object::parse_class_spec($class);
|
2012-12-07 03:16:33 +01:00
|
|
|
$params = $this->injector->convertServiceProperty($params);
|
|
|
|
return parent::create($class, $params);
|
2012-05-09 14:26:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|