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

Commit

Permalink
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/Definition/ClassDefinition.php
Expand Up @@ -136,6 +136,9 @@ public function hasClass($class)
*/
public function getClassSupertypes($class)
{
if ($this->class !== $class) {
return array();
}
return $this->supertypes;
}

Expand All @@ -144,6 +147,9 @@ public function getClassSupertypes($class)
*/
public function getInstantiator($class)
{
if ($this->class !== $class) {
return null;
}
return $this->instantiator;
}

Expand All @@ -160,6 +166,9 @@ public function hasMethods($class)
*/
public function getMethods($class)
{
if ($this->class !== $class) {
return array();
}
return $this->methods;
}

Expand All @@ -168,6 +177,10 @@ public function getMethods($class)
*/
public function hasMethod($class, $method)
{
if ($this->class !== $class) {
return null;
}

if (is_array($this->methods)) {
return array_key_exists($method, $this->methods);
} else {
Expand All @@ -180,6 +193,9 @@ public function hasMethod($class, $method)
*/
public function hasMethodParameters($class, $method)
{
if ($this->class !== $class) {
return false;
}
return (array_key_exists($method, $this->methodParameters));
}

Expand All @@ -188,6 +204,10 @@ public function hasMethodParameters($class, $method)
*/
public function getMethodParameters($class, $method)
{
if ($this->class !== $class) {
return null;
}

if (array_key_exists($method, $this->methodParameters)) {
return $this->methodParameters[$method];
}
Expand Down
8 changes: 6 additions & 2 deletions src/DefinitionList.php
Expand Up @@ -144,9 +144,13 @@ public function getClassSupertypes($class)
$supertypes = array();
/** @var $definition Definition\DefinitionInterface */
foreach ($this as $definition) {
$supertypes = array_merge($supertypes, $definition->getClassSupertypes($class));
if ($definition->hasClass($class)) {
$supertypes = array_merge($supertypes, $definition->getClassSupertypes($class));
if (!$definition instanceof Definition\PartialMarker) {
return $supertypes;
}
}
}
// @todo remove duplicates?
return $supertypes;
}

Expand Down
48 changes: 48 additions & 0 deletions test/Definition/ClassDefinitionTest.php
Expand Up @@ -27,4 +27,52 @@ public function testClassDefinitionHasMethods()
$definition->addMethod('doBar');
$this->assertTrue($definition->hasMethods('Foo'));
}

public function testGetClassSupertypes()
{
$definition = new ClassDefinition('Foo');
$definition->setSupertypes(array('superFoo'));
$this->assertEquals(array(), $definition->getClassSupertypes('Bar'));
$this->assertEquals(array('superFoo'), $definition->getClassSupertypes('Foo'));
}

public function testGetInstantiator()
{
$definition = new ClassDefinition('Foo');
$definition->setInstantiator('__construct');
$this->assertNull($definition->getInstantiator('Bar'));
$this->assertEquals('__construct', $definition->getInstantiator('Foo'));
}

public function testGetMethods()
{
$definition = new ClassDefinition('Foo');
$definition->addMethod("setVar", true);
$this->assertEquals(array(), $definition->getMethods('Bar'));
$this->assertEquals(array('setVar' => true), $definition->getMethods('Foo'));
}

public function testHasMethod()
{
$definition = new ClassDefinition('Foo');
$definition->addMethod("setVar", true);
$this->assertNull($definition->hasMethod('Bar', "setVar"));
$this->assertTrue($definition->hasMethod('Foo', "setVar"));
}

public function testHasMethodParameters()
{
$definition = new ClassDefinition('Foo');
$definition->addMethodParameter("setVar", "var", array(null, true));
$this->assertFalse($definition->hasMethodParameters("Bar", "setVar"));
$this->assertTrue($definition->hasMethodParameters("Foo", "setVar"));
}

public function testGetMethodParameters()
{
$definition = new ClassDefinition('Foo');
$definition->addMethodParameter("setVar", "var", array('type' => null, 'required' => true));
$this->assertNull($definition->getMethodParameters("Bar", "setVar"));
$this->assertEquals(array('Foo::setVar:var' => array("var", null, true)), $definition->getMethodParameters("Foo", "setVar"));
}
}
35 changes: 35 additions & 0 deletions test/DefinitionListTest.php
@@ -0,0 +1,35 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Di
*/

namespace ZendTest\Di;

use Zend\Di\DefinitionList;
use Zend\Di\Definition\ClassDefinition;
use Zend\Di\Definition\RuntimeDefinition;

use PHPUnit_Framework_TestCase as TestCase;

class DefinitionListTest extends TestCase
{
public function testGetClassSupertypes()
{
$definitionClassA = new ClassDefinition("A");
$superTypesA = array("superA");
$definitionClassA->setSupertypes($superTypesA);

$definitionClassB = new ClassDefinition("B");
$definitionClassB->setSupertypes(array("superB"));

$definitionList = new DefinitionList(array($definitionClassA, $definitionClassB));

$this->assertEquals($superTypesA, $definitionList->getClassSupertypes("A"));

}
}

0 comments on commit 9fa9257

Please sign in to comment.