<?php
require_once 'ComplexityAnalyzer.php';
require_once 'DummyClass.php';
require_once 'DummyClassTest.php';
/**
* Test class for ComplexityAnalyzer.
* Generated by PHPUnit on 2011-12-30 at 16:16:39.
*/
class ComplexityAnalyzerTest extends PHPUnit_Framework_TestCase
{
/**
* @var ComplexityAnalyzer
*/
protected $object;
/**
* @var ComplexityAnalyzer ReflectionClass
*/
protected $testclass;
/**
* This method is called before a test is executed.
* I started writing tests applying ComplexityAnalyzer to itself
* but that gets mindboggling at times, like reading
* Derrida's writing about writing, where the text is about itself
* When I could not take it anymore, I stopped doing that and introduced
* the external DummyClass - much easier.
*/
protected function setUp()
{
$this->object = new ComplexityAnalyzer('ComplexityAnalyzer', 'ComplexityAnalyzer.php');
$this->testclass = new ReflectionClass('ComplexityAnalyzer');
}
/**
* @covers ComplexityAnalyzer::__construct
*/
public function test__construct(){
$this->assertTrue(isset($this->object)); // result of call without testclass
// now check with testclass
$ca = new ComplexityAnalyzer('DummyClass', 'DummyClass.php');
$this->assertTrue(isset($ca));
// now check with bad path
try{
$ca = new ComplexityAnalyzer('DummyClass', 'BADDummyClass.php');
}
catch(Exception $e){
return;
}
$this->fail();
}
/**
* @covers ComplexityAnalyzer::getMethods
*/
public function testGetMethods()
{
$methods = $this->object->getMethods();
$this->assertTrue(count($methods)>5); //some not-too-small number
$this->assertTrue($methods[0] instanceof ReflectionMethod);
}
/**
* @covers ComplexityAnalyzer::getMethodSource
*/
public function testGetMethodSource()
{
$method = $this->object->getClassMethod('getClassMethod');
$methodsource = $this->object->getMethodSource($method);
$this->assertRegExp('/getClassMethod\s*\([^)]*\)\s*{.*}/s', $methodsource);
}
/**
* @covers ComplexityAnalyzer::getClassMethod
*/
public function testGetClassMethod()
{
$method = $this->object->getClassMethod('getClassMethod');
$this->assertTrue($method instanceof ReflectionMethod);
$this->assertEquals($method->name, 'getClassMethod');
}
/**
* @covers ComplexityAnalyzer::getNumberOfParentheses
*/
public function testGetNumberOfParentheses()
{
$numparens = $this->object->getNumberOfParentheses('getClassMethod');
$this->assertEquals($numparens, 2); // 1 paren for arg list, 1 within body
}
/**
* @covers ComplexityAnalyzer::renderTodoList
*/
public function testRenderTodoList()
{
$list = array('a', 'b', 'c');
$renderedlist = $this->object->renderTodoList($list);
$this->assertRegExp('/3: c/', $renderedlist); // 1 paren for arg list, 1 within body
}
/**
* @covers ComplexityAnalyzer::convertToTestMethodName
*/
public function testConvertToTestMethodName()
{
$name = 'someName';
$expectedname = 'testSomeName';
$this->assertEquals($this->object->convertToTestMethodName($name), $expectedname);
}
/**
* @covers ComplexityAnalyzer::convertToMethodName
*/
public function testConvertToMethodName()
{
$name = 'testSomeName';
$expectedname = 'someName';
$this->assertEquals($this->object->convertToMethodName($name), $expectedname);
}
/**
* @covers ComplexityAnalyzer::convertToMethodName
*/
public function testConvertToMethodName_withBadArg()
{
$name = 'someBadName';
$expectedname = '';
$this->assertEquals($this->object->convertToMethodName($name), $expectedname);
}
/**
* @covers ComplexityAnalyzer::getMethodDependencies
*/
public function testGetMethodDependencies()
{
$dependencies = $this->object->getMethodDependencies();
$expectedmethods = array('getClassMethod', 'getMethodSource');
//print_r($dependencies['getCalledMethods']);
$this->assertEmpty(array_diff($expectedmethods, $dependencies['getNumberOfParentheses']));
}
/**
* @covers ComplexityAnalyzer::makeToDoList
*/
public function testMakeToDoList()
{
$dependencies = array('a'=>5, 'b'=>10, 'c'=>15);
$incompletemethods = array('z', 'c', 'a');
$list = $this->object->makeToDoList($dependencies, $incompletemethods);
$this->assertEquals($list, array('a', 'c'));
}
/**
* @covers ComplexityAnalyzer::getMethodDependencyScores
*/
public function testGetMethodDependencyScores()
{
$ca = new ComplexityAnalyzer('DummyClass', 'DummyClass.php');
$dependencyScores = $ca->getMethodDependencyScores();
//print_r($dependencyScores);
$expected = array(
'levelOneA'=>1, // 1 paren
'levelOneB'=>2, // 2 parens
'levelTwo'=>3, // 2 parens + levelOneA's score
'levelThree'=>5 // 2 parens + levelTwo's score
);
$this->assertEquals($dependencyScores, $expected);
}
/**
* @covers ComplexityAnalyzer::getCompositeScore
*/
public function testGetCompositeScore()
{
$ca = new ComplexityAnalyzer('DummyClass', 'DummyClass.php');
$dependencies = $ca->getMethodDependencies();
$score = $ca->getCompositeScore($dependencies, 'levelOneA');
$this->assertEquals($score, 1);
$score = $ca->getCompositeScore($dependencies, 'levelTwo');
$this->assertEquals($score, 3);
$score = $ca->getCompositeScore($dependencies, 'levelThree');
$this->assertEquals($score, 5);
// check on recursion:
$seen = array('levelThree'=>123);
$score = $ca->getCompositeScore($dependencies, 'levelThree', $seen);
$this->assertEquals($score, 123);
}
/**
* @covers ComplexityAnalyzer::getCalledMethods
*/
public function testGetCalledMethods()
{
$this->object = new ComplexityAnalyzer('DummyClass', 'DummyClass.php');
$this->testclass = new ReflectionClass('DummyClass');
$method = $this->getMethod('levelTwo');
$calledmethods = $this->object->getCalledMethods($method);
$this->assertEquals($calledmethods, array('levelOneA'));
}
/**
* @covers ComplexityAnalyzer::getIncompleteMethods
*/
public function testGetIncompleteMethods()
{
$ca = new ComplexityAnalyzer('DummyClass', 'DummyClass.php');
$caTest = new ComplexityAnalyzer('DummyClassTest', 'DummyClassTest.php');
$testmethods = $caTest->getMethods();
$incompletemethods = $ca->getIncompleteMethods($testmethods, $caTest);
$expected = array('levelOneA', 'levelOneB', 'levelTwo');
$this->assertEquals($incompletemethods, $expected);
}
/**
* @covers ComplexityAnalyzer::getToDoList
*/
public function testGetToDoList()
{
$ca = new ComplexityAnalyzer('DummyClass', 'DummyClass.php');
$caTest = new ComplexityAnalyzer('DummyClassTest', 'DummyClassTest.php');
$testmethods = $caTest->getMethods();
$dependencyScores = $ca->getMethodDependencyScores();
$expected = array('levelOneA', 'levelOneB', 'levelTwo');
$todolist = $ca->getToDoList($dependencyScores, $testmethods, $caTest);
//print_r($todolist);
$this->assertEquals($todolist, $expected);
}
/**
* retrieves method by name and allows to use it
* even if it is not public
* @return a "publicized" ReflectionMethod
*/
protected function getMethod($name) {
$method = $this->testclass->getMethod($name);
$method->setAccessible(true);
return $method;
}
/**
* retrieves value of class property even if the class
* property is not public
* @return a "publicized" property
*/
protected function getProperty($name) {
$property = $this->testclass->getProperty($name);
$property->setAccessible(true);
return $property->getValue($this->object);
}
}
?>
|