From 11936cd6460d6cc11f45232527a89c2090d967fb Mon Sep 17 00:00:00 2001 From: Cody Phillips Date: Sun, 8 Feb 2015 13:19:58 -0800 Subject: [PATCH] Initial commit --- .gitignore | 4 ++ .travis.yml | 11 ++++ composer.json | 25 ++++++++ phpunit.xml.dist | 16 +++++ src/Container.php | 48 +++++++++++++++ src/ContainerAwareInterface.php | 23 ++++++++ src/ContainerInterface.php | 26 ++++++++ src/Exception/NotFoundException.php | 9 +++ tests/ContainerTest.php | 72 +++++++++++++++++++++++ tests/Exception/NotFoundExceptionTest.php | 13 ++++ 10 files changed, 247 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 composer.json create mode 100644 phpunit.xml.dist create mode 100644 src/Container.php create mode 100644 src/ContainerAwareInterface.php create mode 100644 src/ContainerInterface.php create mode 100644 src/Exception/NotFoundException.php create mode 100644 tests/ContainerTest.php create mode 100644 tests/Exception/NotFoundExceptionTest.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..00bed89 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +vendor/ +composer.lock +phpunit.xml +.DS_Store \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d673176 --- /dev/null +++ b/.travis.yml @@ -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 \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b49df24 --- /dev/null +++ b/composer.json @@ -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": "therealclphillips@gmail.com" + } + ], + "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"} + } +} \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..a91e0ef --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,16 @@ + + + + + tests/ + + + + + + src/ + + + diff --git a/src/Container.php b/src/Container.php new file mode 100644 index 0000000..ed61472 --- /dev/null +++ b/src/Container.php @@ -0,0 +1,48 @@ +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); + } +} diff --git a/src/ContainerAwareInterface.php b/src/ContainerAwareInterface.php new file mode 100644 index 0000000..0dcc91c --- /dev/null +++ b/src/ContainerAwareInterface.php @@ -0,0 +1,23 @@ +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')); + } +} diff --git a/tests/Exception/NotFoundExceptionTest.php b/tests/Exception/NotFoundExceptionTest.php new file mode 100644 index 0000000..d614c24 --- /dev/null +++ b/tests/Exception/NotFoundExceptionTest.php @@ -0,0 +1,13 @@ +assertInstanceOf('\Interop\Container\Exception\NotFoundException', new NotFoundException()); + } +}