Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Source code: The Basic Workflow of Writing Unit Tests in PHPUnit
<?php
class Calculator
{
private $result;
function __construct($initial = 0)
{
$this->result = $initial;
}
public function getResult()
{
return $this->result;
}
public function add($number)
{
$this->result += $number;
return $this;
}
public function sub($number)
{
$this->result -= $number;
return $this;
}
}
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
/** @test */
public function it_can_start_with_an_initial_number()
{
$calculator = new Calculator(8);
$this->assertEquals(8, $calculator->getResult());
}
/** @test */
public function it_starts_with_zero_if_nothing_is_specified()
{
$calculator = new Calculator;
$this->assertEquals(0, $calculator->getResult());
}
/** @test */
public function it_adds_numbers()
{
$calculator = new Calculator(2);
$result = $calculator->add(3)->getResult();
$this->assertEquals(5, $result);
}
/** @test */
public function it_subtracts_numbers()
{
$calculator = new Calculator(10);
$result = $calculator->sub(2)->getResult();
$this->assertEquals(8, $result);
}
/** @test */
public function it_can_mix_operations()
{
$calculator = new Calculator(10);
$result = $calculator->add(2)->sub(5)->getResult();
$this->assertEquals(7, $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment