Last active
December 12, 2015 00:58
-
-
Save jheth/4687709 to your computer and use it in GitHub Desktop.
Sample PHP class and PHPUnit test using a dataProvider.
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 StringUtil | |
{ | |
public function is_palindrome($str) | |
{ | |
if (!is_string($str)) { | |
return false; | |
} | |
$length = strlen($str); | |
for ($i = 0; $i < (int)($length / 2); $i++) { | |
if ($str[$i] != $str[$length-$i-1]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
} | |
<?php | |
require_once 'PHPUnit/Framework.php'; | |
require_once 'StringUtil.php'; | |
class StringUtilTest extends PHPUnit_Framework_TestCase | |
{ | |
protected $object; | |
protected function setUp() | |
{ | |
$this->object = new StringUtil(); | |
} | |
/** | |
* @dataProvider is_palindrome_data_provider | |
*/ | |
public function test_is_palindrome($string, $is_palindrome) | |
{ | |
$this->assertEquals($is_palindrome, $this->object->is_palindrome($string)); | |
} | |
public static function is_palindrome_data_provider() | |
{ | |
return array( | |
array('bob', true), | |
array('racecar', true), | |
array('hannah', true), | |
array('abc', false), | |
array('a', true), | |
array('milk', false), | |
array('anna', true), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment