Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
clphillips committed Feb 8, 2015
0 parents commit 11936cd
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
composer.lock
phpunit.xml
.DS_Store
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
before_script:
- composer install --dev
script:
- ./vendor/bin/phpunit --coverage-text
- ./vendor/bin/phpcs --extensions=php --report=summary --standard=PSR2 ./src ./tests
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "minphp/container",
"description": "A Dependency Injection Container based on Pimple",
"homepage": "http://github.com/phillipsdata/minphp-container",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Cody Phillips",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"container-interop/container-interop": "~1.0",
"pimple/pimple": "~3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~2.2"
},
"autoload": {
"psr-4": {"minphp\\Container\\": "src"}
}
}
16 changes: 16 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./vendor/autoload.php"
backupGlobals="false">
<testsuites>
<testsuite name="minphp/Container">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
48 changes: 48 additions & 0 deletions src/Container.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace minphp\Container;

use Pimple\Container as PimpleContainer;
use minphp\Container\Exception\NotFoundException;

/**
* A Container that extends Pimple using a standards compliant interface
*
*/
class Container extends PimpleContainer implements ContainerInterface
{
/**
* {@inheritdoc}
*/
public function get($id)
{
if (!$this->offsetExists($id)) {
throw new NotFoundException(sprintf('Identifier "%s" is not defined.', $id));
}

return $this->offsetGet($id);
}

/**
* {@inheritdoc}
*/
public function has($id)
{
return $this->offsetExists($id);
}

/**
* {@inheritdoc}
*/
public function set($id, $value)
{
$this->offsetSet($id, $value);
}

/**
* {@inheritdoc}
*/
public function remove($id)
{
$this->offsetUnset($id);
}
}
23 changes: 23 additions & 0 deletions src/ContainerAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace minphp\Container;

/**
* An interface allowing injection of a container
*
*/
interface ContainerAwareInterface
{
/**
* Adds the container
*
* @param minphp\Container\ContainerInterface $container
*/
public function setContainer(ContainerInterface $container = null);

/**
* Fetches a container
*
* @return minphp\Container\ContainerInterface
*/
public function getContainer();
}
26 changes: 26 additions & 0 deletions src/ContainerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace minphp\Container;

use Interop\Container\ContainerInterface as InteropContainerInterface;

/**
* Container interface that expands on the Interop\Container\ContainerInterface
*
*/
interface ContainerInterface extends InteropContainerInterface
{
/**
* Adds an entry to the container
*
* @param string $id Identifier of the entry to add
* @param mixed $value The entry to add to the container
*/
public function set($id, $value);

/**
* Removes an entry from the container
*
* @param string $id Identifier of the entry to remove
*/
public function remove($id);
}
9 changes: 9 additions & 0 deletions src/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace minphp\Container\Exception;

use InvalidArgumentException;
use Interop\Container\Exception\NotFoundException as NotFoundExceptionInterface;

class NotFoundException extends InvalidArgumentException implements NotFoundExceptionInterface
{
}
72 changes: 72 additions & 0 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace minphp\Container;

/**
* @coversDefaultClass \minphp\Container\Container
*/
class ContainerTest extends \PHPUnit_Framework_TestCase
{
private $Container;

public function setUp()
{
$this->Container = new Container();
}

/**
* @covers ::get
*/
public function testGet()
{
$this->Container->set('id', 'value');
$this->assertEquals('value', $this->Container->get('id'));
}

/**
* @covers ::get
* @expectedException \minphp\Container\Exception\NotFoundException
*/
public function testGetException()
{
$this->assertEquals('value', $this->Container->get('non-existent-id'));
}

/**
* @covers ::has
*/
public function testHas()
{
$this->Container->set('id', 'value');
$this->assertTrue($this->Container->has('id'));
$this->assertFalse($this->Container->has('non-existent-id'));
}

/**
* @covers ::set
*/
public function testSet()
{
$this->Container->set(
'queue',
function($c) {
return new \SplQueue();
}
);
$queue = $this->Container->get('queue');
$queue->enqueue('hello');

$this->assertEquals('hello', $this->Container->get('queue')->dequeue());
}

/**
* @covers ::remove
*/
public function testRemove()
{
$this->Container->set('id', 'value');
$this->assertTrue($this->Container->has('id'));

$this->Container->remove('id');
$this->assertFalse($this->Container->has('id'));
}
}
13 changes: 13 additions & 0 deletions tests/Exception/NotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace minphp\Container\Exception;

/**
* @coversDefaultClass \minphp\Container\Exception\NotFoundException
*/
class NotFoundExceptionTest extends \PHPUnit_Framework_TestCase
{
public function testInstance()
{
$this->assertInstanceOf('\Interop\Container\Exception\NotFoundException', new NotFoundException());
}
}

0 comments on commit 11936cd

Please sign in to comment.