From 46f7268dde191494fc7ecdde1bf9e4b10da2b917 Mon Sep 17 00:00:00 2001 From: Rougin Royce Gutib Date: Fri, 27 Oct 2017 15:29:29 +0800 Subject: [PATCH] Fix Configuration and ReflectionContainer --- CHANGELOG.md | 6 ++ src/Container/ReflectionContainer.php | 4 +- src/Integration/Configuration.php | 2 +- tests/Container/ReflectionContainerTest.php | 16 +++++ tests/Fixture/Classes/ParameterClass.php | 35 +++++++++++ .../Classes/WithMultipleParameters.php | 59 +++++++++++++++++++ tests/Integration/ConfigurationTest.php | 26 ++++++++ 7 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 tests/Fixture/Classes/ParameterClass.php create mode 100644 tests/Fixture/Classes/WithMultipleParameters.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 77205957..6132a18f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to `Slytherin` will be documented in this file. +## [0.9.2](https://github.com/rougin/slytherin/compare/v0.9.1...v0.9.2) - 2017-10-27 + +### Fixed +- Getting default values of a class in `Container\ReflectionContainer` +- Returning empty values in `Integration\Configuration` + ## [0.9.1](https://github.com/rougin/slytherin/compare/v0.9.0...v0.9.1) - 2017-07-21 ### Fixed diff --git a/src/Container/ReflectionContainer.php b/src/Container/ReflectionContainer.php index 1f543d0e..4408f20b 100644 --- a/src/Container/ReflectionContainer.php +++ b/src/Container/ReflectionContainer.php @@ -91,7 +91,9 @@ protected function arguments($reflector, $parameters = array()) $argument = $this->argument($parameter, $name); - $arguments[$key] = $argument ?: $parameters[$name]; + $exists = array_key_exists($name, $parameters); + + $arguments[$key] = $exists ? $parameters[$name] : $argument; } return $arguments; diff --git a/src/Integration/Configuration.php b/src/Integration/Configuration.php index e521ed47..68fca532 100644 --- a/src/Integration/Configuration.php +++ b/src/Integration/Configuration.php @@ -47,7 +47,7 @@ public function get($key, $default = null) $data = &$data[$index]; } - return (empty($data)) ? $default : $data; + return ($data !== null) ? $data : $default; } /** diff --git a/tests/Container/ReflectionContainerTest.php b/tests/Container/ReflectionContainerTest.php index a6baa400..26898239 100644 --- a/tests/Container/ReflectionContainerTest.php +++ b/tests/Container/ReflectionContainerTest.php @@ -51,6 +51,22 @@ public function testGetMethodWithParameter() $this->assertInstanceOf($class, $this->container->get($class)); } + /** + * Tests ContainerInterface::get with multiple parameters. + * + * @return void + */ + public function testGetMethodWithMultipleParameters() + { + $class = 'Rougin\Slytherin\Fixture\Classes\ParameterClass'; + + $expected = 'With multiple parameters'; + + $object = $this->container->get($class); + + $this->assertEquals($expected, $object->index()); + } + /** * Tests ContainerInterface::get with Psr\Container\ContainerExceptionInterface. * diff --git a/tests/Fixture/Classes/ParameterClass.php b/tests/Fixture/Classes/ParameterClass.php new file mode 100644 index 00000000..097cd397 --- /dev/null +++ b/tests/Fixture/Classes/ParameterClass.php @@ -0,0 +1,35 @@ + + */ +class ParameterClass +{ + /** + * @var \Rougin\Slytherin\Fixture\Classes\WithMultipleParameters + */ + protected $class; + + /** + * @param \Rougin\Slytherin\Fixture\Classes\WithMultipleParameters $class + */ + public function __construct(WithMultipleParameters $class) + { + $this->class = $class; + } + + /** + * Returns with a string of "With multiple parameters". + * + * @return string + */ + public function index() + { + return $this->class->index(); + } +} diff --git a/tests/Fixture/Classes/WithMultipleParameters.php b/tests/Fixture/Classes/WithMultipleParameters.php new file mode 100644 index 00000000..58017390 --- /dev/null +++ b/tests/Fixture/Classes/WithMultipleParameters.php @@ -0,0 +1,59 @@ + + */ +class WithMultipleParameters +{ + /** + * @var array + */ + protected $data = array(); + + /** + * @var array + */ + protected $fields = array(); + + /** + * @var null + */ + protected $lang = null; + + /** + * @var null + */ + protected $dir = null; + + /** + * @param array $data + * @param array $fields + * @param string|null $lang + * @param string|null $dir + */ + public function __construct($data = array(), $fields = array(), $lang = null, $dir = null) + { + $this->data = $data; + + $this->fields = $fields; + + $this->lang = $lang; + + $this->dir = $dir; + } + + /** + * Returns with a string of "With multiple parameters". + * + * @return string + */ + public function index() + { + return 'With multiple parameters'; + } +} diff --git a/tests/Integration/ConfigurationTest.php b/tests/Integration/ConfigurationTest.php index 1a76f361..b6c06573 100644 --- a/tests/Integration/ConfigurationTest.php +++ b/tests/Integration/ConfigurationTest.php @@ -24,6 +24,32 @@ public function testGetMethodWithString() $this->assertEquals($data['name'], $config->get('name')); } + /** + * Tests Configuration::get with integer and default value. + * + * @return void + */ + public function testGetMethodWithIntegerAndDefaultValue() + { + list($data, $default) = array(array('number' => 0), 1); + + $config = new Configuration($data); + + $this->assertEquals($data['number'], $config->get('number', $default)); + } + + /** + * Tests Conguration::get with default value. + * + * @return void + */ + public function testGetMethodWithDefaultValue() + { + $config = new Configuration; + + $this->assertNull($config->get('name')); + } + /** * Tests Configuration::get with array. *