Skip to content

Commit

Permalink
Fix Configuration and ReflectionContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Dec 14, 2017
1 parent dd87202 commit 46f7268
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/Container/ReflectionContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function get($key, $default = null)
$data = &$data[$index];
}

return (empty($data)) ? $default : $data;
return ($data !== null) ? $data : $default;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Container/ReflectionContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/Fixture/Classes/ParameterClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rougin\Slytherin\Fixture\Classes;

/**
* Parameter Class
*
* @package Slytherin
* @author Rougin Royce Gutib <[email protected]>
*/
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();
}
}
59 changes: 59 additions & 0 deletions tests/Fixture/Classes/WithMultipleParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Rougin\Slytherin\Fixture\Classes;

/**
* With Multiple Parameters
*
* @package Slytherin
* @author Rougin Royce Gutib <[email protected]>
*/
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';
}
}
26 changes: 26 additions & 0 deletions tests/Integration/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 46f7268

Please sign in to comment.