Skip to content

Commit ae1bae9

Browse files
committed
Updates ContainerInterop to use PSR-11 (breaks BC)
This is a breaking change from the previous version, as the [ContainerInterop][1] interfaces are no longer implemented. As of their recommendation, but not because of it 😉, we are now conforming the [PSR-11][2] interfaces. Not all interfaces were implemented (👀 Exceptions), so start using them - conforming with previous exceptions thrown by Respect\Config. Change 95ab990 modified an `\InvalidArgumentException` to an `Exception` type, this reverts that change to conform with previous contracts. Rather than testing interoperability inside `ContainerTest` test case, extract those tests into their own test case under a more friendly namespace (`Test\Feature`) and better test names - along with the test for the exception bullshit. Technical debts --------------- The test suite now holds two different organizations, one (introduced here) unfinished. I've already dived into this code base a few times and tests helped me a lot, but having them matching their concrete classes might not be the best organization for that. As splitting this suite into groups like "unit" and "integration" may not as useful as it is inside a huge application (with dozens of dependencies), we can use something like: "feature", "issues" and "library". [1]: https://github.com/container-interop/container-interop [2]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md
1 parent 4eaa861 commit ae1bae9

File tree

7 files changed

+129
-36
lines changed

7 files changed

+129
-36
lines changed

Diff for: composer.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "library",
44
"homepage": "https://github.com/Respect/Config",
55
"description": "A powerful, small, deadly simple configurator and dependency injection container made to be easy.",
6-
"keywords": ["respect", "config", "dic", "dependency injection"],
6+
"keywords": ["respect", "config", "dic", "dependency injection", "psr-11", "container"],
77
"license": "BSD-3-Clause",
88
"authors": [
99
{
@@ -18,7 +18,7 @@
1818
},
1919
"require": {
2020
"php": ">=5.3.0",
21-
"container-interop/container-interop": "^1.1"
21+
"psr/container": "^1.0"
2222
},
2323
"require-dev": {
2424
"phpunit/phpunit": "~4.4",

Diff for: library/Respect/Config/Container.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Respect\Config;
44

5-
use InvalidArgumentException as Argument;
5+
use Respect\Config\InvalidArgumentException as Argument;
66
use ArrayObject;
77
use ReflectionClass;
88
use ReflectionFunction;
9-
use Interop\Container\ContainerInterface;
9+
use Psr\Container\ContainerInterface;
1010

1111
class Container extends ArrayObject implements ContainerInterface
1212
{

Diff for: library/Respect/Config/InvalidArgumentException.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Respect\Config;
4+
5+
use Psr\Container as Interop;
6+
7+
class InvalidArgumentException extends \InvalidArgumentException implements Interop\ContainerExceptionInterface
8+
{
9+
}
10+

Diff for: library/Respect/Config/NotFoundException.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Respect\Config;
44

5-
use Interop\Container\Exception\NotFoundException as BaseNotFoundException;
5+
use Psr\Container as Interop;
66

7-
class NotFoundException extends \Exception implements BaseNotFoundException
7+
class NotFoundException extends InvalidArgumentException implements Interop\NotFoundExceptionInterface
88
{
9-
}
9+
}
10+

Diff for: phpunit.xml.dist

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
syntaxCheck="false"
1010
verbose="false">
1111
<testsuites>
12-
<testsuite>
13-
<directory suffix="Test.php">tests</directory>
12+
<testsuite name="library">
13+
<directory suffix="Test.php">tests/library</directory>
14+
</testsuite>
15+
<testsuite name="feature">
16+
<directory suffix=".php">tests/feature</directory>
1417
</testsuite>
1518
</testsuites>
1619
<filter>

Diff for: tests/feature/Psr11.php

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace Test\Feature;
4+
5+
use Respect\Config\Container;
6+
use Respect\Test\StreamWrapper;
7+
8+
class Psr11Test extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md
12+
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container-meta.md
13+
*/
14+
public function testContainerInteropMethodsAkaPsr11()
15+
{
16+
$ini = <<<INI
17+
foo = bar
18+
baz = bat
19+
INI;
20+
$c = new Container;
21+
$c->loadArray(parse_ini_string($ini, true));
22+
23+
$this->assertTrue(
24+
$c->has('foo'),
25+
'Searching an existing item must succeed.'
26+
);
27+
$this->assertEquals(
28+
'bar',
29+
$c->get('foo'),
30+
'Retrieving an existing item must succeed.'
31+
);
32+
$this->assertEquals(
33+
'bat',
34+
$c->get('baz'),
35+
'Retrieving an existing item must succeed.'
36+
);
37+
}
38+
39+
/**
40+
* @expectedException Psr\Container\NotFoundExceptionInterface
41+
* @expectedExceptionMessage Item baz not found
42+
*/
43+
public function testItemNotFoundInContainerMatchesInteropException()
44+
{
45+
$this->loadInvalidItem();
46+
}
47+
48+
/**
49+
* @expectedException \InvalidArgumentException
50+
* @expectedExceptionMessage Item baz not found
51+
*/
52+
public function testItemNotFoundInContainerMatchesPreviousRespectConfigException()
53+
{
54+
$this->loadInvalidItem();
55+
}
56+
57+
/**
58+
* @expectedException Respect\Config\InvalidArgumentException
59+
* @expectedExceptionMessage Item baz not found
60+
*/
61+
public function testItemNotFoundInContainerMatchesCustomRespectException()
62+
{
63+
$this->loadInvalidItem();
64+
}
65+
66+
private function loadInvalidItem()
67+
{
68+
$ini = <<<INI
69+
foo = bar
70+
INI;
71+
$c = new Container;
72+
$c->loadArray(parse_ini_string($ini, true));
73+
$c->get('baz');
74+
}
75+
76+
/**
77+
* @expectedException Psr\Container\ContainerExceptionInterface
78+
*/
79+
public function testContainerExceptionMatchesInteropException()
80+
{
81+
$this->parseInvalidIni();
82+
}
83+
84+
/**
85+
* @expectedException \InvalidArgumentException
86+
*/
87+
public function testContainerExceptionMatchesPreviousRespectConfigException()
88+
{
89+
$this->parseInvalidIni();
90+
}
91+
92+
/**
93+
* @expectedException Respect\Config\InvalidArgumentException
94+
*/
95+
public function testContainerExceptionUsesCustomRespectException()
96+
{
97+
$this->parseInvalidIni();
98+
}
99+
100+
private function parseInvalidIni()
101+
{
102+
$c = new Container(14);
103+
$c->foo;
104+
}
105+
}
106+

Diff for: tests/library/Respect/Config/ContainerTest.php

-27
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,6 @@ public function testLoadFile()
4848
$this->assertEquals('bar', $c->getItem('foo'));
4949
$this->assertEquals('bat', $c->getItem('baz'));
5050
}
51-
52-
public function testContainerInterop()
53-
{
54-
$ini = <<<INI
55-
foo = bar
56-
baz = bat
57-
INI;
58-
$c = new Container;
59-
$c->loadArray(parse_ini_string($ini, true));
60-
$this->assertTrue($c->has('foo'));
61-
$this->assertEquals('bar', $c->get('foo'));
62-
$this->assertEquals('bat', $c->get('baz'));
63-
}
64-
65-
/**
66-
* @expectedException Interop\Container\Exception\NotFoundException
67-
* @expectedExceptionMessage Item baz not found
68-
*/
69-
public function testLoadInvalidName()
70-
{
71-
$ini = <<<INI
72-
foo = bar
73-
INI;
74-
$c = new Container;
75-
$c->loadArray(parse_ini_string($ini, true));
76-
$c->get('baz');
77-
}
7851

7952
/**
8053
* @expectedException InvalidArgumentException

0 commit comments

Comments
 (0)