Skip to content

Instantly share code, notes, and snippets.

@jonmchan
Created January 17, 2013 19:15
Show Gist options
  • Save jonmchan/4558701 to your computer and use it in GitHub Desktop.
Save jonmchan/4558701 to your computer and use it in GitHub Desktop.
PHP Unit Testing Example Code
<?php
// Calculator.php
class Calculator {
public function getNumberFromUserInput() {
// complicated function to get number from user input
}
public function printToScreen($value) {
// another complicated function
}
public function divideBy($num2) {
if ($num2 == 0) return NAN;
return $this->getNumberFromUserInput()/$num2;
}
public function divideByAndPrint($num2) {
if ($num2 == 0) $this->printToScreen("NaN");
$this->printToScreen($this->getNumberFromUserInput()/$num2);
}
}
<?php
// CalculatorTest.php
include_once("Calculator.php");
class CalculatorTest extends \PHPUnit_Framework_TestCase {
public function testDivideByPositiveNumber() {
$calcMock=$this->getMock('\Calculator',array('getNumberFromUserInput'));
$calcMock->expects($this->once())
->method('getNumberFromUserInput')
->will($this->returnValue(10));
$this->assertEquals(5,$calcMock->divideBy(2));
}
public function testDivideByZero() {
$calcMock=$this->getMock('\Calculator',array('getNumberFromUserInput'));
$calcMock->expects($this->never())
->method('getNumberFromUserInput')
->will($this->returnValue(10));
$this->assertEquals(NAN, $calcMock->divideBy(0));
}
public function testDivideByNegativeNumber() {
$calcMock=$this->getMock('\Calculator',array('getNumberFromUserInput'));
$calcMock->expects($this->once())
->method('getNumberFromUserInput')
->will($this->returnValue(10));
$this->assertEquals(-2,$calcMock->divideBy(-5));
}
public function testDivideByPositiveNumberAndPrint() {
$calcMock=$this->getMock('\Calculator',array('getNumberFromUserInput', 'printToScreen'));
$calcMock->expects($this->once())
->method('getNumberFromUserInput')
->will($this->returnValue(10));
$calcMock->expects($this->once())
->method('printToScreen')
->with($this->equalTo('5'));
$calcMock->divideByAndPrint(2);
}
}
@azazqadir
Copy link

Before writing test case, you first have to organize your directory accordingly after install the PHPunit tool. There are also some basic conventions of writing the test case

Following are some basic conventions and steps for writing tests with PHPUnit:

Test File names should have a suffix Test. For example, if First.php needs to be tested,the name of the test file name will be FirstTest.php
Similarly, If the class name is MyFirstClass than the test class name will be MyFirstClassTest.
Add test as the prefix for method names. For example,if the method name is getuser(), then in test class, it will become testgetuser(). You can also use @test annotation in document block to declare it as a testing method.
All testing methods are public
MyFirstClassTest should be inherited from PHPUnit\Framework\TestCase

Source: PHP Unit testing with PHPUnit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment