Skip to content

Commit

Permalink
Merging in refactored unit tests for Zend\Di
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Schindler committed Jun 8, 2011
1 parent f9a939a commit 270f61e
Show file tree
Hide file tree
Showing 55 changed files with 607 additions and 1,578 deletions.
21 changes: 19 additions & 2 deletions library/Zend/Di/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@

class Configuration
{
protected $definition = null;
protected $instanceManager = null;
protected $data = array();

public function __construct($data)
{
if ($data instanceof \Zend\Config\Config) {
$data = $data->toArray();
}
}

public function getDefinition()
{

}

public function getInstanceManager()
{

}

}
2 changes: 1 addition & 1 deletion library/Zend/Di/Definition/Builder/InjectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setName($name)
$this->name = $name;
}

public function getName($name)
public function getName()
{
return $this->name;
}
Expand Down
9 changes: 8 additions & 1 deletion library/Zend/Di/Definition/Builder/PhpClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@ public function setInstantiator($instantiator)
return $this;
}

public function getInstantiator()
{
return $this->instantiator;
}

public function addSuperType($superType)
{
$this->superTypes[] = $superType;
return $this;
}

public function getSuperTypes()
Expand All @@ -38,11 +44,12 @@ public function getSuperTypes()
public function addInjectionMethod(InjectionMethod $injectionMethod)
{
$this->injectionMethods[] = $injectionMethod;
return $this;
}

public function getInjectionMethods()
{
$this->injectionMethods;
return $this->injectionMethods;
}

}
3 changes: 2 additions & 1 deletion library/Zend/Di/Definition/BuilderDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ public function getInstantiator($class)

public function hasInjectionMethods($class)
{
/* @var $class Zend\Di\Definition\Builder\PhpClass */
$class = $this->getClass($class);
if ($class === false) {
throw new Exception\RuntimeException('Cannot find class object in this builder definition.');
}
return $class->getInstantiator();
return (count($class->getInjectionMethods()) > 0);
}

public function getInjectionMethods($class)
Expand Down
24 changes: 13 additions & 11 deletions library/Zend/Di/DependencyInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ class DependencyInjector implements DependencyInjection
*/
protected $references = array();

protected $circularDependencyCheck = true;

/**
* @param Zend\DI\Configuration $config
*/
public function __construct(Configuration $config = null)
{
if ($this->config) {
if ($config) {
$this->setConfiguration($config);
}
}
Expand All @@ -43,7 +45,7 @@ public function setConfiguration(Configuration $config)
// @todo process this
}

public function setDefinition(Definition\DefinitionInterface $definition)
public function setDefinition(Definition $definition)
{
$this->definition = $definition;
return $this;
Expand Down Expand Up @@ -112,13 +114,11 @@ public function get($name, array $params = array())
public function newInstance($name, array $params = array(), $isShared = true)
{
$this->getDefinition();

// check if name is alias
//$class = (array_key_exists($name, $this->aliases)) ? $this->aliases[$name] : $name;
$class = $name;

$class = $this->getInstanceManager()->getClassFromAlias($name);

if (!$this->definition->hasClass($class)) {
throw new Exception\InvalidArgumentException('Invalid class name or alias provided.');
throw new Exception\ClassNotFoundException('Class or alias name ' . $name . ' could not be located in provided definition.');
}

$instantiator = $this->definition->getInstantiator($class);
Expand Down Expand Up @@ -241,13 +241,13 @@ protected function resolveMethodParameters($class, $method, array $params, $isIn
$index = 0;
foreach ($this->definition->getInjectionMethodParameters($class, $method) as $name => $value) {
if ($value === null && !array_key_exists($name, $params)) {
throw new Exception\RuntimeException('Missing parameter named ' . $name . ' for ' . $class . '::' . $method);
throw new Exception\MissingPropertyException('Missing parameter named ' . $name . ' for ' . $class . '::' . $method);
}

// circular dep check
if ($isInstantiator && $value !== null) {
$this->dependencies[$class][$value]= true;
//$this->references[$serviceName][$className]= true;
$this->checkCircularDependency($class, $value);
}

if ($value === null) {
Expand All @@ -273,12 +273,12 @@ protected function checkCircularDependency($class, $dependency)
if (is_array($dependency)) {
foreach ($dependency as $dep) {
if (isset($this->dependencies[$dep][$class]) && $this->dependencies[$dep][$class]) {
throw new Exception\RuntimeException("Circular dependency detected: $class depends on $dep and viceversa");
throw new Exception\CircularDependencyException("Circular dependency detected: $class depends on $dep and viceversa");
}
}
} else {
if (isset($this->dependencies[$dependency][$class]) && $this->dependencies[$dependency][$class]) {
throw new Exception\RuntimeException("Circular dependency detected: $class depends on $dependency and viceversa");
throw new Exception\CircularDependencyException("Circular dependency detected: $class depends on $dependency and viceversa");
}
}
return true;
Expand All @@ -291,6 +291,7 @@ protected function checkCircularDependency($class, $dependency)
* @param type $dependency
* @return void
*/
/*
protected function checkPathDependencies($class, $dependency)
{
if (!empty($this->references[$class])) {
Expand All @@ -303,5 +304,6 @@ protected function checkPathDependencies($class, $dependency)
}
}
}
*/

}
9 changes: 9 additions & 0 deletions library/Zend/Di/Exception/CircularDependencyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Zend\Di\Exception;

use Zend\Di\Exception,
DomainException;

class CircularDependencyException extends DomainException implements Exception
{
}
9 changes: 9 additions & 0 deletions library/Zend/Di/Exception/MissingPropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Zend\Di\Exception;

use Zend\Di\Exception,
DomainException;

class MissingPropertyException extends DomainException implements Exception
{
}
11 changes: 6 additions & 5 deletions library/Zend/Di/InstanceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

interface InstanceCollection
{
public function hasSharedInstance($class, array $params = array());
public function getSharedInstance($class, array $params = array());
public function hasSharedInstance($classOrAlias, array $params = array());
public function getSharedInstance($classOrAlias, array $params = array());
public function addSharedInstance($object, $class, array $params = array());
public function getClassFromAlias($alias);
public function addAlias($class, $alias);
public function hasProperties($classOrAlias);
public function getProperties($classOrAlias);
public function getProperty($class, $name);
public function setProperty($class, $name, $value);
public function unsetProperty($class, $name);
public function hasProperty($classOrAlias, $name);
public function getProperty($classOrAlias, $name);
public function setProperty($classOrAlias, $name, $value);
public function unsetProperty($classOrAlias, $name);
}
13 changes: 13 additions & 0 deletions library/Zend/Di/InstanceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public function getProperties($classOrAlias)
return array();
}

/**
* (non-PHPdoc)
* @see Zend\Di.InstanceCollection::getProperty()
*/
public function hasProperty($classOrAlias, $name)
{
// @todo better alias property management
if (isset($this->properties[$classOrAlias])) {
return true;
}
return false;
}

/**
* (non-PHPdoc)
* @see Zend\Di.InstanceCollection::getProperty()
Expand Down
10 changes: 6 additions & 4 deletions tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,24 @@
if (defined('TESTS_GENERATE_REPORT') && TESTS_GENERATE_REPORT === true &&
version_compare(PHPUnit_Runner_Version::id(), '3.1.6', '>=')) {

$codeCoverageFilter = PHP_CodeCoverage_Filter::getInstance();

/*
* Add Zend Framework library/ directory to the PHPUnit code coverage
* whitelist. This has the effect that only production code source files
* appear in the code coverage report and that all production code source
* files, even those that are not covered by a test yet, are processed.
*/
PHPUnit_Util_Filter::addDirectoryToWhitelist($zfCoreLibrary);
//$codeCoverageFilter->addDirectoryToWhitelist($zfCoreLibrary);

/*
* Omit from code coverage reports the contents of the tests directory
*/
foreach (array('.php', '.phtml', '.csv', '.inc') as $suffix) {
PHPUnit_Util_Filter::addDirectoryToFilter($zfCoreTests, $suffix);
$codeCoverageFilter->addDirectoryToBlacklist($zfCoreTests, $suffix);
}
PHPUnit_Util_Filter::addDirectoryToFilter(PEAR_INSTALL_DIR);
PHPUnit_Util_Filter::addDirectoryToFilter(PHP_LIBDIR);
$codeCoverageFilter->addDirectoryToBlacklist(PEAR_INSTALL_DIR);
$codeCoverageFilter->addDirectoryToBlacklist(PHP_LIBDIR);
}


Expand Down
Loading

0 comments on commit 270f61e

Please sign in to comment.