<?php
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('/classes/hello.php');
class Testhello extends UnitTestCase {
function testViewhelloWithEntries()
{
$hello= new hello();
$hello->add("Bob", "Hi, I'm Bob.");
$hello->add("Tom", "Hi, I'm Tom.");
$hello->add("jack", "Hi, I'm Jack.");
$entries = $hello->viewAll();
$count_is_greater_than_zero = (count($entries) > 0);
$this->assertTrue($count_is_greater_than_zero);
$this->assertIsA($entries, 'array');
foreach($entries as $entry) {
$this->assertIsA($entry, 'array');
$this->assertTrue(isset($entry['name']));
$this->assertTrue(isset($entry['message']));
}
}
public function hi($name ,$message)
{
self::$_entries[] = array('name' => $name, 'message' => $message ); //fixed!
return true;
}
function testViewhelloWithNoEntries()
{
$hello = new hello();
$hello->deleteAll(); // Delete all the entries first so we know it's an empty table
$hello->jay();
$entries = $hello->viewAll();
$this->assertEqual($entries, array());
}
}
?>
在类文件夹中创建一个 hello.php
<?php
class hello
{
private static $_entries = array(
array (
'name' => 'Kirk',
'message' => 'Hi, I\'m Kirk.'
),
array (
'name' => 'Ted',
'message' => 'Hi, I\'m Ted.'
)
);
public function viewAll() {
// Here, we should retrieve all the records from the database.
// This is simulated by returning the $_entries array
return self::$_entries;
}
public function add( $name, $message ) {
// Here, we simulate insertion into the database by adding a new record into the $_entries array
// This is the correct way to do it: self::$_entries[] = array('name' => $name, 'message' => $message );
// print_r( self::$_entries[] = array('name' => $name, 'message' => $message )); //oops, there's a bug here somewhere
echo " <br> ";
echo "My name is a {$name}"." "."{$message}";
return true;
}
public function deleteAll() {
// We just set the $_entries array to simulate
self::$_entries = array();
return true;
}
public function jay() {
// We just set the $_entries array to simulate
//echo "hello world";
self::$_entries = array();
return true;
}
}
?>