Skip to content

Instantly share code, notes, and snippets.

@Sitebase
Created December 2, 2014 11:18
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 Sitebase/d1a47bc5bc8bed139285 to your computer and use it in GitHub Desktop.
Save Sitebase/d1a47bc5bc8bed139285 to your computer and use it in GitHub Desktop.
Example of a very basic unit test in PHP
<?php
function sum( $number1, $number2 )
{
if( ! is_int( $number1 ) || ! is_int( $number2 ) )
throw new Exception( 'Both arguments should be numeric' );
return $number1 + $number2;
}
<?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