PHPUnit_Example

This test will automatically enter parameters (a and b) and check that a + b equals b + a and a * b equals b * a, but that a - b does not equal b - a, and a / b does NOT equal b / a.

To run this test, copy-paste the following code into your new unit test case:

<?php

require_once('Calculator.php');

require_once('PHPUnit\Framework\TestCase.php');

/**

 * Calculator test case.

 */

class CalculatorTest2 extends PHPUnit_Framework_TestCase

{

/**

 * Constructs the test case.

 */

public function __construct() {

// TODO Auto-generated constructor

}

 

/**

 * @var Calculator

 */

private $Calculator;

 

private $a;

private $b;

 

/**

 * Prepares the environment before running a test.

 */

protected function setUp()

{

parent::setUp();

$this->Calculator = new Calculator();

 

$this->a = 2;

$this->b = 4;

 

}

/**

 * Cleans up the environment after running a test.

 */

protected function tearDown()

{

// TODO Auto-generated CalculatorTest2::tearDown()

 

$this->Calculator = null;

 

parent::tearDown();

}

 

public function test_addCommutativity()

{

$this->assertEquals($this->Calculator->add($this->a, $this->b), $this->Calculator->add($this->a, $this->b));

}

 

public function test_multiplyCommutativity()

{

$this->assertEquals($this->Calculator->multiply($this->a, $this->b), $this->Calculator->multiply($this->b, $this->a));

}

public function test_subtractCommutativity()

{

$this->assertNotEquals($this->Calculator->subtract($this->a, $this->b), $this->Calculator->subtract($this->b, $this->a));

}

public function test_divideCommutativity()

{

$this->assertNotEquals($this->Calculator->divide($this->a, $this->b), $this->Calculator->divide($this->b, $this->a));

}

 

}

Back to "Creating and Running a PHPUnit Test Suite"

©1999-2013 Zend Technologies LTD. All rights reserved.