Skip to content

Instantly share code, notes, and snippets.

@TahaSh
Created February 8, 2016 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TahaSh/a5a8e5684b20fdc34231 to your computer and use it in GitHub Desktop.
Save TahaSh/a5a8e5684b20fdc34231 to your computer and use it in GitHub Desktop.
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