My personal attempt to learn unit test
in PHP, using PHPUnit
.
I used two following examples to learn.
reference1: https://www.youtube.com/watch?v=-9YVcssCACI
reference2: https://www.youtube.com/watch?v=84j61_aI0q8
Tutorial 1
- Composer is necessary for PHPUnit
Include PHPUnit framework in composer. Open
composer.json
& include the following123"require-dev":{"phpunit/phpunit": "3.7.*"}run
composer update
. This generates phpunit files.create folder for tests. Organize it into appropriate structure.
Declare class in the test file
12345class PagesTest extends PHPUnit_Framework_TestCase{}execute it by
phpunit [file]
at commandline.- 123456789class PagesTest extends PHPUnit_Framework_TestCase{public function testRenderReturnsHelloWorld(){$pages = new \Controllers\Core\Web\Pages();$expected = 'Hello World';$this->assertEquals($expected, $pages->render());}}
Create
phpunit.xml
12345678910111213141516"1.0" encoding="UTF-8" xml version=<phpunit bootstrap="./vendor/autoload.php"color="true"convertErrorsToExceptions="true"convertNoticesToExceptions="true"convertWarningsToExceptions="true"stopOnFailures="false"syntaxCheck="false"><testsuites><testsuite name="Tutorial Unit Tests"><!-- indicating the test directory --><directory>./tests/</directory></testsuite></testsuites></phpunit>add test for other functions
1234567891011121314151617181920212223class PagesTest extends PHPUnit_Framework_TestCase{public function testRenderReturnsHelloWorld(){$pages = new \Controllers\Core\Web\Pages();$expected = 'Hello World';$this->assertEquals($expected, $pages->render());}public function testReturnTrueReturnsTrue(){$pages = new \Controllers\Core\Web\Pages();$this->assertTrue($pages->returnTrue());}public function testReturnArrayReturnsValidArray(){$pages = new \Controllers\Core\Web\Pages();$this->assertTrue(is_array($pages->returnArray()));}public function testReturnArrayReturnsNonEmptyArray(){$pages = new \Controllers\Core\Web\Pages();$this->assertTrue((count($pates->returnArray() > 0));}}
Tutorial 2
Create
test
directory and make testcase.1234567class CalculatorTest extends PHPUnit_Framework_TestCase {public funtion testAddNumbers(){$calc = new Calculator;$this->assertEquals(4, $calc->add(2,2));}}Create
app
directory andlibraries
directory in it.add
require
keyword to the test file.12345678910// but this means everytime we add classes & dependencies, we need to add require statement. Let's automate it with composerrequire 'app/libraries/Calculator.php';class CalculatorTest extends PHPUnit_Framework_TestCase {public funtion testAddNumbers(){$calc = new Calculator;$this->assertEquals(4, $calc->add(2,2));}}Create
composer.json
1234567{"autoload":{"classmap": ["app/libraries"]}}run
composer install
Add
require 'vendor/autoload.php'
to the test file instead of the path require.That works, but use
phpunit.xml
to do bootstrapping.Use
namespace
by adding1namespace App\Libraries;regenerate
autoload_classmap.php
, so that namespace can be applied.add
12345678910111213141516// including specific class using namespaceuse App\Libraries\Calculator;class CalculatorTest extends PHPUnit_Framework_TestCase{public funtion testAddNumbers(){$calc = new Calculator;$this->assertEquals(4, $calc->add(2,2));}public function testThrowsExceptionIfNonNumericIsPassed(){$calc = new Calculator;$calc->add('a', []);}}Throw exception for non-numeric input
123456789101112131415namespace App\Libraries;class Calculator {public function add($x, $y){// wrong. Should be numeric instead of int// if ( ! is_int($x) or ! is_int($y)){if ( ! is_numeric($x) or ! is_numeric($y)){// `\` indicates this is a global exception, not a local exception.// if '\' is not here, it considers as local (App\Libraries\InvalidArgumentException) exception can causes errorthrow new \InvalidArgumentException;}return $x + $y;}}Make the test expect an exception
12345678910111213141516171819202122232425262728293031// including specific class using namespaceuse App\Libraries\Calculator;class CalculatorTest extends PHPUnit_Framework_TestCase{public funtion testAddNumbers(){$calc = new Calculator;$values = [[2,2,4],[2.5,2.5,5],[-3,1,-2]];foreach($values as $nums){$this->assertEquals($numbers[2], $calc->add($numbers[0], $numbers[1]));}// $this->assertEquals(4, $calc->add(2,2));// // add another assert// $this->assertEquals(5, $calc->add(2.5,2.5));// $this->assertEquals(-2, $calc->add(-3,1));}/*** @expectedException InvalidArgumentException*/public function testThrowsExceptionIfNonNumericIsPassed(){$calc = new Calculator;$calc->add('a', []);}}Abstract out your tests using PHPUnit.
|
|