From 29a773ed67f1bb0a88b68962ced61287ca04fa3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Wed, 22 Mar 2017 05:24:53 -0300 Subject: [PATCH] Implement PSR-11 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement PSR-11 to container interface with tests. Signed-off-by: MaurĂ­cio Meneghini Fauth --- composer.json | 3 +- libraries/di/Container.php | 34 ++++++-- libraries/di/ContainerException.php | 21 +++++ libraries/di/NotFoundException.php | 20 +++++ libraries/di/ReflectorItem.php | 16 ++-- test/classes/di/ContainerExceptionTest.php | 77 ++++++++++++++++++ test/classes/di/ContainerTest.php | 94 ++++++++++++++++++++++ test/classes/di/NotFoundExceptionTest.php | 90 +++++++++++++++++++++ 8 files changed, 342 insertions(+), 13 deletions(-) create mode 100644 libraries/di/ContainerException.php create mode 100644 libraries/di/NotFoundException.php create mode 100644 test/classes/di/ContainerExceptionTest.php create mode 100644 test/classes/di/ContainerTest.php create mode 100644 test/classes/di/NotFoundExceptionTest.php diff --git a/composer.json b/composer.json index a3e6fb8ab5..e9ba511acd 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ "phpmyadmin/motranslator": "^3.0", "phpmyadmin/shapefile": "^2.0", "phpseclib/phpseclib": "^2.0", - "google/recaptcha": "^1.1" + "google/recaptcha": "^1.1", + "psr/container": "^1.0" }, "conflict": { "tecnickcom/tcpdf": "<6.2" diff --git a/libraries/di/Container.php b/libraries/di/Container.php index cce98ad980..557a20c002 100644 --- a/libraries/di/Container.php +++ b/libraries/di/Container.php @@ -7,14 +7,15 @@ */ namespace PMA\libraries\di; +use Psr\Container\ContainerInterface; + /** * Class Container * * @package PMA\libraries\di */ -class Container +class Container implements ContainerInterface { - /** * @var Item[] $content */ @@ -46,19 +47,40 @@ class Container * @param string $name Name * @param array $params Parameters * + * @throws NotFoundException No entry was found for **this** identifier. + * @throws ContainerException Error while retrieving the entry. + * * @return mixed */ public function get($name, $params = array()) { + if (!$this->has($name)) { + throw new NotFoundException("No entry was found for $name identifier."); + } + if (isset($this->content[$name])) { return $this->content[$name]->get($params); - } - - if (isset($GLOBALS[$name])) { + } else if (isset($GLOBALS[$name])) { return $GLOBALS[$name]; + } else { + throw new ContainerException("Error while retrieving the entry."); } + } - return null; + /** + * Returns true if the container can return an entry for the given identifier. + * Returns false otherwise. + * + * `has($name)` returning true does not mean that `get($name)` will not throw an exception. + * It does however mean that `get($name)` will not throw a `NotFoundException`. + * + * @param string $name Identifier of the entry to look for. + * + * @return bool + */ + public function has($name) + { + return isset($this->content[$name]) || isset($GLOBALS[$name]); } /** diff --git a/libraries/di/ContainerException.php b/libraries/di/ContainerException.php new file mode 100644 index 0000000000..53fae18382 --- /dev/null +++ b/libraries/di/ContainerException.php @@ -0,0 +1,21 @@ +_container->get($name); - if (isset($content)) { - $args[] = $content; - } elseif (is_string($type)) { - $args[] = $this->_container->get($type); - } else { + try { + $content = $this->_container->get($name); + if (isset($content)) { + $args[] = $content; + } elseif (is_string($type)) { + $args[] = $this->_container->get($type); + } else { + $args[] = null; + } + } catch (NotFoundException $e) { $args[] = null; } } diff --git a/test/classes/di/ContainerExceptionTest.php b/test/classes/di/ContainerExceptionTest.php new file mode 100644 index 0000000000..84f4a00300 --- /dev/null +++ b/test/classes/di/ContainerExceptionTest.php @@ -0,0 +1,77 @@ +exception = new ContainerException(); + } + + /** + * Tears down the fixture. + * This method is called after a test is executed. + * + * @access protected + * @return void + */ + protected function tearDown() + { + unset($this->exception); + } + + /** + * Test for ContainerException + * + * @return void + */ + public function testContainerExceptionImplementsInteface() + { + $this->assertInstanceOf( + 'Psr\Container\ContainerExceptionInterface', + $this->exception + ); + } + + /** + * Test for ContainerException + * + * @return void + */ + public function testContainerExceptionExtendsException() + { + $this->assertInstanceOf( + 'Exception', + $this->exception + ); + } +} diff --git a/test/classes/di/ContainerTest.php b/test/classes/di/ContainerTest.php new file mode 100644 index 0000000000..2679469fd4 --- /dev/null +++ b/test/classes/di/ContainerTest.php @@ -0,0 +1,94 @@ +container = new Container(); + } + + /** + * Tears down the fixture. + * This method is called after a test is executed. + * + * @access protected + * @return void + */ + protected function tearDown() + { + unset($this->container); + } + + /** + * Test for get + * + * @return void + */ + public function testGetWithValidEntry() + { + $this->container->set('name', 'value'); + $this->assertSame('value', $this->container->get('name')); + } + + /** + * Test for get + * + * @return void + */ + public function testGetThrowsNotFoundException() + { + $this->setExpectedException('Psr\Container\NotFoundExceptionInterface'); + $this->container->get('name'); + } + + /** + * Test for has + * + * @return void + */ + public function testHasReturnsTrueForValidEntry() + { + $this->container->set('name', 'value'); + $this->assertTrue($this->container->has('name')); + } + + /** + * Test for has + * + * @return void + */ + public function testHasReturnsFalseForInvalidEntry() + { + $this->assertFalse($this->container->has('name')); + } +} diff --git a/test/classes/di/NotFoundExceptionTest.php b/test/classes/di/NotFoundExceptionTest.php new file mode 100644 index 0000000000..3fa1c8ed76 --- /dev/null +++ b/test/classes/di/NotFoundExceptionTest.php @@ -0,0 +1,90 @@ +exception = new NotFoundException(); + } + + /** + * Tears down the fixture. + * This method is called after a test is executed. + * + * @access protected + * @return void + */ + protected function tearDown() + { + unset($this->exception); + } + + /** + * Test for NotFoundException + * + * @return void + */ + public function testNotFoundExceptionImplementsInteface() + { + $this->assertInstanceOf( + 'Psr\Container\NotFoundExceptionInterface', + $this->exception + ); + } + + /** + * Test for NotFoundException + * + * @return void + */ + public function testNotFoundExceptionExtendsContainerExceptionInteface() + { + $this->assertInstanceOf( + 'Psr\Container\ContainerExceptionInterface', + $this->exception + ); + } + + /** + * Test for NotFoundException + * + * @return void + */ + public function testContainerExceptionExtendsException() + { + $this->assertInstanceOf( + 'Exception', + $this->exception + ); + } +}