* @license PHP License * @package WB * @subpackage unittest */ WBClass::load('WBException_Datasource'); /** * Unit Test: Datasource Comment * * Test table datasource comment * * @version 0.1.1 * @package WB * @subpackage unittest */ class TestCaseDatasourceKeyvaluepair extends UnitTestCase { /** * datasource * @var WBDatasource_KeyValuePair */ protected $kvp; /** * datasource * @var WBDatasource_Table */ protected $table; public function __construct() { WBClass::load('WBUnitTest_SQLUte'); WBUnitTest_SQLUte::source(__FILE__,'construct'); $this->table = WBClass::create('WBDatasource_Table'); $this->kvp = WBClass::create('WBDatasource_KeyValuePair'); } public function __destruct() { WBUnitTest_SQLUte::source( __FILE__, 'destruct' ); } /** * check instance * */ public function testInit() { $this->assertTrue(class_exists('WBDatasource_Table', false)); $this->assertTrue(is_object($this->table)); $this->assertIsA($this->table, 'WBDatasource_Table'); $this->assertTrue(class_exists('WBDatasource_KeyValuePair', false)); $this->assertTrue(is_object($this->kvp)); $this->assertIsA($this->kvp, 'WBDatasource_KeyValuePair'); } public function testEmptyDomain() { $this->kvp->setDomain(); $this->doTestAdd(); $this->doTestUpdate(); $this->doTestGet(); $this->doTestGetAsList(); $this->doTestDelete(); $this->doTestGetKeys(); } public function testAddFooDomain() { $this->kvp->setDomain('foo'); $this->doTestAdd('foo'); $this->doTestUpdate('foo'); $this->doTestGet('foo'); $this->doTestGetAsList('foo'); $this->doTestDelete('foo'); $this->doTestGetKeys('foo'); } public function doTestAdd($domain = 'empty') { $data = array( $domain . '-forename' => 'Peter', $domain . '-surname' => 'Parker', $domain . '-superhero' => 'Spiderman' ); $kvpid = $this->kvp->set($data); $this->assertNotNull($kvpid); $data = array( $domain . '-forename' => 'Clark', $domain . '-surname' => 'Kent', $domain . '-superhero' => 'Supermann' ); $kvpid = $this->kvp->set($data); $this->assertNotNull($kvpid); $data = array( $domain . '-forename' => 'Bruce', $domain . '-surname' => 'Wayne', $domain . '-superhero' => 'Batman' ); $kvpid = $this->kvp->set($data); $this->assertNotNull($kvpid); $data = array( $domain . '-forename' => 'Charley', $domain . '-surname' => 'Parker', $domain . '-superhero' => 'Golden Eagle' ); $kvpid = $this->kvp->set($data); $this->assertNotNull($kvpid); } public function doTestUpdate($domain = 'empty') { $data = array( $domain . '-forename' => 'Jean Paul', $domain . '-surname' => 'Valley', $domain . '-superhero' => 'Batman' ); $first = $this->kvp->set($data); $this->assertNotNull($first); $data = array( $domain . '-forename' => 'Bruce', $domain . '-surname' => 'Wayne', $domain . '-superhero' => 'Batman' ); $second = $this->kvp->set($data, $first); $this->assertNotNull($second); $this->assertEqual($first, $second, 'ids should be the same for updates'); } public function doTestGet($domain = 'empty') { $dataSet = array( $domain . '-forename' => 'Tom', $domain . '-surname' => 'Townsend', $domain . '-superhero' => 'Captain Flag' ); $kvid = $this->kvp->set($dataSet); $this->assertNotNull($kvid); $dataGet = $this->kvp->get($kvid); $this->assertEqual($dataGet, $dataSet); } public function doTestGetAsList($domain = 'empty') { $dataSet = array( $domain . '-forename' => 'Tom', $domain . '-superhero' => 'Captain Flag', $domain . '-surname' => 'Townsend' ); $kvid = $this->kvp->set($dataSet); $this->assertNotNull($kvid); $listSet = array(); foreach ($dataSet as $k => $v) { $listSet[] = array( 'key' => $k, 'value' => $v ); } $listGet = $this->kvp->get($kvid, true); // remove id items for ($i = 0; $i < count($listGet); ++$i) { unset($listGet[$i]['id']); } $this->assertEqual($listGet, $listSet); } public function doTestDelete($domain = 'empty') { $dataSet = array( $domain . '-forename' => 'Keith', $domain . '-surname' => 'Spencer', $domain . '-superhero' => 'Captain Flash' ); $kvid = $this->kvp->set($dataSet); $this->assertNotNull($kvid); $dataGet = $this->kvp->get($kvid); $this->assertEqual($dataGet, $dataSet); $this->kvp->delete($kvid); $dataGet = $this->kvp->get($kvid); $this->assertTrue(is_array($dataGet)); $this->assertTrue(empty($dataGet)); } public function doTestGetKeys($domain = 'empty') { $dataSet = array( $domain . '-forename' => 'Don', $domain . '-surname' => 'Wright', $domain . '-superhero' => 'Captain Freedom' ); $kvid = $this->kvp->set($dataSet); $this->assertNotNull($kvid); $keys = $this->kvp->getKeys(); $this->assertTrue(is_array($keys)); $this->assertEqual(count($keys), 3); foreach ($keys as $k) { $this->assertEqual(0, strncmp($domain . '-', $k, strlen($domain) + 1)); } } }