diff --git a/Dice.php b/Dice.php index b5e5a1a..a200943 100644 --- a/Dice.php +++ b/Dice.php @@ -1,11 +1,14 @@ | https:// r.je/dice.html * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * - * @version 2.0 */ + * @version 3.0 */ namespace Dice; class Dice { + const CONSTANT = 'Dice::CONSTANT'; + const GLOBAL = 'Dice::GLOBAL'; + const INSTANCE = 'Dice::INSTANCE'; /** * @var array $rules Rules which have been set using addRule() */ @@ -26,9 +29,22 @@ class Dice { * @param string $name The name of the class to add the rule for * @param array $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules. */ - public function addRule($name, array $rule) { - if (isset($rule['instanceOf']) && (!array_key_exists('inherit', $rule) || $rule['inherit'] === true )) $rule = array_replace_recursive($this->getRule($rule['instanceOf']), $rule); + public function addRule(string $name, array $rule) { + if (isset($rule['instanceOf']) && (!array_key_exists('inherit', $rule) || $rule['inherit'] === true )) { + $rule = array_replace_recursive($this->getRule($rule['instanceOf']), $rule); + } + //Allow substitutions rules to be defined with a leading a slash + if (isset($rule['substitutions'])) foreach($rule['substitutions'] as $key => $value) $rule[ltrim($key, '\\')] = $value; + $this->rules[ltrim(strtolower($name), '\\')] = array_replace_recursive($this->getRule($name), $rule); + } + + /** + * Add rules as array. Useful for JSON loading $dice->addRules(json_decode(file_get_contents('foo.json')); + * @param array Rules in a single array [name => $rule] format + */ + public function addRules(array $rules) { + foreach ($rules as $name => $rule) $this->addRule($name, $rule); } /** @@ -36,7 +52,7 @@ public function addRule($name, array $rule) { * @param string name The name of the class to get the rules for * @return array The rules for the specified class */ - public function getRule($name) { + public function getRule(string $name): array { $lcName = strtolower(ltrim($name, '\\')); if (isset($this->rules[$lcName])) return $this->rules[$lcName]; @@ -58,7 +74,7 @@ public function getRule($name) { * @param array $share Whether or not this class instance be shared, so that the same instance is passed around each time * @return object A fully constructed object based on the specified input arguments */ - public function create($name, array $args = [], array $share = []) { + public function create(string $name, array $args = [], array $share = []) { // Is there a shared instance set? Return it. Better here than a closure for this, calling a closure is slower. if (!empty($this->instances[$name])) return $this->instances[$name]; @@ -75,7 +91,7 @@ public function create($name, array $args = [], array $share = []) { * @param array $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules. * @return callable A closure */ - private function getClosure($name, array $rule) { + private function getClosure(string $name, array $rule) { // Reflect the class and constructor, this should only ever be done once per class and get cached $class = new \ReflectionClass(isset($rule['instanceOf']) ? $rule['instanceOf'] : $name); $constructor = $class->getConstructor(); @@ -114,8 +130,8 @@ private function getClosure($name, array $rule) { $object = $closure($args, $share); foreach ($rule['call'] as $call) { - // Generate the method arguments using getParams() and call the returned closure (in php7 will be ()() rather than __invoke) - $params = $this->getParams($class->getMethod($call[0]), ['shareInstances' => isset($rule['shareInstances']) ? $rule['shareInstances'] : [] ])->__invoke($this->expand(isset($call[1]) ? $call[1] : [])); + // Generate the method arguments using getParams() and call the returned closure + $params = $this->getParams($class->getMethod($call[0]), ['shareInstances' => isset($rule['shareInstances']) ? $rule['shareInstances'] : [] ])(($this->expand(isset($call[1]) ? $call[1] : []))); $return = $object->{$call[0]}(...$params); if (isset($call[2]) && is_callable($call[2])) call_user_func($call[2], $return); } @@ -124,24 +140,29 @@ private function getClosure($name, array $rule) { } /** - * Looks for 'instance' array keys in $param and when found returns an object based on the value see {@link https:// r.je/dice.html#example3-1} + * Looks for Dice::INSTANCE, Dice::GLOBAL or Dice::CONSTANT array keys in $param and when found returns an object based on the value see {@link https:// r.je/dice.html#example3-1} * @param mixed $param Either a string or an array, - * @param array $share Whether or not this class instance be shared, so that the same instance is passed around each time + * @param array $share Array of instances from 'shareInstances', required for calls to `create` * @param bool $createFromString * @return mixed */ - private function expand($param, array $share = [], $createFromString = false) { - if (is_array($param) && isset($param['instance'])) { - // Call or return the value sored under the key 'instance' - // For ['instance' => ['className', 'methodName'] construct the instance before calling it - $args = isset($param['params']) ? $this->expand($param['params']) : []; - if (is_array($param['instance'])) $param['instance'][0] = $this->expand($param['instance'][0], $share, true); - if (is_callable($param['instance'])) return call_user_func($param['instance'], ...$args); - else return $this->create($param['instance'], array_merge($args, $share)); + private function expand($param, array $share = [], bool $createFromString = false) { + if (is_array($param)) { + //if a rule specifies Dice::INSTANCE, look up the relevant instance + if (isset($param[self::INSTANCE])) { + //Check for 'params' which allows parameters to be sent to the instance when it's created + //Either as a callback method or to the constructor of the instance + $args = isset($param['params']) ? $this->expand($param['params']) : []; + + //Support Dice::INSTANCE by creating/fetching the specified instance + if (is_callable($param[self::INSTANCE])) return call_user_func($param[self::INSTANCE], ...$args); + else return $this->create($param[self::INSTANCE], array_merge($args, $share)); + } + else if (isset($param[self::GLOBAL])) return $GLOBALS[$param[self::GLOBAL]]; + else if (isset($param[self::CONSTANT])) return constant($param[self::CONSTANT]); + else foreach ($param as $name => $value) $param[$name] = $this->expand($value, $share); } - // Recursively search for 'instance' keys in $param - else if (is_array($param)) foreach ($param as $name => $value) $param[$name] = $this->expand($value, $share); - // 'instance' wasn't found, return the value unchanged + return is_string($param) && $createFromString ? $this->create($param) : $param; } @@ -182,7 +203,6 @@ private function getParams(\ReflectionMethod $method, array $rule) { $parameters[] = $sub ? $this->expand($rule['substitutions'][$class], $share, true) : $this->create($class, [], $share); } catch (\InvalidArgumentException $e) { - } // For variadic parameters, provide remaining $args else if ($param->isVariadic()) $parameters = array_merge($parameters, $args); diff --git a/Loader/Json.php b/Loader/Json.php deleted file mode 100644 index 32f6103..0000000 --- a/Loader/Json.php +++ /dev/null @@ -1,33 +0,0 @@ - | http://r.je/dice.html * - * @license http://www.opensource.org/licenses/bsd-license.php BSD License * - * @version 1.3.2 */ -namespace Dice\Loader; -class Json { - public function load($json, \Dice\Dice $dice = null) { - if ($dice === null) $dice = new \Dice\Dice; - if (trim($json)[0] != '{') { - $path = dirname(realpath($json)); - $json = str_replace('__DIR__', $path, file_get_contents($json)); - } - - $map = json_decode($json, true); - - if (!is_array($map)) throw new \Exception('Could not decode json: ' . json_last_error_msg()); - - if (isset($map['rules'])) { - foreach ($map['rules'] as $rule) { - $name = $rule['name']; - unset($rule['name']); - $dice->addRule($name, $rule); - } - } - else { - foreach ($map as $name => $rule) $dice->addRule($name, $rule); - } - - return $dice; - } -} diff --git a/Loader/Xml.php b/Loader/Xml.php index 10f1ab9..3e573ca 100644 --- a/Loader/Xml.php +++ b/Loader/Xml.php @@ -1,16 +1,14 @@ - -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ + | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ namespace Dice\Loader; class Xml { private function getComponent(\SimpleXmlElement $element, $forceInstance = false) { - if ($forceInstance) return ['instance' => (string) $element]; - else if ($element->instance) return ['instance' => (string) $element->instance]; + if ($forceInstance) return [\Dice\Dice::INSTANCE => (string) $element]; + else if ($element->instance) return [\Dice\Dice::INSTANCE => (string) $element->instance]; else return (string) $element; } diff --git a/README.md b/README.md index b419388..283f37e 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Simple example: b = $b; } @@ -63,7 +63,7 @@ Dice is compatible with PHP5.4 and up, there are archived versions of Dice which Performance ----------- -Dice uses reflection which is often wrongly labelled "slow". Reflection is considerably faster than loading and parsing a configuration file. There are a set of benchmarks [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test1-5_results.html) and [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test6_results.html) (To download the benchmark tool yourself see [this repository](https://github.com/TomBZombie/php-dependency-injection-benchmarks)) and Dice is faster than the others in most cases. +Dice uses reflection which is often wrongly labelled "slow". Reflection is considerably faster than loading and parsing a configuration file. There are a set of benchmarks [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test1-5_results.html) and [here](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test6_results.html) (To download the benchmark tool yourself see [this repository](https://github.com/TomBZombie/php-dependency-injection-benchmarks)) and Dice is faster than the others in most cases. In the real world test ([test 6](https://rawgit.com/TomBZombie/php-dependency-injection-benchmarks/master/test6_results.html)) Dice is neck-and-neck with Pimple (which requires writing an awful lot of configuration code) and although Symfony\DependencyInjection is faster at creating objects, it has a larger overhead and you need to create over 500 objects on each page load until it becomes faster than Dice. The same is true of Phalcon, the overhead of loading the Phalcon extension means that unless you're creating well over a thousand objects per HTTP request, the overhead is not worthwhile. @@ -77,6 +77,91 @@ Originally developed by Tom Butler (@TomBZombie), with many thanks to daniel-mei Updates ------------ +06/03/2018 + +### 3.0 Release - Backwards incompatible + +**New Features** + +#### 1. The JSON loader has been removed in favour of a new `addRules` method. + +```php +$dice->addRules([ + '\PDO' => [ + 'shared' => true + ], + 'Framework\Router' => [ + 'constructParams' => ['Foo', 'Bar'] + ] +]); +``` + +The puropse of this addition is to make the JSON loader redundant. Loading of rules from a JSON file can easily be achieved with the code: + +```php +$dice->addRules(json_decode(file_get_contents('rules.json'))); +``` + +#### 2. Better JSON file support: constants and superglobals + +In order to improve support for rules being defined in external JSON files, constants and superglobals can now be passed into objects created by Dice. + +For example, passing the `$_SERVER` superglobal into a router instance and calling PDO's `setAttribute` with `PDO::ATTR_ERRMODE` and `PDO::ERRMODE_EXCEPTION` can be achieved like this in a JSON file: + +_rules.json_ + +```json +{ + "Router": { + "constructParams": [ + {"Dice::GLOBAL": "_SERVER"} + ] + }, + "PDO": { + "shared": true, + "constructParams": [ + "mysql:dbname=testdb;host=127.0.0.1", + "dbuser", + "dbpass" + ], + "call": [ + [ + "setAttribute", + [ + {"Dice::CONSTANT": "PDO::ATTR_ERRMODE"}, + {"Dice::CONSTANT": "PDO::ERRMODE_EXCEPTION"} + ] + ] + ] + } +} + + +```php +$dice->addRules(json_decode(file_get_contents('rules.json'))); +``` + +**Backwards incompatible changes** + +1. Dice 3.0 requires PHP 7.0 or above, PHP 5.6 is no longer supported. + +2. Dice no longer supports `'instance'` keys to signify instances. For example: + +```php +$dice->addRule('ClassName', [ + 'constructParams' => ['instance' => '$NamedPDOInstance'] +]); +``` + +As noted in issue #125 this made it impossible to pass an array to a constructor if the array had a key `'instance'`. Instead, the new `\Dice\Dice::INSTANCE` constant should be used: + +```php +$dice->addRule('ClassName', [ + 'constructParams' => [\Dice\Dice::INSTANCE => '$NamedPDOInstance'] +]); +``` +_to make the constant shorter to type out, you can `use \Dice\Dice;` and reference `Dice::INSTANCE`_ + 10/06/2016 ** Backwards incompatible change ** @@ -99,7 +184,7 @@ $dice->addRule('$MyNamedInstance', $rule); ``` -`$dice->create('$MyNamedInstance')` will now create a class following the rules applied to both `MyClass` and `$MyNamedInstance` so the instance will be shared. +`$dice->create('$MyNamedInstance')` will now create a class following the rules applied to both `MyClass` and `$MyNamedInstance` so the instance will be shared. Previously only the rules applied to the named instance would be used. @@ -127,7 +212,7 @@ $dice->addRule('$MyNamedInstance', $rule); 29/10/2014 -* Based on [Issue #15](https://github.com/TomBZombie/Dice/issues/15), Dice will now only call closures if they are wrapped in \Dice\Instance. **PLEASE NOTE: THIS IS BACKWARDS INCOMPATIBLE **. +* Based on [Issue #15](https://github.com/TomBZombie/Dice/issues/15), Dice will now only call closures if they are wrapped in \Dice\Instance. **PLEASE NOTE: THIS IS BACKWARDS INCOMPATIBLE **. Previously Dice ran closures that were passed as substitutions, constructParams and when calling methods: @@ -146,9 +231,9 @@ $rule->constructParams[] = function() { //'abc' will be providedas the first constructor parameter return 'abc'; }; -``` +``` -This behaviour has changed as it makes it impossible to provide a closure as a construct parameter or when calling a method because the closure was always called and executed. +This behaviour has changed as it makes it impossible to provide a closure as a construct parameter or when calling a method because the closure was always called and executed. To overcome this, Dice will now only call a closures if they're wrapped in \Dice\Instance: @@ -166,7 +251,7 @@ $rule->constructParams[] = ['instance' => function() { { //'abc' will be providedas the first constructor parameter return 'abc'; }]); -``` +``` @@ -197,7 +282,7 @@ $rule->constructParams[] = ['instance' => function() { { * Added a JSON loader + test case * Added all test cases to a test suite * Moved to PHP5.4 array syntax. A PHP5.3 compatible version is now available in the PHP5.3 branch. -* Fixed an issue where using named instances would trigger the autoloader with an invalid class name every time a class was created +* Fixed an issue where using named instances would trigger the autoloader with an invalid class name every time a class was created 28/02/2014 diff --git a/composer.json b/composer.json index 8ce9892..0b30a04 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "level-2/dice", - "description": "A minimalist Dependency injection container (DIC) for PHP. Please note: This branch is only compatible with PHP5.6. 5.5, 5.4 and 5.3 compatible version is available as a separate branch on github. ", + "description": "A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.", "license": "BSD-2-Clause", "homepage": "http://r.je/dice.html", "keywords": ["Dependency injection", "ioc", "Dependency injection container", "DI"], @@ -11,7 +11,7 @@ } ], "require": { - "php": ">=5.6.0" + "php": ">=7.0.0" }, "autoload": { "psr-4": { diff --git a/tests/BasicTest.php b/tests/BasicTest.php index c653b51..f4fea65 100644 --- a/tests/BasicTest.php +++ b/tests/BasicTest.php @@ -1,11 +1,9 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class BasicTest extends DiceTest { @@ -175,4 +173,33 @@ public function testScalarTypeHintWithShareInstances() { $this->assertInstanceOf('ScalarTypeHint', $obj); } + + public function testPassGlobals() { + //write to the global $_GET variable + $_GET['foo'] = 'bar'; + + $this->dice->addRule('CheckConstructorArgs', + [ + 'constructParams' => [ + [\Dice\Dice::GLOBAL => '_GET'] + ] + ]); + + $obj = $this->dice->create('CheckConstructorArgs'); + + $this->assertEquals($_GET, $obj->arg1); + } + + public function testPassConstantString() { + $this->dice->addRule('CheckConstructorArgs', + [ + 'constructParams' => [ + [\Dice\Dice::CONSTANT => '\PDO::FETCH_ASSOC'] + ] + ]); + + $obj = $this->dice->create('CheckConstructorArgs'); + + $this->assertEquals(\PDO::FETCH_ASSOC, $obj->arg1); + } } \ No newline at end of file diff --git a/tests/CallTest.php b/tests/CallTest.php index 47011d9..49fd62c 100644 --- a/tests/CallTest.php +++ b/tests/CallTest.php @@ -1,11 +1,9 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class CallTest extends DiceTest { public function testCall() { $rule = []; @@ -14,7 +12,7 @@ public function testCall() { $object = $this->dice->create('TestCall'); $this->assertTrue($object->isCalled); } - + public function testCallWithParameters() { $rule = []; $rule['call'][] = array('callMe', array('one', 'two')); @@ -23,15 +21,15 @@ public function testCallWithParameters() { $this->assertEquals('one', $object->foo); $this->assertEquals('two', $object->bar); } - + public function testCallWithInstance() { $rule = []; - $rule['call'][] = array('callMe', array(['instance' => 'A'])); + $rule['call'][] = array('callMe', array([\Dice\Dice::INSTANCE => 'A'])); $this->dice->addRule('TestCall3', $rule); $object = $this->dice->create('TestCall3'); - + $this->assertInstanceOf('a', $object->a); - + } public function testCallAutoWireInstance() { @@ -39,7 +37,7 @@ public function testCallAutoWireInstance() { $rule['call'][] = array('callMe', []); $this->dice->addRule('TestCall3', $rule); $object = $this->dice->create('TestCall3'); - + $this->assertInstanceOf('a', $object->a); } @@ -55,7 +53,7 @@ public function testCallReturnValue() { $this->dice->addRule('TestCall3', $rule); $object = $this->dice->create('TestCall3'); - + $this->assertInstanceOf('a', $object->a); $this->assertEquals('callMe called', $returnValue); } diff --git a/tests/ConstructParamsTest.php b/tests/ConstructParamsTest.php index 9cdb052..093f82e 100644 --- a/tests/ConstructParamsTest.php +++ b/tests/ConstructParamsTest.php @@ -1,20 +1,18 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class ConstructParamsTest extends DiceTest { public function testConstructParams() { $rule = []; $rule['constructParams'] = array('foo', 'bar'); $this->dice->addRule('RequiresConstructorArgsA', $rule); - + $obj = $this->dice->create('RequiresConstructorArgsA'); - + $this->assertEquals($obj->foo, 'foo'); $this->assertEquals($obj->bar, 'bar'); } @@ -23,40 +21,40 @@ public function testConstructParams() { public function testInternalClass() { $rule = []; $rule['constructParams'][] = '.'; - + $this->dice->addRule('DirectoryIterator', $rule); - + $dir = $this->dice->create('DirectoryIterator'); - + $this->assertInstanceOf('DirectoryIterator', $dir); } - + public function testInternalClassExtended() { $rule = []; $rule['constructParams'][] = '.'; - + $this->dice->addRule('MyDirectoryIterator', $rule); - + $dir = $this->dice->create('MyDirectoryIterator'); - + $this->assertInstanceOf('MyDirectoryIterator', $dir); } - - + + public function testInternalClassExtendedConstructor() { $rule = []; $rule['constructParams'][] = '.'; - + $this->dice->addRule('MyDirectoryIterator2', $rule); - + $dir = $this->dice->create('MyDirectoryIterator2'); - + $this->assertInstanceOf('MyDirectoryIterator2', $dir); } public function testDefaultNullAssigned() { $rule = []; - $rule['constructParams'] = [ ['instance' => 'A'], null]; + $rule['constructParams'] = [ [\Dice\Dice::INSTANCE => 'A'], null]; $this->dice->addRule('MethodWithDefaultNull', $rule); $obj = $this->dice->create('MethodWithDefaultNull'); $this->assertNull($obj->b); @@ -70,21 +68,21 @@ public function testConstructParamsNested() { $rule = []; $rule['shareInstances'] = array('D'); $this->dice->addRule('ParamRequiresArgs', $rule); - + $obj = $this->dice->create('ParamRequiresArgs'); - + $this->assertEquals($obj->a->foo, 'foo'); $this->assertEquals($obj->a->bar, 'bar'); } - + public function testConstructParamsMixed() { $rule = []; $rule['constructParams'] = array('foo', 'bar'); $this->dice->addRule('RequiresConstructorArgsB', $rule); - + $obj = $this->dice->create('RequiresConstructorArgsB'); - + $this->assertEquals($obj->foo, 'foo'); $this->assertEquals($obj->bar, 'bar'); $this->assertInstanceOf('A', $obj->a); diff --git a/tests/CreateArgsTest.php b/tests/CreateArgsTest.php index 7e10088..d2b5e29 100644 --- a/tests/CreateArgsTest.php +++ b/tests/CreateArgsTest.php @@ -1,42 +1,40 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class CreateArgsTest extends DiceTest { public function testConsumeArgs() { $rule = []; - $rule['constructParams'] = ['A']; + $rule['constructParams'] = ['A']; $this->dice->addRule('ConsumeArgsSub', $rule); $foo = $this->dice->create('ConsumeArgsTop',['B']); - + $this->assertEquals('A', $foo->a->s); } public function testConstructArgs() { - $obj = $this->dice->create('RequiresConstructorArgsA', array('foo', 'bar')); + $obj = $this->dice->create('RequiresConstructorArgsA', array('foo', 'bar')); $this->assertEquals($obj->foo, 'foo'); $this->assertEquals($obj->bar, 'bar'); } - + public function testConstructArgsMixed() { $obj = $this->dice->create('RequiresConstructorArgsB', array('foo', 'bar')); $this->assertEquals($obj->foo, 'foo'); $this->assertEquals($obj->bar, 'bar'); $this->assertInstanceOf('A', $obj->a); } - + public function testCreateArgs1() { $a = $this->dice->create('A', array($this->dice->create('ExtendedB'))); $this->assertInstanceOf('ExtendedB', $a->b); } - - + + public function testCreateArgs2() { $a2 = $this->dice->create('A2', array($this->dice->create('ExtendedB'), 'Foo')); $this->assertInstanceOf('B', $a2->b); @@ -44,7 +42,7 @@ public function testCreateArgs2() { $this->assertEquals($a2->foo, 'Foo'); } - + public function testCreateArgs3() { //reverse order args. It should be smart enough to handle this. $a2 = $this->dice->create('A2', array('Foo', $this->dice->create('ExtendedB'))); @@ -52,7 +50,7 @@ public function testCreateArgs3() { $this->assertInstanceOf('C', $a2->c); $this->assertEquals($a2->foo, 'Foo'); } - + public function testCreateArgs4() { $a2 = $this->dice->create('A3', array('Foo', $this->dice->create('ExtendedB'))); $this->assertInstanceOf('B', $a2->b); @@ -65,7 +63,7 @@ public function testBestMatch() { $this->assertEquals('foo', $bestMatch->string); $this->assertInstanceOf('A', $bestMatch->a); } - + public function testTwoDefaultNullClass() { $obj = $this->dice->create('MethodWithTwoDefaultNullC'); $this->assertNull($obj->a); diff --git a/tests/DiceTest.php b/tests/DiceTest.php index 61674b5..b02c012 100644 --- a/tests/DiceTest.php +++ b/tests/DiceTest.php @@ -1,11 +1,9 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ abstract class DiceTest extends \PHPUnit\Framework\TestCase { protected $dice; @@ -16,24 +14,27 @@ public function __construct() { //Load the test classes for this test $name = str_replace('Test', '', get_class($this)); require_once 'tests/TestData/Basic.php'; - require_once 'tests/TestData/' . $name . '.php'; + + if (file_exists('tests/TestData/' . $name . '.php')) { + require_once 'tests/TestData/' . $name . '.php'; + } } - + public function autoload($class) { //If Dice Triggers the autoloader the test fails - //This generally means something invalid has been passed to + //This generally means something invalid has been passed to //a method such as is_subclass_of or dice is trying to construct //an object from something it shouldn't. $this->fail('Autoload triggered: ' . $class); } - + protected function setUp() { parent::setUp (); - $this->dice = new \Dice\Dice(); + $this->dice = new \Dice\Dice(); } protected function tearDown() { - $this->dice = null; + $this->dice = null; parent::tearDown (); } } \ No newline at end of file diff --git a/tests/Loaders/JsonLoaderTest.php b/tests/Loaders/JsonLoaderTest.php deleted file mode 100644 index f4ea5a7..0000000 --- a/tests/Loaders/JsonLoaderTest.php +++ /dev/null @@ -1,237 +0,0 @@ - | http://r.je/dice.html * - * @license http://www.opensource.org/licenses/bsd-license.php BSD License * - * @version 2.0 */ - -require_once 'Dice.php'; -require_once 'Loader/Json.php'; - - -class JsonLoaderTest extends \PHPUnit\Framework\TestCase { - private $dice; - private $jsonLoader; - - protected function setUp() { - parent::setUp (); - $dice = new \Dice\Dice; - $this->dice = $this->createMock('\\Dice\\Dice', array('getRule', 'addRule')); - $this->dice->expects($this->any())->method('getRule')->will($this->returnValue($dice->getRule('*'))); - $this->jsonLoader = new \Dice\Loader\Json; - } - protected function tearDown() { - $this->dice = null; - $this->jsonLoader = null; - parent::tearDown (); - } - - - public function testSetDefaultRule() { - $json = '{ -"rules": [ - { - "name": "*", - "shared": true - } - ] -}'; - - - $equivalentRule = []; - $equivalentRule['shared'] = true; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('*'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - - } - - public function testShared() { - $json = '{ -"rules": [ - { - "name": "A", - "shared": true - } - ] -}'; - - - $equivalentRule = []; - $equivalentRule['shared'] = true; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - - public function testConstructParams() { - $json = '{ -"rules": [ - { - "name": "A", - "constructParams": ["A", "B"] - } - ] -}'; - - $equivalentRule = []; - $equivalentRule['constructParams'][] = 'A'; - $equivalentRule['constructParams'][] = 'B'; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - public function testSubstitutions() { - $json = '{ -"rules": [ - { - "name": "A", - "substitutions": {"B": {"instance": "C"}} - } - ] -}'; - - $equivalentRule = []; - $equivalentRule['substitutions']['B'] = ['instance' => 'C']; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - public function testSubstitutions2() { - $json = '{ -"rules": [ - { - "name": "A", - "substitutions": {"B": {"instance": "C"}, "F": {"instance": "E"}} - } - ] -}'; - - $equivalentRule = []; - $equivalentRule['substitutions']['B'] = ['instance' => 'C']; - $equivalentRule['substitutions']['F'] = ['instance' => 'E']; - - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - public function testSubstitutionsCall() { - $json = '{ -"rules": [ - { - "name": "A", - "substitutions": {"B": {"instance": ["JsonLoaderTest", "foo"]}} - } - ] -}'; - - $equivalentRule = []; - $equivalentRule['substitutions']['B'] = ['instance' => ['JsonLoaderTest', 'foo']]; - - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - public function testInstanceOf() { - $json = '{ -"rules": [ - { - "name": "[C]", - "instanceOf": "C" - } - ] -}'; - $equivalentRule = []; - $equivalentRule['instanceOf'] = 'C'; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('[C]'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - public function testCall() { - $json = '{ -"rules": [ - { - "name": "A", - "call": [ - ["setFoo", ["Foo", "Bar"]] - ] - } - ] -}'; - - $equivalentRule = []; - $equivalentRule['call'][] = ['setFoo', ['Foo', 'Bar']]; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - - public function testInherit() { - $json = '{ -"rules": [ - { - "name": "A", - "inherit": true - } - ] -}'; - - - $equivalentRule = []; - $equivalentRule['inherit'] = true; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - public function testInherit2() { - $json = '{ -"rules": [ - { - "name": "A", - "inherit": false - } - ] -}'; - $equivalentRule = []; - $equivalentRule['inherit'] = false; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - - - public function testShareInstance() { - $json = '{ -"rules": [ - { - "name": "A", - "shareInstances": ["C", "D"] - } - ] -}'; - $equivalentRule = []; - $equivalentRule['shareInstances'] = ['C', 'D']; - - $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); - $this->jsonLoader->load($json, $this->dice); - } - - -} diff --git a/tests/Loaders/XmlLoaderTest.php b/tests/Loaders/XmlLoaderTest.php index 5cadce6..fa382a4 100644 --- a/tests/Loaders/XmlLoaderTest.php +++ b/tests/Loaders/XmlLoaderTest.php @@ -1,10 +1,9 @@ | http://r.je/dice.html * - * @license http://www.opensource.org/licenses/bsd-license.php BSD License * - * @version 20. */ - +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ require_once 'Dice.php'; require_once 'Loader/Xml.php'; @@ -16,18 +15,18 @@ class XmlLoaderTest extends \PHPUnit\Framework\TestCase { protected function setUp() { parent::setUp (); $dice = new \Dice\Dice; - $this->dice = $this->createMock('\\Dice\\Dice', array('getRule', 'addRule')); + $this->dice = $this->createMock('\\Dice\\Dice', array('getRule', 'addRule')); $this->dice->expects($this->any())->method('getRule')->will($this->returnValue($dice->getRule('*'))); $this->xmlLoader = new \Dice\Loader\XML; } protected function tearDown() { - $this->dice = null; + $this->dice = null; $this->xmlLoader = null; parent::tearDown (); } - - + + public function testSetDefaultRule() { $xml = ' @@ -36,16 +35,16 @@ public function testSetDefaultRule() { true '; - - + + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['shared'] = true; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('*'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); - + } - + public function testShared() { $xml = ' @@ -54,17 +53,17 @@ public function testShared() { true '; - - + + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['shared'] = true; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - - + + + public function testConstructParams() { $xml = ' @@ -76,17 +75,17 @@ public function testConstructParams() { '; - - + + $equivalentRule = []; $equivalentRule['constructParams'][] = 'A'; $equivalentRule['constructParams'][] = 'B'; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testSubstitutions() { $xml = ' @@ -98,16 +97,16 @@ public function testSubstitutions() { '; - - + + $equivalentRule = []; - $equivalentRule['substitutions']['B'] = ['instance' => 'C']; - + $equivalentRule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'C']; + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testSubstitutions2() { $xml = ' @@ -117,24 +116,24 @@ public function testSubstitutions2() { C B - + E F '; - - + + $equivalentRule = $this->dice->getRule('*'); - $equivalentRule['substitutions']['B'] = ['instance' => 'C']; - $equivalentRule['substitutions']['F'] = ['instance' => 'E']; - - + $equivalentRule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'C']; + $equivalentRule['substitutions']['F'] = [\Dice\Dice::INSTANCE => 'E']; + + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - + public function testInstanceOf() { $xml = ' @@ -143,38 +142,38 @@ public function testInstanceOf() { C '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['instanceOf'] = 'C'; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('[C]'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testCall() { $xml = ' A - setFoo - - Foo - Bar + setFoo + + Foo + Bar - + '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['call'][] = ['setFoo', ['Foo', 'Bar']]; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testInherit() { $xml = ' @@ -184,15 +183,15 @@ public function testInherit() { true '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['inherit'] = true; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testInherit2() { $xml = ' @@ -201,14 +200,14 @@ public function testInherit2() { false '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['inherit'] = false; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - + public function testShareInstance() { $xml = ' @@ -218,13 +217,13 @@ public function testShareInstance() { D '; - + $equivalentRule = []; $equivalentRule['shareInstances'] = ['C', 'D']; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + } diff --git a/tests/Loaders/XmlLoaderV2Test.php b/tests/Loaders/XmlLoaderV2Test.php index 49a27cb..01f77bf 100644 --- a/tests/Loaders/XmlLoaderV2Test.php +++ b/tests/Loaders/XmlLoaderV2Test.php @@ -1,10 +1,9 @@ | http://r.je/dice.html * - * @license http://www.opensource.org/licenses/bsd-license.php BSD License * - * @version 20. */ - +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ require_once 'Dice.php'; require_once 'Loader/Xml.php'; @@ -16,33 +15,33 @@ class XmlLoaderV2Test extends \PHPUnit\Framework\TestCase { protected function setUp() { parent::setUp (); $dice = new \Dice\Dice; - $this->dice = $this->createMock('\\Dice\\Dice', array('getRule', 'addRule')); + $this->dice = $this->createMock('\\Dice\\Dice', array('getRule', 'addRule')); $this->dice->expects($this->any())->method('getRule')->will($this->returnValue($dice->getRule('*'))); $this->xmlLoader = new \Dice\Loader\XML; } protected function tearDown() { - $this->dice = null; + $this->dice = null; $this->xmlLoader = null; parent::tearDown (); } - - + + public function testSetDefaultRule() { $xml = ' '; - - + + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['shared'] = true; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('*'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); - + } - + public function testShared() { $xml = ' @@ -51,17 +50,17 @@ public function testShared() { true '; - - + + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['shared'] = true; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - - + + + public function testConstructParams() { $xml = ' @@ -72,17 +71,17 @@ public function testConstructParams() { '; - - + + $equivalentRule = []; $equivalentRule['constructParams'][] = 'A'; $equivalentRule['constructParams'][] = 'B'; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testSubstitutions() { $xml = ' @@ -91,16 +90,16 @@ public function testSubstitutions() { '; - - + + $equivalentRule = []; - $equivalentRule['substitutions']['B'] = ['instance' => 'C']; - + $equivalentRule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'C']; + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testSubstitutions2() { $xml = ' @@ -109,79 +108,79 @@ public function testSubstitutions2() { '; - - + + $equivalentRule = $this->dice->getRule('*'); - $equivalentRule['substitutions']['B'] = ['instance' => 'C']; - $equivalentRule['substitutions']['F'] = ['instance' => 'E']; - - + $equivalentRule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'C']; + $equivalentRule['substitutions']['F'] = [\Dice\Dice::INSTANCE => 'E']; + + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testInstanceOf() { $xml = ' '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['instanceOf'] = 'C'; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('[C]'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testCall() { $xml = ' - Foo - Bar - + Foo + Bar + '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['call'][] = ['setFoo', ['Foo', 'Bar']]; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testInherit() { $xml = ' '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['inherit'] = true; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + public function testInherit2() { $xml = ' '; - + $equivalentRule = $this->dice->getRule('*'); $equivalentRule['inherit'] = false; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - + public function testShareInstance() { $xml = ' @@ -190,16 +189,16 @@ public function testShareInstance() { C D - + '; - + $equivalentRule = []; $equivalentRule['shareInstances'] = ['C', 'D']; - + $this->dice->expects($this->once())->method('addRule')->with($this->equalTo('A'), $this->equalTo($equivalentRule)); $this->xmlLoader->load(simplexml_load_string($xml), $this->dice); } - - + + } diff --git a/tests/NamedInstancesTest.php b/tests/NamedInstancesTest.php index c783511..d6f4019 100644 --- a/tests/NamedInstancesTest.php +++ b/tests/NamedInstancesTest.php @@ -1,111 +1,109 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class NamedInstancesTest extends DiceTest { public function testMultipleSharedInstancesByNameMixed() { $rule = []; $rule['shared'] = true; $rule['constructParams'][] = 'FirstY'; - + $this->dice->addRule('Y', $rule); - + $rule = []; $rule['instanceOf'] = 'Y'; $rule['shared'] = true; $rule['inherit'] = false; $rule['constructParams'][] = 'SecondY'; - + $this->dice->addRule('[Y2]', $rule); - + $rule = []; - $rule['constructParams'] = [ ['instance' => 'Y'], ['instance' => '[Y2]']]; - + $rule['constructParams'] = [ [\Dice\Dice::INSTANCE => 'Y'], [\Dice\Dice::INSTANCE => '[Y2]']]; + $this->dice->addRule('Z', $rule); - + $z = $this->dice->create('Z'); $this->assertEquals($z->y1->name, 'FirstY'); - $this->assertEquals($z->y2->name, 'SecondY'); + $this->assertEquals($z->y2->name, 'SecondY'); } public function testNonSharedComponentByNameA() { $rule = []; $rule['instanceOf'] = 'ExtendedB'; $this->dice->addRule('$B', $rule); - + $rule = []; - $rule['constructParams'][] = ['instance' => '$B']; + $rule['constructParams'][] = [\Dice\Dice::INSTANCE => '$B']; $this->dice->addRule('A', $rule); - + $a = $this->dice->create('A'); $this->assertInstanceOf('ExtendedB', $a->b); } - + public function testNonSharedComponentByName() { - + $rule = []; $rule['instanceOf'] = 'Y3'; $rule['constructParams'][] = 'test'; - - + + $this->dice->addRule('$Y2', $rule); - - + + $y2 = $this->dice->create('$Y2'); //echo $y2->name; $this->assertInstanceOf('Y3', $y2); - + $rule = []; - $rule['constructParams'][] = ['instance' => '$Y2']; + $rule['constructParams'][] = [\Dice\Dice::INSTANCE => '$Y2']; $this->dice->addRule('Y1', $rule); - + $y1 = $this->dice->create('Y1'); $this->assertInstanceOf('Y3', $y1->y2); } - + public function testSubstitutionByName() { $rule = []; $rule['instanceOf'] = 'ExtendedB'; $this->dice->addRule('$B', $rule); - + $rule = []; - $rule['substitutions']['B'] = ['instance' => '$B']; - - $this->dice->addRule('A', $rule); + $rule['substitutions']['B'] = [\Dice\Dice::INSTANCE => '$B']; + + $this->dice->addRule('A', $rule); $a = $this->dice->create('A'); - + $this->assertInstanceOf('ExtendedB', $a->b); } - + public function testMultipleSubstitutions() { $rule = []; $rule['instanceOf'] = 'Y2'; $rule['constructParams'][] = 'first'; $this->dice->addRule('$Y2A', $rule); - + $rule = []; $rule['instanceOf'] = 'Y2'; $rule['constructParams'][] = 'second'; $this->dice->addRule('$Y2B', $rule); - + $rule = []; - $rule['constructParams'] = array(['instance' => '$Y2A'], ['instance' => '$Y2B']); + $rule['constructParams'] = array([\Dice\Dice::INSTANCE => '$Y2A'], [\Dice\Dice::INSTANCE => '$Y2B']); $this->dice->addRule('HasTwoSameDependencies', $rule); - + $twodep = $this->dice->create('HasTwoSameDependencies'); - + $this->assertEquals('first', $twodep->y2a->name); - $this->assertEquals('second', $twodep->y2b->name); + $this->assertEquals('second', $twodep->y2b->name); } public function testNamedInstanceCallWithInheritance() { $rule1 = []; - $rule1['call'] = [ + $rule1['call'] = [ ['callMe', [1, 3] ], ['callMe', [3, 4] ] ]; @@ -121,10 +119,10 @@ public function testNamedInstanceCallWithInheritance() { $this->assertEquals(array_merge_recursive($rule1, $rule2), $this->dice->getRule('$MyInstance')); } - + public function testNamedInstanceCallWithoutInheritance() { $rule1 = []; - $rule1['call'] = [ + $rule1['call'] = [ ['callMe', [1, 3] ], ['callMe', [3, 4] ] ]; @@ -140,5 +138,5 @@ public function testNamedInstanceCallWithoutInheritance() { $this->assertEquals($rule2, $this->dice->getRule('$MyInstance')); } - + } \ No newline at end of file diff --git a/tests/NamespaceTest.php b/tests/NamespaceTest.php index 66d7421..7f90a70 100644 --- a/tests/NamespaceTest.php +++ b/tests/NamespaceTest.php @@ -1,70 +1,68 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class NamespaceTest extends DiceTest { public function testNamespaceBasic() { $a = $this->dice->create('Foo\\A'); $this->assertInstanceOf('Foo\\A', $a); } - - + + public function testNamespaceWithSlash() { $a = $this->dice->create('\\Foo\\A'); $this->assertInstanceOf('\\Foo\\A', $a); } - + public function testNamespaceWithSlashrule() { $rule = []; - $rule['substitutions']['Foo\\A'] = ['instance' => 'Foo\\ExtendedA']; + $rule['substitutions']['Foo\\A'] = [\Dice\Dice::INSTANCE => 'Foo\\ExtendedA']; $this->dice->addRule('\\Foo\\B', $rule); - + $b = $this->dice->create('\\Foo\\B'); $this->assertInstanceOf('Foo\\ExtendedA', $b->a); } - + public function testNamespaceWithSlashruleInstance() { $rule = []; - $rule['substitutions']['Foo\\A'] = ['instance' => 'Foo\\ExtendedA']; + $rule['substitutions']['Foo\\A'] = [\Dice\Dice::INSTANCE => 'Foo\\ExtendedA']; $this->dice->addRule('\\Foo\\B', $rule); - + $b = $this->dice->create('\\Foo\\B'); $this->assertInstanceOf('Foo\\ExtendedA', $b->a); } - + public function testNamespaceTypeHint() { $rule = []; $rule['shared'] = true; $this->dice->addRule('Bar\\A', $rule); - + $c = $this->dice->create('Foo\\C'); $this->assertInstanceOf('Bar\\A', $c->a); - + $c2 = $this->dice->create('Foo\\C'); $this->assertNotSame($c, $c2); - + //Check the rule has been correctly recognised for type hinted classes in a different namespace $this->assertSame($c2->a, $c->a); } - + public function testNamespaceInjection() { $b = $this->dice->create('Foo\\B'); $this->assertInstanceOf('Foo\\B', $b); - $this->assertInstanceOf('Foo\\A', $b->a); + $this->assertInstanceOf('Foo\\A', $b->a); } - - + + public function testNamespaceRuleSubstitution() { $rule = []; - $rule['substitutions']['Foo\\A'] = ['instance' => 'Foo\\ExtendedA']; + $rule['substitutions']['Foo\\A'] = [\Dice\Dice::INSTANCE => 'Foo\\ExtendedA']; $this->dice->addRule('Foo\\B', $rule); - + $b = $this->dice->create('Foo\\B'); $this->assertInstanceOf('Foo\\ExtendedA', $b->a); } - + } \ No newline at end of file diff --git a/tests/ShareInstancesTest.php b/tests/ShareInstancesTest.php index 0260246..91fe6ea 100644 --- a/tests/ShareInstancesTest.php +++ b/tests/ShareInstancesTest.php @@ -1,30 +1,28 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class ShareInstancesTest extends DiceTest { public function testShareInstances() { $rule = []; $rule['shareInstances'] = ['Shared']; $this->dice->addRule('TestSharedInstancesTop', $rule); - - + + $shareTest = $this->dice->create('TestSharedInstancesTop'); - + $this->assertinstanceOf('TestSharedInstancesTop', $shareTest); - + $this->assertInstanceOf('SharedInstanceTest1', $shareTest->share1); $this->assertInstanceOf('SharedInstanceTest2', $shareTest->share2); - + $this->assertSame($shareTest->share1->shared, $shareTest->share2->shared); $this->assertEquals($shareTest->share1->shared->uniq, $shareTest->share2->shared->uniq); - + } - + public function testNamedShareInstances() { $rule = []; @@ -34,15 +32,15 @@ public function testNamedShareInstances() { $rule = []; $rule['shareInstances'] = ['$Shared']; $this->dice->addRule('TestSharedInstancesTop', $rule); - - + + $shareTest = $this->dice->create('TestSharedInstancesTop'); - + $this->assertinstanceOf('TestSharedInstancesTop', $shareTest); - + $this->assertInstanceOf('SharedInstanceTest1', $shareTest->share1); $this->assertInstanceOf('SharedInstanceTest2', $shareTest->share2); - + $this->assertSame($shareTest->share1->shared, $shareTest->share2->shared); $this->assertEquals($shareTest->share1->shared->uniq, $shareTest->share2->shared->uniq); @@ -59,31 +57,31 @@ public function testShareInstancesNested() { $a = $this->dice->create('A4'); $this->assertTrue($a->m1->f === $a->m2->e->f); } - - + + public function testShareInstancesMultiple() { $rule = []; $rule['shareInstances'] = ['Shared']; $this->dice->addRule('TestSharedInstancesTop', $rule); - - + + $shareTest = $this->dice->create('TestSharedInstancesTop'); - + $this->assertinstanceOf('TestSharedInstancesTop', $shareTest); - + $this->assertInstanceOf('SharedInstanceTest1', $shareTest->share1); $this->assertInstanceOf('SharedInstanceTest2', $shareTest->share2); - + $this->assertSame($shareTest->share1->shared, $shareTest->share2->shared); $this->assertEquals($shareTest->share1->shared->uniq, $shareTest->share2->shared->uniq); - - + + $shareTest2 = $this->dice->create('TestSharedInstancesTop'); $this->assertSame($shareTest2->share1->shared, $shareTest2->share2->shared); $this->assertEquals($shareTest2->share1->shared->uniq, $shareTest2->share2->shared->uniq); - + $this->assertNotSame($shareTest->share1->shared, $shareTest2->share2->shared); $this->assertNotEquals($shareTest->share1->shared->uniq, $shareTest2->share2->shared->uniq); - + } } \ No newline at end of file diff --git a/tests/SubstitutionsTest.php b/tests/SubstitutionsTest.php index 7b346d2..359c9b3 100644 --- a/tests/SubstitutionsTest.php +++ b/tests/SubstitutionsTest.php @@ -1,22 +1,20 @@ -* @link http://r.je/dice.html -* @license http://www.opensource.org/licenses/bsd-license.php BSD License -* @version 2.0 -*/ +/* @description Dice - A minimal Dependency Injection Container for PHP * + * @author Tom Butler tom@r.je * + * @copyright 2012-2018 Tom Butler | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class SubstitutionsTest extends DiceTest { public function testNoMoreAssign() { $rule = []; - $rule['substitutions']['Bar77'] = ['instance' => function() { + $rule['substitutions']['Bar77'] = [\Dice\Dice::INSTANCE => function() { return Baz77::create(); }]; - + $this->dice->addRule('Foo77', $rule); - + $foo = $this->dice->create('Foo77'); - + $this->assertInstanceOf('Bar77', $foo->bar); $this->assertEquals('Z', $foo->bar->a); } @@ -27,39 +25,39 @@ public function testNullSubstitution() { $this->dice->addRule('MethodWithDefaultNull', $rule); $obj = $this->dice->create('MethodWithDefaultNull'); $this->assertNull($obj->b); - } - + } + public function testSubstitutionText() { $rule = []; - $rule['substitutions']['B'] = ['instance' => 'ExtendedB']; + $rule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'ExtendedB']; $this->dice->addRule('A', $rule); - + $a = $this->dice->create('A'); - + $this->assertInstanceOf('ExtendedB', $a->b); } public function testSubstitutionTextMixedCase() { $rule = []; - $rule['substitutions']['B'] = ['instance' => 'exTenDedb']; + $rule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'exTenDedb']; $this->dice->addRule('A', $rule); - + $a = $this->dice->create('A'); - + $this->assertInstanceOf('ExtendedB', $a->b); } public function testSubstitutionCallback() { $rule = []; $injection = $this->dice; - $rule['substitutions']['B'] = ['instance' => function() use ($injection) { + $rule['substitutions']['B'] = [\Dice\Dice::INSTANCE => function() use ($injection) { return $injection->create('ExtendedB'); }]; - + $this->dice->addRule('A', $rule); - + $a = $this->dice->create('A'); - + $this->assertInstanceOf('ExtendedB', $a->b); } @@ -68,25 +66,25 @@ public function testSubstitutionObject() { $rule = []; $rule['substitutions']['B'] = $this->dice->create('ExtendedB'); - + $this->dice->addRule('A', $rule); - + $a = $this->dice->create('A'); $this->assertInstanceOf('ExtendedB', $a->b); } - + public function testSubstitutionString() { $rule = []; - - $rule['substitutions']['B'] = ['instance' => 'ExtendedB']; - + + $rule['substitutions']['B'] = [\Dice\Dice::INSTANCE => 'ExtendedB']; + $this->dice->addRule('A', $rule); - + $a = $this->dice->create('A'); $this->assertInstanceOf('ExtendedB', $a->b); } - - + + public function testSubFromString() { $rule = [ 'substitutions' => ['Bar' => 'Baz'] diff --git a/tests/TestData/Basic.php b/tests/TestData/Basic.php index c8bce6b..32f8ff7 100644 --- a/tests/TestData/Basic.php +++ b/tests/TestData/Basic.php @@ -1,5 +1,9 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class NoConstructor { public $a = 'b'; } @@ -133,4 +137,12 @@ class ScalarTypeHint { public function __construct(string $a = null) { } +} + +class CheckConstructorArgs { + public $arg1; + + public function __construct($arg1) { + $this->arg1 = $arg1; + } } \ No newline at end of file diff --git a/tests/TestData/Call.php b/tests/TestData/Call.php index 4a82aac..44159e0 100644 --- a/tests/TestData/Call.php +++ b/tests/TestData/Call.php @@ -1,8 +1,12 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class TestCall { public $isCalled = false; - + public function callMe() { $this->isCalled = true; } diff --git a/tests/TestData/ConstructParams.php b/tests/TestData/ConstructParams.php index f623737..ac93304 100644 --- a/tests/TestData/ConstructParams.php +++ b/tests/TestData/ConstructParams.php @@ -1,7 +1,11 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class MyDirectoryIterator extends DirectoryIterator { - + } @@ -13,7 +17,7 @@ public function __construct($f) { class ParamRequiresArgs { public $a; - + public function __construct(D $d, RequiresConstructorArgsA $a) { $this->a = $a; } diff --git a/tests/TestData/CreateArgs.php b/tests/TestData/CreateArgs.php index b0fe460..52e4b6e 100644 --- a/tests/TestData/CreateArgs.php +++ b/tests/TestData/CreateArgs.php @@ -1,5 +1,9 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class ConsumeArgsTop { public $s; @@ -58,19 +62,19 @@ class BestMatch { public $a; public $string; public $b; - + public function __construct($string, A $a, B $b) { $this->a = $a; $this->string = $string; $this->b = $b; - } + } } //From: https://github.com/TomBZombie/Dice/issues/62#issuecomment-112370319 class ScalarConstructors { public $string; public $null; - + public function __construct($string, $null) { $this->string = $string; $this->null = $null; diff --git a/tests/TestData/NamedInstances.php b/tests/TestData/NamedInstances.php index 7aff9ee..013165a 100644 --- a/tests/TestData/NamedInstances.php +++ b/tests/TestData/NamedInstances.php @@ -1,5 +1,9 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class Z { public $y1; public $y2; @@ -11,7 +15,7 @@ public function __construct(Y $y1, Y $y2) { class Y1 { public $y2; - + public function __construct(Y2 $y2) { $this->y2 = $y2; } @@ -19,15 +23,15 @@ public function __construct(Y2 $y2) { class Y2 { - public $name; - + public $name; + public function __construct($name) { $this->name = $name; } } class Y3 extends Y2 { - + } @@ -42,7 +46,7 @@ public function __construct($name) { class HasTwoSameDependencies { public $y2a; public $y2b; - + public function __construct(Y2 $y2a, Y2 $y2b) { $this->y2a = $y2a; $this->y2b = $y2b; diff --git a/tests/TestData/Namespace.php b/tests/TestData/Namespace.php index 41d5f89..18bb0cb 100644 --- a/tests/TestData/Namespace.php +++ b/tests/TestData/Namespace.php @@ -1,26 +1,31 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ namespace Foo { class A { - + } class B { public $a; - + public function __construct(A $a) { - $this->a = $a; + $this->a = $a; } } class ExtendedA extends A { - + } class C { public $a; - + public function __construct(\Bar\A $a) { $this->a = $a; } @@ -30,10 +35,10 @@ public function __construct(\Bar\A $a) { namespace Bar { class A { - - } - + + } + class B { - + } } \ No newline at end of file diff --git a/tests/TestData/ShareInstances.php b/tests/TestData/ShareInstances.php index 8966e26..855a8ad 100644 --- a/tests/TestData/ShareInstances.php +++ b/tests/TestData/ShareInstances.php @@ -1,12 +1,17 @@ | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ class TestSharedInstancesTop { public $share1; public $share2; - + public function __construct(SharedInstanceTest1 $share1, SharedInstanceTest2 $share2) { $this->share1 = $share1; $this->share2 = $share2; - } + } } @@ -14,9 +19,9 @@ public function __construct(SharedInstanceTest1 $share1, SharedInstanceTest2 $sh class SharedInstanceTest1 { public $shared; - + public function __construct(Shared $shared) { - $this->shared = $shared; + $this->shared = $shared; } } @@ -70,7 +75,7 @@ public static function create() { class Shared { public $uniq; - + public function __construct() { $this->uniq = uniqid(); } diff --git a/tests/TestData/Substitutions.php b/tests/TestData/Substitutions.php deleted file mode 100644 index b3d9bbc..0000000 --- a/tests/TestData/Substitutions.php +++ /dev/null @@ -1 +0,0 @@ - | https:// r.je/dice.html * + * @license http:// www.opensource.org/licenses/bsd-license.php BSD License * + * @version 3.0 */ require_once 'Dice.php'; require_once 'tests/DiceTest.php';