1
0
mirror of https://github.com/phpmyadmin/phpmyadmin.git synced 2026-10-28 07:41:55 +03:00

Implement PSR-11

Implement PSR-11 to container interface with tests.

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
This commit is contained in:
Maurício Meneghini Fauth
2017-03-22 05:24:53 -03:00
committed by Michal Čihař
parent bd4316419a
commit 29a773ed67
8 changed files with 342 additions and 13 deletions

View File

@@ -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"

View File

@@ -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]);
}
/**

View File

@@ -0,0 +1,21 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\libraries\di\ContainerException class
*
* @package PMA
*/
namespace PMA\libraries\di;
use Exception;
use Psr\Container\ContainerExceptionInterface;
/**
* Class ContainerException
*
* @package PMA\libraries\di
*/
class ContainerException extends Exception implements ContainerExceptionInterface
{
}

View File

@@ -0,0 +1,20 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Holds the PMA\libraries\di\NotFoundException class
*
* @package PMA
*/
namespace PMA\libraries\di;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class NotFoundException
*
* @package PMA\libraries\di
*/
class NotFoundException extends ContainerException implements NotFoundExceptionInterface
{
}

View File

@@ -88,12 +88,16 @@ abstract class ReflectorItem implements Item
} elseif (is_string($type) && isset($params[$type])) {
$args[] = $params[$type];
} else {
$content = $this->_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;
}
}

View File

@@ -0,0 +1,77 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for PMA\libraries\di\ContainerException class
*
* @package PhpMyAdmin-test
*/
/*
* Include to test.
*/
require_once 'test/PMATestCase.php';
use PMA\libraries\di\ContainerException;
/**
* Tests for PMA\libraries\di\ContainerException class
*
* @package PhpMyAdmin-test
*/
class ContainerExceptionTest extends PMATestCase
{
/**
* @access protected
*/
protected $exception;
/**
* Sets up the fixture.
* This method is called before a test is executed.
*
* @access protected
* @return void
*/
protected function setUp()
{
$this->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
);
}
}

View File

@@ -0,0 +1,94 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for PMA\libraries\di\Container class
*
* @package PhpMyAdmin-test
*/
/*
* Include to test.
*/
require_once 'test/PMATestCase.php';
use PMA\libraries\di\Container;
/**
* Tests for PMA\libraries\di\Container class
*
* @package PhpMyAdmin-test
*/
class ContainerTest extends PMATestCase
{
/**
* @access protected
*/
protected $container;
/**
* Sets up the fixture.
* This method is called before a test is executed.
*
* @access protected
* @return void
*/
protected function setUp()
{
$this->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'));
}
}

View File

@@ -0,0 +1,90 @@
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* Tests for PMA\libraries\di\NotFoundException class
*
* @package PhpMyAdmin-test
*/
/*
* Include to test.
*/
require_once 'test/PMATestCase.php';
use PMA\libraries\di\NotFoundException;
/**
* Tests for PMA\libraries\di\NotFoundException class
*
* @package PhpMyAdmin-test
*/
class NotFoundExceptionTest extends PMATestCase
{
/**
* @access protected
*/
protected $exception;
/**
* Sets up the fixture.
* This method is called before a test is executed.
*
* @access protected
* @return void
*/
protected function setUp()
{
$this->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
);
}
}