Skip to content

Commit

Permalink
Introducing simple tests for the reflection class builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Ocramius committed Jul 20, 2013
1 parent 8867e1a commit b1372a8
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/CodeGenerationUtils/ReflectionBuilder/ClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PHPParser_Node_Const;
use PHPParser_Node_Expr_ConstFetch;
use PHPParser_Node_Name;
use PHPParser_Node_Name_FullyQualified;
use PHPParser_Node_Stmt_Class;
use PHPParser_Node_Stmt_ClassConst;
use PHPParser_Node_Stmt_Namespace;
Expand Down Expand Up @@ -54,6 +55,18 @@ public function fromReflection(ReflectionClass $reflectionClass)
$class = new PHPParser_Node_Stmt_Class($reflectionClass->getShortName());
$stmts = array($class);

if ($parentClass = $reflectionClass->getParentClass()) {
$class->extends = new PHPParser_Node_Name_FullyQualified($parentClass->getName());
}

$interfaces = array();

foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
$interfaces[] = new PHPParser_Node_Name_FullyQualified($reflectionInterface->getName());
}

$class->implements = $interfaces;

foreach ($reflectionClass->getConstants() as $constant => $value) {
$class->stmts[] = new PHPParser_Node_Stmt_ClassConst(
array(new PHPParser_Node_Const($constant, $this->normalizeValue($value)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtilsTest\Visitor;

use CodeGenerationUtils\ReflectionBuilder\ClassBuilder;
use PHPParser_Node_Stmt_ClassMethod;
use PHPUnit_Framework_TestCase;
use ReflectionClass;

/**
* Tests for {@see \CodeGenerationUtils\ReflectionBuilder\ClassBuilder}
*
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*
* @covers \CodeGenerationUtils\ReflectionBuilder\ClassBuilder
*/
class ClassBuilderTest extends PHPUnit_Framework_TestCase
{
/**
* Simple test reflecting this test class
*/
public function testBuildSelf()
{
$classBuilder = new ClassBuilder();
$ast = $classBuilder->fromReflection(new ReflectionClass(__CLASS__));
/* @var $namespace \PHPParser_Node_Stmt_Namespace */
$namespace = $ast[0];

$this->assertInstanceOf('PHPParser_Node_Stmt_Namespace', $namespace);
$this->assertSame(__NAMESPACE__, $namespace->name->toString());

/* @var $class \PHPParser_Node_Stmt_Class */
$class = $namespace->stmts[0];

$this->assertInstanceOf('PHPParser_Node_Stmt_Class', $class);
$this->assertSame('ClassBuilderTest', $class->name);

$currentMethod = __FUNCTION__;
/* @var $methods PHPParser_Node_Stmt_ClassMethod[] */
$methods = array_filter(
$class->stmts,
function ($node) use ($currentMethod) {
return $node instanceof PHPParser_Node_Stmt_ClassMethod && $node->name === $currentMethod;
}
);

$this->assertCount(1, $methods);

/* @var $thisMethod PHPParser_Node_Stmt_ClassMethod */
$thisMethod = reset($methods);

$this->assertSame($currentMethod, $thisMethod->name);
}
}

0 comments on commit b1372a8

Please sign in to comment.