[go: nahoru, domu]

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

Implement PSR-11 #13060

Merged
merged 6 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Apply requested changes and add more tests
Remove try-catch blocks
Refactor has method
Refactor testGetWithInvalidEntry method
Add tests for ContainerException and NotFoundException

Signed-off-by: Maurício Meneghini Fauth <mauriciofauth@gmail.com>
  • Loading branch information
MauricioFauth committed Mar 13, 2017
commit b0121f9f44da15ada510f2527715888e478705a2
36 changes: 9 additions & 27 deletions libraries/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,16 @@ public function __construct(Container $base = null)
*/
public function get($name, $params = array())
{
try {
if (!$this->has($name)) {
throw new NotFoundException("No entry was found for $name identifier.");
}
} catch (NotFoundException $e) {
echo $e->getMessage();
return null;
if (!$this->has($name)) {
throw new NotFoundException("No entry was found for $name identifier.");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't get the code here:

  • Why is there try { throw } catch {} block at all?
  • The echo is there for something else than debugging purposes?


try {
if (isset($this->content[$name])) {
return $this->content[$name]->get($params);
} else if (isset($GLOBALS[$name])) {
return $GLOBALS[$name];
} else {
throw new ContainerException("Error while retrieving the entry.");
}
} catch (ContainerException $e) {
echo $e->getMessage();
return null;
if (isset($this->content[$name])) {
return $this->content[$name]->get($params);
} else if (isset($GLOBALS[$name])) {
return $GLOBALS[$name];
} else {
throw new ContainerException("Error while retrieving the entry.");
}
}

Expand All @@ -90,15 +80,7 @@ public function get($name, $params = array())
*/
public function has($name)
{
if (isset($this->content[$name])) {
return true;
}

if (isset($GLOBALS[$name])) {
return true;
}

return false;
return isset($this->content[$name]) || isset($GLOBALS[$name]);
}

/**
Expand Down
77 changes: 77 additions & 0 deletions test/classes/di/ContainerExceptionTest.php
Original file line number Diff line number Diff line change
@@ -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
);
}
}
7 changes: 4 additions & 3 deletions test/classes/di/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public function testGetWithValidEntry()

/**
* Test for get
* @expectedExceptionMessageRegExp #Right.*#
*
* @return void
*/
public function testGetWithInvalidEntry()
public function testGetThrowsNotFoundException()
{
$this->assertSame(null, $this->container->get('invalid'));
$this->setExpectedException('Psr\Container\NotFoundExceptionInterface');
$this->container->get('name');
}

/**
Expand Down
90 changes: 90 additions & 0 deletions test/classes/di/NotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -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
);
}
}