Skip to content

Commit 29a773e

Browse files
MauricioFauthnijel
authored andcommitted
Implement PSR-11
Implement PSR-11 to container interface with tests. Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
1 parent bd43164 commit 29a773e

8 files changed

Lines changed: 342 additions & 13 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
"phpmyadmin/motranslator": "^3.0",
3737
"phpmyadmin/shapefile": "^2.0",
3838
"phpseclib/phpseclib": "^2.0",
39-
"google/recaptcha": "^1.1"
39+
"google/recaptcha": "^1.1",
40+
"psr/container": "^1.0"
4041
},
4142
"conflict": {
4243
"tecnickcom/tcpdf": "<6.2"

libraries/di/Container.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
*/
88
namespace PMA\libraries\di;
99

10+
use Psr\Container\ContainerInterface;
11+
1012
/**
1113
* Class Container
1214
*
1315
* @package PMA\libraries\di
1416
*/
15-
class Container
17+
class Container implements ContainerInterface
1618
{
17-
1819
/**
1920
* @var Item[] $content
2021
*/
@@ -46,19 +47,40 @@ public function __construct(Container $base = null)
4647
* @param string $name Name
4748
* @param array $params Parameters
4849
*
50+
* @throws NotFoundException No entry was found for **this** identifier.
51+
* @throws ContainerException Error while retrieving the entry.
52+
*
4953
* @return mixed
5054
*/
5155
public function get($name, $params = array())
5256
{
53-
if (isset($this->content[$name])) {
54-
return $this->content[$name]->get($params);
57+
if (!$this->has($name)) {
58+
throw new NotFoundException("No entry was found for $name identifier.");
5559
}
5660

57-
if (isset($GLOBALS[$name])) {
61+
if (isset($this->content[$name])) {
62+
return $this->content[$name]->get($params);
63+
} else if (isset($GLOBALS[$name])) {
5864
return $GLOBALS[$name];
65+
} else {
66+
throw new ContainerException("Error while retrieving the entry.");
5967
}
68+
}
6069

61-
return null;
70+
/**
71+
* Returns true if the container can return an entry for the given identifier.
72+
* Returns false otherwise.
73+
*
74+
* `has($name)` returning true does not mean that `get($name)` will not throw an exception.
75+
* It does however mean that `get($name)` will not throw a `NotFoundException`.
76+
*
77+
* @param string $name Identifier of the entry to look for.
78+
*
79+
* @return bool
80+
*/
81+
public function has($name)
82+
{
83+
return isset($this->content[$name]) || isset($GLOBALS[$name]);
6284
}
6385

6486
/**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/* vim: set expandtab sw=4 ts=4 sts=4: */
3+
/**
4+
* Holds the PMA\libraries\di\ContainerException class
5+
*
6+
* @package PMA
7+
*/
8+
namespace PMA\libraries\di;
9+
10+
use Exception;
11+
use Psr\Container\ContainerExceptionInterface;
12+
13+
/**
14+
* Class ContainerException
15+
*
16+
* @package PMA\libraries\di
17+
*/
18+
class ContainerException extends Exception implements ContainerExceptionInterface
19+
{
20+
21+
}

libraries/di/NotFoundException.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/* vim: set expandtab sw=4 ts=4 sts=4: */
3+
/**
4+
* Holds the PMA\libraries\di\NotFoundException class
5+
*
6+
* @package PMA
7+
*/
8+
namespace PMA\libraries\di;
9+
10+
use Psr\Container\NotFoundExceptionInterface;
11+
12+
/**
13+
* Class NotFoundException
14+
*
15+
* @package PMA\libraries\di
16+
*/
17+
class NotFoundException extends ContainerException implements NotFoundExceptionInterface
18+
{
19+
20+
}

libraries/di/ReflectorItem.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,16 @@ private function _resolveArgs($required, $params = array())
8888
} elseif (is_string($type) && isset($params[$type])) {
8989
$args[] = $params[$type];
9090
} else {
91-
$content = $this->_container->get($name);
92-
if (isset($content)) {
93-
$args[] = $content;
94-
} elseif (is_string($type)) {
95-
$args[] = $this->_container->get($type);
96-
} else {
91+
try {
92+
$content = $this->_container->get($name);
93+
if (isset($content)) {
94+
$args[] = $content;
95+
} elseif (is_string($type)) {
96+
$args[] = $this->_container->get($type);
97+
} else {
98+
$args[] = null;
99+
}
100+
} catch (NotFoundException $e) {
97101
$args[] = null;
98102
}
99103
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/* vim: set expandtab sw=4 ts=4 sts=4: */
3+
/**
4+
* Tests for PMA\libraries\di\ContainerException class
5+
*
6+
* @package PhpMyAdmin-test
7+
*/
8+
9+
/*
10+
* Include to test.
11+
*/
12+
require_once 'test/PMATestCase.php';
13+
14+
use PMA\libraries\di\ContainerException;
15+
16+
/**
17+
* Tests for PMA\libraries\di\ContainerException class
18+
*
19+
* @package PhpMyAdmin-test
20+
*/
21+
class ContainerExceptionTest extends PMATestCase
22+
{
23+
/**
24+
* @access protected
25+
*/
26+
protected $exception;
27+
28+
/**
29+
* Sets up the fixture.
30+
* This method is called before a test is executed.
31+
*
32+
* @access protected
33+
* @return void
34+
*/
35+
protected function setUp()
36+
{
37+
$this->exception = new ContainerException();
38+
}
39+
40+
/**
41+
* Tears down the fixture.
42+
* This method is called after a test is executed.
43+
*
44+
* @access protected
45+
* @return void
46+
*/
47+
protected function tearDown()
48+
{
49+
unset($this->exception);
50+
}
51+
52+
/**
53+
* Test for ContainerException
54+
*
55+
* @return void
56+
*/
57+
public function testContainerExceptionImplementsInteface()
58+
{
59+
$this->assertInstanceOf(
60+
'Psr\Container\ContainerExceptionInterface',
61+
$this->exception
62+
);
63+
}
64+
65+
/**
66+
* Test for ContainerException
67+
*
68+
* @return void
69+
*/
70+
public function testContainerExceptionExtendsException()
71+
{
72+
$this->assertInstanceOf(
73+
'Exception',
74+
$this->exception
75+
);
76+
}
77+
}

test/classes/di/ContainerTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/* vim: set expandtab sw=4 ts=4 sts=4: */
3+
/**
4+
* Tests for PMA\libraries\di\Container class
5+
*
6+
* @package PhpMyAdmin-test
7+
*/
8+
9+
/*
10+
* Include to test.
11+
*/
12+
require_once 'test/PMATestCase.php';
13+
14+
use PMA\libraries\di\Container;
15+
16+
/**
17+
* Tests for PMA\libraries\di\Container class
18+
*
19+
* @package PhpMyAdmin-test
20+
*/
21+
class ContainerTest extends PMATestCase
22+
{
23+
/**
24+
* @access protected
25+
*/
26+
protected $container;
27+
28+
/**
29+
* Sets up the fixture.
30+
* This method is called before a test is executed.
31+
*
32+
* @access protected
33+
* @return void
34+
*/
35+
protected function setUp()
36+
{
37+
$this->container = new Container();
38+
}
39+
40+
/**
41+
* Tears down the fixture.
42+
* This method is called after a test is executed.
43+
*
44+
* @access protected
45+
* @return void
46+
*/
47+
protected function tearDown()
48+
{
49+
unset($this->container);
50+
}
51+
52+
/**
53+
* Test for get
54+
*
55+
* @return void
56+
*/
57+
public function testGetWithValidEntry()
58+
{
59+
$this->container->set('name', 'value');
60+
$this->assertSame('value', $this->container->get('name'));
61+
}
62+
63+
/**
64+
* Test for get
65+
*
66+
* @return void
67+
*/
68+
public function testGetThrowsNotFoundException()
69+
{
70+
$this->setExpectedException('Psr\Container\NotFoundExceptionInterface');
71+
$this->container->get('name');
72+
}
73+
74+
/**
75+
* Test for has
76+
*
77+
* @return void
78+
*/
79+
public function testHasReturnsTrueForValidEntry()
80+
{
81+
$this->container->set('name', 'value');
82+
$this->assertTrue($this->container->has('name'));
83+
}
84+
85+
/**
86+
* Test for has
87+
*
88+
* @return void
89+
*/
90+
public function testHasReturnsFalseForInvalidEntry()
91+
{
92+
$this->assertFalse($this->container->has('name'));
93+
}
94+
}

0 commit comments

Comments
 (0)