Created
December 2, 2014 11:18
-
-
Save Sitebase/d1a47bc5bc8bed139285 to your computer and use it in GitHub Desktop.
Example of a very basic unit test in PHP
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 | |
function sum( $number1, $number2 ) | |
{ | |
if( ! is_int( $number1 ) || ! is_int( $number2 ) ) | |
throw new Exception( 'Both arguments should be numeric' ); | |
return $number1 + $number2; | |
} |
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 | |
include "code.php"; | |
class CodeTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testValidSum() | |
{ | |
$result = sum( 4, 5 ); | |
$this->assertEquals( 9, $result ); | |
} | |
public function testNegativeIntSum() | |
{ | |
$result = sum( -8, 10 ); | |
$this->assertEquals( 2, $result ); | |
} | |
/** | |
* @expectedException Exception | |
*/ | |
public function testInvalidInput() | |
{ | |
$result = sum( "4", 5); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment