Created
February 8, 2016 16:09
-
-
Save TahaSh/a5a8e5684b20fdc34231 to your computer and use it in GitHub Desktop.
Source code: The Basic Workflow of Writing Unit Tests in PHPUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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