public function test_blockRows() { $x = new Template(dirname(__FILE__) . '/files/templates/mainWithBlockRow.tmpl'); $rowDefs = $x->get_block_row_defs(); $this->assertTrue(is_array($rowDefs), "missing block rows array"); $this->assertTrue(count($rowDefs) > 0, "no block rows found... "); $this->assertEquals(1, count($rowDefs), "failed to parse block rows from main template"); $rows = array('first' => array('var1' => "this", 'var2' => "is", 'var3' => "the first row"), 'second' => array('var1' => "And this", 'var2' => "can be", 'var3' => "the next(second) row"), 'third' => array('var1' => "The final", 'var2' => "version", 'var3' => "right here")); $x->setBlockRow('test'); $x->parseBlockRow('test', $rows); foreach ($rows as $rowName => $data) { $joined = implode(' ', $data); $testPosition = strpos($x->render(), $joined); $this->assertTrue(is_numeric($testPosition), "string position isn't numeric:" . ToolBox::debug_var_dump($testPosition, 0)); $this->assertTrue($testPosition > 0, " ({$testPosition}) rendered template is missing string '" . $joined . "'... " . ToolBox::debug_var_dump($testPosition, 0) . $x->render()); } $this->assertFalse((bool) preg_match('~<!-- BEGIN ~', $x->render()), "rendered template still contains block row begin tag"); $this->assertFalse((bool) preg_match('~<!-- END ~', $x->render()), "rendered template still contains block row end tag"); }
/** * Update a single record with the given changes. * * @recId (int) ID to update. * @updates (array) field=>value list of changes. * * @RETURN (int) SUCCESS: (int) is the number of records updated (should always be 1) * @EXCEPTION FAIL: exception indicates the error. * * TODO: remove arg #3, since it is now unused * TODO: remove arg #4, since arg #1 can be an array (to make the same specification) */ public function update_record($recId, array $updates) { if ((is_numeric($recId) && $recId >= 0 or is_array($recId) && count($recId)) && is_array($updates) && count($updates) > 0) { $updateString = ""; $params = array(); foreach ($updates as $f => $v) { $updateString = $this->gfObj->create_list($updateString, $f . '=:' . $f, ', '); $params[$f] = $v; } if (is_array($recId)) { foreach ($recId as $f => $v) { $whereClause = $this->gfObj->create_list($whereClause, $f . '=:' . $f, ' AND '); $params[$f] = $v; } } else { $whereClause = $this->pkeyField . '=:id'; $params['id'] = $recId; } $sql = 'UPDATE ' . $this->tableName . ' SET ' . $updateString . ' WHERE ' . $whereClause; try { $retval = $this->dbObj->run_update($sql, $params); } catch (Exception $e) { throw new exception(__METHOD__ . ":: failed to update record (" . $recId . "), DETAILS::: " . $e->getMessage()); } } else { throw new exception(__METHOD__ . ":: failed to update record (" . $recId . "), invalid recordId (" . $recId . "), or no data in array::: " . ToolBox::debug_var_dump($updates, 0)); } return $retval; }
public function test_basics() { $this->assertTrue(is_object($this->dbObj), "No database objects to test"); $type = 'pgsql'; $this->assertEquals($type, $this->dbObj->get_dbType(), "Database type mismatch, expecting (" . $type . "), got (" . $this->dbObj->get_dbType() . ")"); $this->assertTrue($this->reset_db(dirname(__FILE__) . '/../setup/schema.' . $type . '.sql'), "Failed to reset database"); $this->assertFalse($this->dbObj->get_transaction_status(), "Already in transaction...?"); $beginTransRes = $this->dbObj->beginTrans(); $transactionStatus = $this->dbObj->get_transaction_status(); $this->assertTrue($transactionStatus); $this->assertTrue($beginTransRes, "Start of transaction failed (" . $beginTransRes . "), status=(" . $transactionStatus . ")"); $this->dbObj->exec('CREATE TABLE test (id serial not null PRIMARY KEY, data text not null);'); $this->assertTrue($this->dbObj->get_transaction_status(), "Got out of transaction...?"); // Make sure we get 0 rows before any data has been inserted. $numRows = $this->dbObj->run_query("SELECT * FROM test"); $data = $this->dbObj->farray_fieldnames(); $this->assertEquals($numRows, count($data), "Invalid number of rows returned: expected (" . count($data) . "), got (" . $numRows . ")"); $this->assertEquals($numRows, 0, "Returned unexpected number of rows on fresh table (" . $numRows . ")"); $testData = array(0 => 'test1', 1 => 'test2'); $i = 1; $insertTestSql = "INSERT INTO test (data) VALUES (:val)"; foreach ($testData as $val) { $createdId = $this->dbObj->run_insert($insertTestSql, array('val' => $val), 'test_id_seq'); $this->assertTrue(is_numeric($createdId), "Insert did not yield integer value (" . $createdId . ")"); $this->assertEquals($i, $createdId, "Expected Id (" . $i . ") does not match created id (" . $createdId . ") for test data (" . $val . ")"); $i++; } // now make sure we've got the date expected. $numRows = $this->dbObj->run_query("SELECT * FROM test"); $data = $this->dbObj->farray_fieldnames(); $this->assertTrue(is_array($data), "Returned data in an invalid format"); $this->assertEquals($numRows, count($testData), "Invalid number of records created, expected (" . count($testData) . "), got (" . $numRows . ")"); $this->assertTrue(isset($data[0]), "Zeroth index does not exist?"); $this->assertTrue(isset($data[0]['id']), "ID index missing from returned data"); $this->assertTrue(isset($data[0]['data']), "DATA index missing from returned data"); $this->assertEquals($data[0]['id'], 1, "Invalid ID in element 0, expected 1 but got (" . $data[0]['id'] . ")"); $this->assertEquals($data[1]['id'], 2, "Invalid ID in element 1, expected 2 but got (" . $data[1]['id'] . ")"); $numRows = $this->dbObj->run_query("SELECT * FROM test"); $data = $this->dbObj->farray_nvp('id', 'data'); $this->assertEquals("test1", $data[1], "Expected ID 1 to be 'test1', but instead got '" . $data[1] . "'"); $this->assertEquals("test2", $data[2], "Expected ID 2 to be 'test2', but instead got '" . $data[2] . "'"); // add a record with a specified ID (retrieving the sequence value will appear to be incorrect, because we're not using it). $testData[4] = "test5"; $createdId = $this->dbObj->run_insert("INSERT INTO test (id, data) VALUES (:id, :val)", array('id' => 5, 'val' => $testData[4]), 'test_id_seq'); $this->assertNotEquals($createdId, 5, "Inserting out-of-order index failed, insert ID should have been 2 (not " . $createdId . ")"); $numRows = $this->dbObj->run_query("SELECT * FROM test"); $data = $this->dbObj->farray_nvp('id', 'data'); $this->assertTrue(is_array($data), "Did not retrieve array of information from database... (" . ToolBox::debug_var_dump($data, 0) . ")"); $this->assertEquals(count($data), count($testData), "Number of records in database (" . count($data) . ") do not match what is expected (" . count($testData) . ")"); $testData[2] = "test3"; $createdId = $this->dbObj->run_insert($insertTestSql, array('val' => $testData[2]), 'test_id_seq'); $this->assertEquals($createdId, 3, "Failed to insert ID #3...?"); $testData[3] = "test4"; $createdId = $this->dbObj->run_insert($insertTestSql, array('val' => $testData[3]), 'test_id_seq'); $this->assertEquals($createdId, 4, "Failed to insert ID #4...?"); // Make sure farray_fieldnames works as expected. $numRows = $this->dbObj->run_query("SELECT * FROM test"); $data = $this->dbObj->farray_fieldnames('id'); $this->assertEquals(array('id' => 1, 'data' => 'test1'), $data[1]); $this->assertEquals(array('id' => 2, 'data' => 'test2'), $data[2]); $this->assertEquals(array('id' => 3, 'data' => 'test3'), $data[3]); $this->assertEquals(array('id' => 4, 'data' => 'test4'), $data[4]); $this->assertEquals(array('id' => 5, 'data' => 'test5'), $data[5]); $this->assertEquals(count($data), 5); $this->assertTrue($this->dbObj->commitTrans()); $this->assertTrue($this->dbObj->beginTrans()); //Okay, here's where there should be an error (re-inserting data that's already there) try { $createdId = $this->dbObj->run_insert($insertTestSql, array('val' => $testData[4]), 'test_id_seq'); $this->assertTrue(false, "DANGER WILL ROBINSON! This should have produced an error!"); } catch (Exception $ex) { // Make sure it said something about a duplicate key, throw an error if not. $this->assertEquals(1, preg_match('~duplicate key~', $ex->getMessage()), "Error was strange: " . $ex->getMessage()); } $this->assertTrue($this->dbObj->commitTrans()); // make sure we're not in a transaction. $this->assertFalse($this->dbObj->get_transaction_status()); // Simpler test for farray() $numRows = $this->dbObj->run_query("SELECT * FROM test WHERE id > :id ORDER BY id", array('id' => 0)); $data = $this->dbObj->farray(); $this->assertTrue($numRows == count($data)); $this->assertTrue($numRows > 0); $this->assertTrue($numRows == 5); $this->assertEquals($data[0][0], 1); $this->assertEquals($data[0][1], 'test1'); $this->assertEquals($data[0]['id'], 1); $this->assertEquals($data[0]['data'], 'test1'); $testElement4 = array(0 => 5, 'id' => 5, 1 => 'test5', 'data' => 'test5'); $this->assertEquals($data[4], $testElement4); // use farray_nvp(), but swap id with value (should work, since values are unique) $numRows = $this->dbObj->run_query("SELECT * FROM test WHERE id > :id ORDER BY :orderBy", array('id' => 0, 'orderBy' => 'id')); $data = $this->dbObj->farray_nvp('data', 'id'); $this->assertEquals($numRows, count($data)); $this->assertEquals($numRows, 5); $this->assertEquals($data['test5'], 5); $this->assertEquals($data['test3'], 3); $numRows = $this->dbObj->run_query("SELECT * FROM test WHERE id=:id", array('id' => 2)); $data = $this->dbObj->get_single_record(); $this->assertEquals(array('id' => 2, 'data' => 'test2'), $data); }
/** * Retrieve category name from the given ID. */ protected function get_category_name($categoryId) { if (is_numeric($categoryId)) { $sql = "SELECT category_name FROM " . self::categoryTable . " WHERE category_id=:categoryId"; try { $this->db->run_query($sql, array('categoryId' => $categoryId)); $data = $this->db->get_single_record(); if (is_array($data) && isset($data['category_name']) && $this->db->numRows() == 1) { $categoryName = $data['category_name']; } else { throw new exception(__METHOD__ . ": failed to retrieve " . "category name::: " . ToolBox::debug_var_dump($data, 0)); } } catch (exception $e) { throw new exception(__METHOD__ . ": error encountered while " . "retrieving category name::: " . $e->getMessage()); } } else { throw new exception(__METHOD__ . ": invalid category ID (" . $categoryId . ")"); } return $categoryName; }