Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

House keeping #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "library",
"homepage": "https://github.com/Respect/Config",
"description": "A powerful, small, deadly simple configurator and dependency injection container made to be easy.",
"keywords": ["respect", "config", "dic", "dependency injection"],
"keywords": ["respect", "config", "dic", "dependency injection", "psr-11", "container"],
"license": "BSD-3-Clause",
"authors": [
{
Expand All @@ -18,9 +18,11 @@
},
"require": {
"php": ">=5.3.0",
"container-interop/container-interop": "^1.1"
"psr/container": "^1.0"
},
"require-dev": {
"ext-PDO": "*",
"ext-pdo_sqlite": "*",
"phpunit/phpunit": "~4.4",
"respect/test": "dev-master"
},
Expand Down
29 changes: 14 additions & 15 deletions library/Respect/Config/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Respect\Config;

use InvalidArgumentException as Argument;
use Respect\Config\InvalidArgumentException as Argument;
use ArrayObject;
use ReflectionClass;
use ReflectionFunction;
use Interop\Container\ContainerInterface;
use Psr\Container\ContainerInterface;

class Container extends ArrayObject implements ContainerInterface
{
Expand All @@ -21,12 +21,10 @@ public function __isset($name)
{
return $this->has($name);
}
public function has($name)

public function has($name)
{
if ($this->configurator) {
$this->configure();
}
$this->configure();

return parent::offsetExists($name);
}
Expand Down Expand Up @@ -70,9 +68,7 @@ function ($param) use ($container) {
parent::offsetSet($name, $item);
}

if ($this->configurator) {
$this->configure();
}
$this->configure();

return $this;
}
Expand Down Expand Up @@ -107,12 +103,10 @@ protected function configure()

throw new Argument("Invalid input. Must be a valid file or array");
}

public function getItem($name, $raw = false)
{
if ($this->configurator) {
$this->configure();
}
$this->configure();

if (!isset($this[$name])) {
throw new NotFoundException("Item $name not found");
Expand All @@ -124,14 +118,15 @@ public function getItem($name, $raw = false)

return $this->lazyLoad($name);
}

public function get($name)
{
return $this->getItem($name);
}

public function loadString($configurator)
{
$configurator = preg_replace('/^[\s\t]+/', '', $configurator);
$iniData = parse_ini_string($configurator, true);
if (false === $iniData || count($iniData) == 0) {
throw new Argument("Invalid configuration string");
Expand Down Expand Up @@ -268,6 +263,10 @@ protected function hasCompleteBrackets($value)

protected function parseSingleValue($value)
{
if (is_object($value)) {
return $value;
}

$value = trim($value);
if ($this->hasCompleteBrackets($value)) {
return $this->parseBrackets($value);
Expand Down
10 changes: 10 additions & 0 deletions library/Respect/Config/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Respect\Config;

use Psr\Container as Interop;

class InvalidArgumentException extends \InvalidArgumentException implements Interop\ContainerExceptionInterface
{
}

7 changes: 4 additions & 3 deletions library/Respect/Config/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Respect\Config;

use Interop\Container\Exception\NotFoundException as BaseNotFoundException;
use Psr\Container as Interop;

class NotFoundException extends \Exception implements BaseNotFoundException
class NotFoundException extends InvalidArgumentException implements Interop\NotFoundExceptionInterface
{
}
}

7 changes: 5 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
syntaxCheck="false"
verbose="false">
<testsuites>
<testsuite>
<directory suffix="Test.php">tests</directory>
<testsuite name="library">
<directory suffix="Test.php">tests/library</directory>
</testsuite>
<testsuite name="feature">
<directory suffix=".php">tests/feature</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
98 changes: 98 additions & 0 deletions tests/feature/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace Test\Feature;

use Respect\Config\Container;

class ItemConsuptionViaCallback extends \PHPUnit_Framework_TestCase
{
public function testLateDefinitionOfVariableExpansionThroughContainerCallbackReturnsContainer()
{
$c = new Container(<<<INI
foo = [undef]
bar = [foo]
INI
);
$definition = array('undef' => 'Hello');
$result = $c($definition);

$this->assertEquals(
'Hello',
$result->bar,
'Calling the container as a function will append the array passed as content to it.' . PHP_EOL .
'It will return the itself, as well.'
);
$this->assertSame(
$result->bar,
$c->bar,
"But it doesn't matter on which instance of the container you call."
);
}

public function testLateDefinitionOfVariableExpansionThroughItemCallbackReturnsValue()
{
$c = new Container(<<<INI
foo = [undef]
bar = [foo]
INI
);

$result = $c->bar(array('undef'=>'Hello'));
$this->assertEquals('Hello', $result);
}

public function testRetrievalOfItemThroughInstanceTypeOnContainerCallbackReturnsValue()
{
$called = false;
$c = new Container(<<<INI
[instanceof DateTime]
time = now
INI
);
$result = $c(function(\DateTime $date) use (&$called) {
$called = true;
return $date;
});

$result2 = $c['DateTime'];
$this->assertInstanceOf('DateTime', $result);
$this->assertInstanceOf('DateTime', $result2);
$this->assertTrue($called);
}

public function testRetrievalOfInstanceTypeThroughContainerCallbackReturnsValueEvenWithoutDeclaringItsType()
{
$c = new Container();
$c(new \DateTime);
$called = false;

$result = $c(function(\DateTime $date) use (&$called) {
$called = true;
return $date;
});

$result2 = $c['DateTime'];
$this->assertInstanceOf('DateTime', $result);
$this->assertInstanceOf('DateTime', $result2);
$this->assertTrue($called);
}

public function testContainerCallbackReceivingACallableCallsItAndReturnsValue()
{
$c = new Container();
$c(new \DateTime);
$result = $c(array('Test\Stub\TimePrinter', 'returnTimePassedAsArgument'));
$this->assertInstanceOf('DateTime', $result);
}
}

namespace Test\Stub;

class TimePrinter
{
public function returnTimePassedAsArgument(\DateTime $time)
{
return $time;
}
}

45 changes: 45 additions & 0 deletions tests/feature/ContainerAsArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Test\Feature;

use Respect\Config\Container;

class ContainerAsArray extends \PHPUnit_Framework_TestCase
{
/**
* @TODO Fix issue where passing INI to container construction does not
* allow ArrayAccess usage. T.T
*/
public function testUsingItemsAsContainerWasAnArray()
{
$c = new Container();
$c->loadString(<<<INI
fibonacci = [1, 1, 2, 3, 5]
INI
);

$this->assertInstanceOf(
'ArrayAccess',
$c,
'The container implements the \ArrayAccess interface, so it behaves like an array.'
);
$this->assertTrue(
isset($c['fibonacci'])
);
$this->assertEquals(
array(1, 1, 2, 3, 5),
$c['fibonacci'],
'The container implements the \ArrayAccess interface, so it behaves like an array.'
);
}

public function testDefiningItemWithArrayLikeNotation()
{
$c = new Container;

$c['not'] = false;
$this->assertTrue(isset($c['not']));
$this->assertFalse($c['not']);
}
}

92 changes: 92 additions & 0 deletions tests/feature/ContainerManipulation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Test\Feature;

use Respect\Config\Container;

class ContainerManipulation extends \PHPUnit_Framework_TestCase
{
public function testDefiningNewItemOnContainer()
{
$c = new Container;

$c->name = 'John Snow';

$this->assertEquals(
'John Snow',
$c->name,
'You can define new items just like you would define a property on an instance.'
);
}

public function testDefinitionOfItemWithAnonymousFunctionExecutesItAndReturnsItsValueUponUsage()
{
$c = new Container;

$c->name = function() { return 'John Doe'; };

$this->assertEquals(
'John Doe',
$c->name,
'The function gets executed and the return value is stored in the container.'
);
}

public function testDefinitionOfItemOnContainerWithItems()
{
$c = new Container(<<<INI
respect_blah = ""
INI
);

$c->panda = function() { return 'ok'; };

$this->assertEquals(
'ok',
$c->panda,
'It works if the container has stuff or not.'
);
}

/**
* @group issue
* @ticket 14
*/
public function testItemOverwrrite()
{
$c = new Container(<<<INI
[pdo StdClass]

[db Test\Stub\DatabaseWow]
con = [pdo];
INI
);

$c->pdo = new \PDO('sqlite::memory:');

$this->assertNotInstanceOf(
'StdClass',
$c->pdo,
'Although PDO was defined with StdClass we overwritten it to a proper instance of PDO manually.'
);

$this->assertSame(
$c->pdo,
$c->db->c,
'This should overwrite every usage of that element inside the container.'
);
}
}

namespace Test\Stub;

class DatabaseWow
{
public $c;

public function __construct($con)
{
$this->c = $con;
}
}

Loading