Skip to content

Instantly share code, notes, and snippets.

@georgestephanis
Last active March 30, 2016 05:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save georgestephanis/98b42358032501936ab0 to your computer and use it in GitHub Desktop.
Save georgestephanis/98b42358032501936ab0 to your computer and use it in GitHub Desktop.
Unit Tests Demo
<?php
class StephanisAddition {
public static function add_two_numbers( $number_one, $number_two ) {
if ( ! is_numeric( $number_one ) || ! is_numeric( $number_two ) ) {
return null;
}
return $number_one + $number_two;
}
}
<?php
include 'class.stephanis-addition.php';
class StephanisAdditionTest extends PHPUnit_Framework_TestCase {
public function test_adding_two_numbers() {
$this->assertEquals( 12, StephanisAddition::add_two_numbers( 5, 7 ) );
}
public function test_rejection_of_non_numbers() {
$this->assertNull( StephanisAddition::add_two_numbers( 'Blue', 7 ) );
$this->assertNull( StephanisAddition::add_two_numbers( 7, 'Pink' ) );
$this->assertNull( StephanisAddition::add_two_numbers( 'Blue', 'Pink' ) );
}
}
@georgestephanis
Copy link
Author

To run the tests in test-class.stephanis-addition.php from the command line, you would run:

phpunit test-class.stephanis-addition.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment