Skip to content
Snippets Groups Projects
Commit ae793d19 authored by Thomas Flori's avatar Thomas Flori
Browse files

added code coverage and the first real test

parent 658d51da
No related branches found
No related tags found
No related merge requests found
Pipeline #184 passed with stage
/vendor/
/coverage/
composer.lock
image: "iras/php7-phalcon-test"
test:
artifacts:
path:
- coverage/
script:
- composer install
- vendor/bin/phpunit
- composer test-wcc
# Dependency Injector
A simple and lightweight dependency injector. You will need nothing more to tests your old legacy code.
Did you ever wondered how complicated it could be to write a dependency injector that works? In fact it is very easy
and you don't need a tons of classes laying around and learn a dozens of new fancy words.
It is just a storage of functions and values for a specific key. Store your function to create an instance and it get
executed the first time you need this instance.
Sounds to easy? What about dependencies? Check the examples. It is nothing more needed than this.
## What is a dependency
......
{
"name": "tflori/dependency-injector",
"type": "library",
"keywords": ["di", "dependency injection", "dependency injector", "dependency", "dependencies"],
"description": "Easy and lightweight dependency injector",
"licence": "MIT",
"authors": [
{
"name": "Thomas Flori",
"email": "thflori@gmail.com",
"role": "Developer"
}
],
"scripts": {
"test": "phpunit",
"test-wcc": "phpunit --coverage-html coverage"
},
"version": "1.0.0-alpha",
"require-dev": {
"phpunit/phpunit": "5.5.*",
......
<?php
class DependencyInjector {}
/**
* Class DependencyInjector
*
* This is a Dependency Injector. It gives the opportunity to define how to get a dependency from outside of the code
* that needs the dependency. This is especially helpful for testing proposes when you need to mock a dependency.
*
* You can never get an instance of this class. For usage you have only two static methods.
*
* Example usage:
* DependencyInjector::set('hello', function() { return 'hello'; });
* DependencyInjector::set('world', function() { return 'world'; });
* echo DependencyInjector:get('hello') . " " . DependencyInjector:.get('world') . "!\n";
*/
class DependencyInjector {
/**
* Get a previously defined dependency identified by $name.
*
* @param string $name
* @throws DependencyInjectorException
*/
public static function get($name) {
throw new DependencyInjectorException("Unknown dependency '" . $name . "'");
}
}
<?php
class DependencyInjectorException extends Exception {};
<?php
use PHPUnit\Framework\TestCase;
use DependencyInjector as DI;
class DependencyInjectorTest extends TestCase {
/**
* Test that the setup works - the class should exist
*/
public function testSetUp() {
$this->assertTrue(class_exists('DependencyInjector'));
}
/**
* Test that DI::get() throws a exception when the requested depency is unknown.
*
* @expectedException DependencyInjectorException
* @expectedExceptionMessage Unknown dependency 'unknown'
*/
public function testGet_throwsForUnknownDependencies() {
DI::get('unknown');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment