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

Commit

Permalink
Unit testing (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
edsonmedina committed Feb 8, 2015
1 parent ad42166 commit 359adf1
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 32 deletions.
18 changes: 16 additions & 2 deletions tests/Issues/CodeOnGlobalSpaceIssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ class CodeOnGlobalSpaceIssueTest extends PHPUnit_Framework_TestCase
public function testGetTitle ()
{
$node = $this->getMockBuilder('PhpParser\Node\Stmt\Class_')
->disableOriginalConstructor()
->getMock();
->disableOriginalConstructor()
->getMock();

$issue = new CodeOnGlobalSpaceIssue ($node);

$this->assertEquals('Code on global space', $issue->getTitle());
}

/**
* @covers edsonmedina\php_testability\Issues\CodeOnGlobalSpaceIssue::getID
*/
public function testGetID ()
{
$node = $this->getMockBuilder('PhpParser\Node\Stmt\Class_')
->disableOriginalConstructor()
->getMock();

$issue = new CodeOnGlobalSpaceIssue ($node);

$this->assertEquals('', $issue->getID());
}
}
36 changes: 36 additions & 0 deletions tests/Issues/EmptyCatchIssueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

require_once __DIR__.'/../../vendor/autoload.php';

use edsonmedina\php_testability\Issues\EmptyCatchIssue;

class EmptyCatchIssueTest extends PHPUnit_Framework_TestCase
{
/**
* @covers edsonmedina\php_testability\Issues\EmptyCatchIssue::getTitle
*/
public function testGetTitle ()
{
$node = $this->getMockBuilder('PhpParser\Node\Stmt\Catch_')
->disableOriginalConstructor()
->getMock();

$issue = new EmptyCatchIssue ($node);

$this->assertEquals('Empty catch block', $issue->getTitle());
}

/**
* @covers edsonmedina\php_testability\Issues\EmptyCatchIssue::getID
*/
public function testGetID ()
{
$node = $this->getMockBuilder('PhpParser\Node\Stmt\Catch_')
->disableOriginalConstructor()
->getMock();

$issue = new EmptyCatchIssue ($node);

$this->assertEquals('', $issue->getID());
}
}
36 changes: 36 additions & 0 deletions tests/Issues/ErrorSuppressionIssueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

require_once __DIR__.'/../../vendor/autoload.php';

use edsonmedina\php_testability\Issues\ErrorSuppressionIssue;

class ErrorSuppressionIssueTest extends PHPUnit_Framework_TestCase
{
/**
* @covers edsonmedina\php_testability\Issues\ErrorSuppressionIssue::getTitle
*/
public function testGetTitle ()
{
$node = $this->getMockBuilder('PhpParser\Node\Expr\ErrorSuppress')
->disableOriginalConstructor()
->getMock();

$issue = new ErrorSuppressionIssue ($node);

$this->assertEquals('Error suppression', $issue->getTitle());
}

/**
* @covers edsonmedina\php_testability\Issues\ErrorSuppressionIssue::getID
*/
public function testGetID ()
{
$node = $this->getMockBuilder('PhpParser\Node\Expr\ErrorSuppress')
->disableOriginalConstructor()
->getMock();

$issue = new ErrorSuppressionIssue ($node);

$this->assertEquals('', $issue->getID());
}
}
36 changes: 36 additions & 0 deletions tests/Issues/ExitIssueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

require_once __DIR__.'/../../vendor/autoload.php';

use edsonmedina\php_testability\Issues\ExitIssue;

class ExitIssueTest extends PHPUnit_Framework_TestCase
{
/**
* @covers edsonmedina\php_testability\Issues\ExitIssue::getTitle
*/
public function testGetTitle ()
{
$node = $this->getMockBuilder('PhpParser\Node\Expr\Exit_')
->disableOriginalConstructor()
->getMock();

$issue = new ExitIssue ($node);

$this->assertEquals('Exit', $issue->getTitle());
}

/**
* @covers edsonmedina\php_testability\Issues\ExitIssue::getID
*/
public function testGetID ()
{
$node = $this->getMockBuilder('PhpParser\Node\Expr\Exit_')
->disableOriginalConstructor()
->getMock();

$issue = new ExitIssue ($node);

$this->assertEquals('', $issue->getID());
}
}
123 changes: 93 additions & 30 deletions tests/NodeVisitors/ClassVisitorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function testEnterNodeWithDifferentType ()
$factory = $this->getMock('edsonmedina\php_testability\AnalyserAbstractFactory');

$scope = $this->getMockBuilder('edsonmedina\php_testability\AnalyserScope')
->disableOriginalConstructor()
->setMethods(array ('startClass'))
->getMock();
->disableOriginalConstructor()
->setMethods(array ('startClass'))
->getMock();

$scope->expects($this->never())->method('endClass');

Expand All @@ -27,9 +27,9 @@ public function testEnterNodeWithDifferentType ()
->getMock();

$visitor = $this->getMockBuilder('edsonmedina\php_testability\NodeVisitors\ClassVisitor')
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();

$visitor->expects($this->once())->method('isClass')->willReturn(false);

Expand All @@ -45,9 +45,9 @@ public function testLeaveNodeWithDifferentType ()
$factory = $this->getMock('edsonmedina\php_testability\AnalyserAbstractFactory');

$scope = $this->getMockBuilder('edsonmedina\php_testability\AnalyserScope')
->disableOriginalConstructor()
->setMethods(array ('endClass'))
->getMock();
->disableOriginalConstructor()
->setMethods(array ('endClass'))
->getMock();

$scope->expects($this->never())->method('endClass');

Expand All @@ -56,9 +56,9 @@ public function testLeaveNodeWithDifferentType ()
->getMock();

$visitor = $this->getMockBuilder('edsonmedina\php_testability\NodeVisitors\ClassVisitor')
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();

$visitor->expects($this->once())->method('isClass')->willReturn(false);

Expand All @@ -74,9 +74,9 @@ public function testLeaveNode ()
$factory = $this->getMock('edsonmedina\php_testability\AnalyserAbstractFactory');

$scope = $this->getMockBuilder('edsonmedina\php_testability\AnalyserScope')
->disableOriginalConstructor()
->setMethods(array ('endClass'))
->getMock();
->disableOriginalConstructor()
->setMethods(array ('endClass'))
->getMock();

$scope->expects($this->once())->method('endClass');

Expand All @@ -85,9 +85,9 @@ public function testLeaveNode ()
->getMock();

$visitor = $this->getMockBuilder('edsonmedina\php_testability\NodeVisitors\ClassVisitor')
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();

$visitor->expects($this->once())->method('isClass')->willReturn(true);

Expand All @@ -97,24 +97,78 @@ public function testLeaveNode ()
/**
* @covers edsonmedina\php_testability\NodeVisitors\ClassVisitor::enterNode
*/
public function testEnterNode ()
public function testEnterNodeWithNonFinalClass ()
{
$data = $this->getMock('edsonmedina\php_testability\ReportData');
// data
$data = $this->getMockBuilder('edsonmedina\php_testability\ReportData')
->disableOriginalConstructor()
->setMethods(array('addIssue'))
->getMock();

$data->expects($this->never())->method('addIssue');

// scope
$scope = $this->getMockBuilder('edsonmedina\php_testability\AnalyserScope')
->disableOriginalConstructor()
->setMethods(array ('startClass'))
->getMock();
->disableOriginalConstructor()
->setMethods(array('startClass'))
->getMock();

$scope->expects($this->once())->method('startClass')->with($this->equalTo('foo'));

// node wrapper
$nodewrapper = $this->getMockBuilder ('edsonmedina\php_testability\NodeWrapper')
->disableOriginalConstructor()
->getMock();

$nodewrapper->method ('getName')->willReturn ('foo');

$scope->expects($this->once())
->method('startClass')
->with($this->equalTo('foo'));
// factory
$factory = $this->getMockBuilder ('edsonmedina\php_testability\AnalyserAbstractFactory')
->getMock();

$factory->method ('getNodeWrapper')->willReturn ($nodewrapper);

// node
$node = $this->getMockBuilder ('PhpParser\Node\Stmt\Class_')
->disableOriginalConstructor()
->setMethods(array('isFinal'))
->getMock();

// node wrapper
$node->expects($this->once())->method('isFinal')->willReturn (false);

// visitor
$visitor = $this->getMockBuilder('edsonmedina\php_testability\NodeVisitors\ClassVisitor')
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array('isClass'))
->getMock();

$visitor->expects($this->once())->method('isClass')->willReturn(true);

$visitor->enterNode ($node);
}

/**
* @covers edsonmedina\php_testability\NodeVisitors\ClassVisitor::enterNode
*/
public function testEnterNode ()
{
// data
$data = $this->getMockBuilder('edsonmedina\php_testability\ReportData')
->disableOriginalConstructor()
->setMethods(array('addIssue'))
->getMock();

$data->expects($this->once())->method('addIssue');

// scope
$scope = $this->getMockBuilder('edsonmedina\php_testability\AnalyserScope')
->disableOriginalConstructor()
->setMethods(array('startClass'))
->getMock();

$scope->expects($this->once())->method('startClass')->with($this->equalTo('foo'));

// node wrapper
$nodewrapper = $this->getMockBuilder ('edsonmedina\php_testability\NodeWrapper')
->disableOriginalConstructor()
->getMock();
Expand All @@ -127,10 +181,19 @@ public function testEnterNode ()

$factory->method ('getNodeWrapper')->willReturn ($nodewrapper);

// node
$node = $this->getMockBuilder ('PhpParser\Node\Stmt\Class_')
->disableOriginalConstructor()
->setMethods(array('isFinal'))
->getMock();

$node->expects($this->once())->method('isFinal')->willReturn (true);

// visitor
$visitor = $this->getMockBuilder('edsonmedina\php_testability\NodeVisitors\ClassVisitor')
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array ('isClass'))
->getMock();
->setConstructorArgs(array($data, $scope, $factory))
->setMethods(array('isClass'))
->getMock();

$visitor->expects($this->once())->method('isClass')->willReturn(true);

Expand Down

0 comments on commit 359adf1

Please sign in to comment.