|
<?php
//------------------------------------------------------------------- // Test cases for the gRESTbook service //------------------------------------------------------------------- // Using the PHPUnit unit testing framework. Current design is // that this would be run from a command-line, passing in the url, // userid, and password to the service being tested. //------------------------------------------------------------------- // Copyright (c) 2007 Patrick Mueller // Using the MIT license: // http://www.opensource.org/licenses/mit-license.php //------------------------------------------------------------------- // 2007/03/xx - Patrick Mueller - pmuellr@yahoo.com //-------------------------------------------------------------------
require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'gRESTbookData.php';
// running from eclipse, with the PDT, the current directory // is always set to the directory of the php executable, but // I need to make the current directory the directory the file // lives in to get local includes working correctly.
$thisDir = dirname(__FILE__); chdir($thisDir);
//------------------------------------------------------------------- // All of our tests live in this 'test case' class //------------------------------------------------------------------- class gRESTbookDataTests extends PHPUnit_Framework_TestCase {
// database private $gRESTbookDB;
//--------------------------------------------------------------- // main entrypoint for the class; set the vars and run the tests //--------------------------------------------------------------- public static function main() {
PHPUnit_TextUI_TestRunner::run(self::suite()); } //--------------------------------------------------------------- // returns the tests in this class //--------------------------------------------------------------- public static function suite() { $suite = new PHPUnit_Framework_TestSuite(); $suite->addTestSuite(__CLASS__); return $suite; } //--------------------------------------------------------------- // initial setup run before each test //--------------------------------------------------------------- public function setUp() { $this->gRESTbookDB = new GRESTbookDB(); $gRESTbookDB = $this->gRESTbookDB; // get all the entries in the database ... $ids = $gRESTbookDB->query(); foreach ($ids as $id) { $entry = $gRESTbookDB->read($id); // ... and delete them $gRESTbookDB->delete($id, $entry->eTag); } } //--------------------------------------------------------------- // teardown after each test //--------------------------------------------------------------- public function tearDown() { $this->gRESTbookDB = null; } //--------------------------------------------------------------- // test create //--------------------------------------------------------------- public function testCreate() {
$gRESTbookDB = $this->gRESTbookDB;
// create a new entry $gRESTbookEntry = new GRESTbookEntry(); $gRESTbookEntry->author = "Abraham Lincoln"; $gRESTbookEntry->message = "Four score and seven years ago"; $result = $gRESTbookDB->create($gRESTbookEntry); $this->assertNotEquals(null, $gRESTbookEntry->id); $this->assertNotEquals(null, $gRESTbookEntry->dateModified); $this->assertNotEquals(null, $gRESTbookEntry->eTag); $this->assertEquals($result, $gRESTbookEntry);
// test null author $pass = false; try { $gRESTbookEntry->author = null; $gRESTbookDB->create($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass); // test empty string author $pass = false; try { $gRESTbookEntry->author = ''; $gRESTbookDB->create($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass);
$gRESTbookEntry->author = "Abraham Lincoln"; // test null message $pass = false; try { $gRESTbookEntry->message = null; $gRESTbookDB->create($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass); // test empty string message $pass = false; try { $gRESTbookEntry->message = ''; $gRESTbookDB->create($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass); } //--------------------------------------------------------------- // test read //--------------------------------------------------------------- public function testRead() { $gRESTbookDB = $this->gRESTbookDB; // create a new entry $gRESTbookEntry = new GRESTbookEntry(); $gRESTbookEntry->author = "Boy George"; $gRESTbookEntry->message = "Karma Chameleon"; $result = $gRESTbookDB->create($gRESTbookEntry); // try reading it $result2 = $gRESTbookDB->read($result->id); $this->assertEquals($result->id, $result2->id); $this->assertEquals($result->dateModified, $result2->dateModified); $this->assertEquals($result->eTag, $result2->eTag); $this->assertEquals($result->author, $result2->author); $this->assertEquals($result->message, $result2->message); // try reading a non-existant entry $result3 = $gRESTbookDB->read(100000); $this->assertEquals(null, $result3); }
//--------------------------------------------------------------- // test update //--------------------------------------------------------------- public function testUpdate() { $gRESTbookDB = $this->gRESTbookDB; // create a new entry $gRESTbookEntry = new GRESTbookEntry(); $gRESTbookEntry->author = "Charlie Brown"; $gRESTbookEntry->message = "Sigh"; // clone it $result1 = clone $gRESTbookDB->create($gRESTbookEntry); $gRESTbookEntry->author = "Dilbert"; $gRESTbookEntry->message = "Why me?"; // wait to make sure dateModified gets updated sleep(2); // update the entry $result2 = $gRESTbookDB->update($gRESTbookEntry); // make sure relevant things changed or didn't $this->assertEquals($result1->id, $result2->id); $this->assertTrue($result1->dateModified < $result2->dateModified); $this->assertNotEquals($result1->eTag, $result->eTag); $this->assertEquals("Dilbert", $result2->author); $this->assertEquals("Why me?", $result2->message); // test bogus id, eTag $oldId = $result2->id; $oldETag = $result2->eTag; // bogus id and eTag $result2->id = 100000; $result2->eTag = 'a'; $this->assertEquals(null, $gRESTbookDB->update($result2)); // bogus eTag $result2->id = $oldId; $result2->eTag = 'a'; $this->assertEquals(null, $gRESTbookDB->update($result2)); // bogus id $result2->id = 100001; $result2->eTag = $oldETag; $this->assertEquals(null, $gRESTbookDB->update($result2)); // test null author $pass = false; try { $gRESTbookEntry->author = null; $gRESTbookDB->update($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass); // test empty string author $pass = false; try { $gRESTbookEntry->author = ''; $gRESTbookDB->update($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass);
$gRESTbookEntry->author = "Abraham Lincoln"; // test null message $pass = false; try { $gRESTbookEntry->message = null; $gRESTbookDB->update($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass); // test empty string message $pass = false; try { $gRESTbookEntry->message = ''; $gRESTbookDB->update($gRESTbookEntry); } catch (Exception $e) { $pass = true; } $this->assertTrue($pass); } //--------------------------------------------------------------- // test delete //--------------------------------------------------------------- public function testDelete() { $gRESTbookDB = $this->gRESTbookDB; // create a new entry $gRESTbookEntry = new GRESTbookEntry(); $gRESTbookEntry->author = "Eric the Red"; $gRESTbookEntry->message = "Charge!"; $result1 = $gRESTbookDB->create($gRESTbookEntry); // try deleting it with a bogus eTag $this->assertFalse($gRESTbookDB->delete($result1->id, 'a')); // try deleting it with a bogus id $this->assertFalse($gRESTbookDB->delete('a', $result1->eTag)); // try deleting it $this->assertTrue($gRESTbookDB->delete($result1->id, $result1->eTag)); } //--------------------------------------------------------------- // test query //--------------------------------------------------------------- public function testQuery() { $gRESTbookDB = $this->gRESTbookDB;
// create a bunch of entries for ($i=0; $i<100; $i++) { $gRESTbookEntry = new GRESTbookEntry(); $gRESTbookEntry->author = "Author: $i"; $gRESTbookEntry->message = "Message: $i"; $result1 = $gRESTbookDB->create($gRESTbookEntry); } // list the entries $ids = $gRESTbookDB->query(); $this->assertEquals(100, count($ids)); // delete the entries foreach ($ids as $id) { $entry = $gRESTbookDB->read($id); $gRESTbookDB->delete($id, $entry->eTag); }
// make sure we have no more entries $ids = $gRESTbookDB->query(); $this->assertEquals(0, count($ids)); } }
//------------------------------------------------------------------- // run the tests //------------------------------------------------------------------- gRESTbookDataTests::main();
?>
|