Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tux-rampage committed Apr 5, 2016
1 parent 6394d77 commit b7e150e
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 84 deletions.
8 changes: 6 additions & 2 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ class Config implements ConfigInterface
protected $data = [];

/**
* Constructor
* Construct from option array
*
* @param array|Traversable $options
* Utilizes the given options array or traversable.
*
* @param array|Traversable $options The options array. Traversables
* will be converted to an array
* internally
* @throws Exception\InvalidArgumentException
*/
public function __construct($options)
Expand Down
6 changes: 2 additions & 4 deletions src/Definition/BuilderDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class BuilderDefinition implements DefinitionInterface
public function addClass(Builder\PhpClass $phpClass)
{
$this->classes[] = $phpClass;

return $this;
}

/**
* Create a class builder object using default class builder class
*
* This method is a factory that can be used in place of addClass().
* This method is a factory that can be used to provide the parameter
* for addClass().
*
* @param null|string $name Optional name of class to assign
* @return Builder\PhpClass
Expand All @@ -71,8 +71,6 @@ public function createClass($name = null)
$class->setName($name);
}

$this->addClass($class);

return $class;
}

Expand Down
78 changes: 0 additions & 78 deletions test/Resolver/DependencyResolver.php

This file was deleted.

230 changes: 230 additions & 0 deletions test/Resolver/DependencyResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Di\Resolver;

use Zend\Di\Resolver\DependencyResolver;

use Zend\Di\ConfigInterface;
use Zend\Di\Config;

use Zend\Di\Definition\DefinitionInterface;
use Zend\Di\Definition\BuilderDefinition;

use PHPUnit_Framework_TestCase as TestCase;


/**
* @coversDefaultClass Zend\Di\Resolver\DependencyResolver
*/
class DependencyResolverTest extends TestCase
{
/**
* @return array
*/
public function globalTypePreferenceTestDataProvider()
{
$definition = new BuilderDefinition();

$definition
->addClass(
$definition->createClass('Some\SpecificClass')
->addSuperType('SomeInterface')
)
->addClass($definition->createClass('NotSomeInterface'))
->addClass(
$definition->createClass('Some\OtherSpecificClass')
->addSuperType('SomeInterface')
);

return [
[
'SomeInterface',
'Some\SpecificClass',
$definition,
new Config([
'preferences' => [
'SomeInterface' => [ 'Some\SpecificClass' ]
],
])
],
[
'SomeInterface',
'Some\SpecificClass',
$definition,
new Config([
'preferences' => [
'SomeInterface' => [ 'NotSomeInterface', 'Some\SpecificClass' ]
],
])
],
[
'SomeInterface',
'my.alias',
$definition,
new Config([
'preferences' => [
'SomeInterface' => [ 'my.alias' ]
],
'instances' => [
'my.alias' => [
'aliasOf' => 'SomeInterface',
]
]
]),
],
];
}

/**
* @return array
*/
public function contextTypePreferenceTestDataProvider()
{
$definition = new BuilderDefinition();

$definition
->addClass(
$definition->createClass('Some\SpecificClass')
->addSuperType('SomeInterface')
)
->addClass($definition->createClass('NotSomeInterface'))
->addClass(
$definition->createClass('Some\OtherSpecificClass')
->addSuperType('SomeInterface')
);

return [
[
'SomeInterface',
'ContextClass',
'Some\SpecificClass',
$definition,
new Config([
'instances' => [
'ContextClass' =>[
'preferences' => [
'SomeInterface' => [ 'Some\SpecificClass' ]
],
]
]
])
],
[
'SomeInterface',
'ContextClassB',
'Some\SpecificClass',
$definition,
new Config([
'instances' => [
'ContextClassB' =>[
'preferences' => [
'SomeInterface' => [ 'NotSomeInterface', 'Some\SpecificClass' ]
],
]
]
])
],
[
'SomeInterface',
'ContextClassC',
'my.alias',
$definition,
new Config([
'instances' => [
'my.alias' => [
'aliasOf' => 'SomeInterface',
],
'ContextClassC' => [
'preferences' => [
'SomeInterface' => [ 'my.alias' ]
],
]
]
]),
],
[
'SomeInterface',
'ContextClassE',
'Some\OtherSpecificClass',
$definition,
new Config([
'preferences' => [
'SomeInterface' => [ 'Some\SpecificClass' ]
],
'instances' => [
'ContextClassE' => [
'preferences' => [
'SomeInterface' => [ 'Some\OtherSpecificClass' ]
],
]
]
]),
],
[
'SomeInterface',
'ContextClassE',
'Some\SpecificClass',
$definition,
new Config([
'preferences' => [
'SomeInterface' => [ 'Some\SpecificClass' ]
],
'instances' => [
'ContextClassE' => [
'preferences' => [
'SomeInterface' => [ 'NotSomeInterface' ]
],
]
]
]),

],
];
}

/**
* Tests the resolve global type preference code
*
* @dataProvider globalTypePreferenceTestDataProvider
* @covers ::resolvePreference
* @uses Zend\Di\Defintion\BuilderDefinition
* @uses Zend\Di\Config
*
* @param string $type The type to resolve for
* @param string $expected The expected preference
* @param DefinitionInterface $definition The definition to utilize
* @param ConfigInterface $config The config to utilize
*/
public function testResolveConfiguredGlobalPreference($type, $expected, DefinitionInterface $definition, ConfigInterface $config)
{
$resolver = new DependencyResolver($definition, $config);
$this->assertEquals($expected, $resolver->resolvePreference($type));
}

/**
* Tests the resolve context based type preference code
*
* @dataProvider contextTypePreferenceTestDataProvider
* @covers ::resolvePreference
* @uses Zend\Di\Defintion\BuilderDefinition
* @uses Zend\Di\Config
*
* @param string $type The type to resolve for
* @param string $contextType The context type name
* @param string $expected The expected preference
* @param DefinitionInterface $definition The definition to utilize
* @param ConfigInterface $config The config to utilize
*/
public function testResolveConfiguredPreferenceForContextType($type, $contextType, $expected, DefinitionInterface $definition, ConfigInterface $config)
{
$resolver = new DependencyResolver($definition, $config);
$this->assertEquals($expected, $resolver->resolvePreference($type, $contextType));
}
}

0 comments on commit b7e150e

Please sign in to comment.