|
From: Jason S. <jsw...@ya...> - 2003-11-19 20:31:30
|
First of all, please do not mock me (pun intentional) for the use of a global.
It is force of habit and thus far I have failed to see the value of
implementing a singleton or passing around a tramp object or creating many
instances of the database connection when they all go to the same spot anyway.
Hrumph.
BTW: this was an attempt at test driven development, and it did already catch
some boneheadded coding mistakes :)
Here is the essence of the test case:
require_once '../models/ListOfValues.php';
define('TEST_LOV_LIST', 'TEST');
class TestListOfValues extends UnitTestCase
{
function TestListOfValues()
{
$this->UnitTestCase();
}
function SetUp()
{
Mock::Generate('ADOConnection');
Mock::Generate('ADORecordSet');
$GLOBALS['go_conn'] =& new MockADOConnection($this);
$this->lov =& new ListOfValues;
}
function TestGetValueSk()
{
//create a result set object
$o_rs =& new MockADORecordSet($this);
$s_ret = '1';
$o_rs->setReturnValue('FetchRow', array('SK' => $s_ret));
$o_rs->setReturnValue('RowCount', 1);
//have the global connection return it
global $go_conn;
$go_conn->SetReturnReference('Execute', $o_rs);
$a_bind = array(
'LIST' => TEST_LOV_LIST
,'CODE' => 'A'
);
$go_conn->ExpectOnce('Execute', array(LOV_SEL_SK, $a_bind));
//test
$this->AssertEqual($s_ret, $this->lov->GetValueSk(TEST_LOV_LIST, 'A',
'desc'));
$go_conn->Tally();
}
}
with relavant bits of the target code:
define('LOV_SEL_SK', <<<EOS
SELECT
*
FROM
cost_card_lov
WHERE
lov_attrbt_name = :LIST
AND lov_code = :CODE
EOS
);
class ListOfValues
{
var $_aoConn;
function ListOfValues()
{
global $go_conn;
$this->_aoConn =& $go_conn;
}
/**
*
* @param string psList the list identifier
* @param string psCode the code value
* @param string psDesc description the code value references
* @return integer surrogate key for the list value
*/
function GetValueSk($psList, $psCode, $psDesc)
{
$a_bind = array(
'LIST' => $psList
,'CODE' => $psCode
);
$o_rs =& $this->_aoConn->Execute(LOV_SEL_SK, $a_bind);
if ($o_rs) {
if (1 == $o_rs->RowCount()) {
$a_row = $o_rs->FetchRow();
return $a_row['SK'];
} else {
$this->_SetListVal($psList, $psCode, $psDesc);
return $this->GetValueSk($psList, $psCode, $psDesc);
}
} else {
trigger_error('Database Error: '.$go_conn->ErrorMsg());
}
}
}
The question I have it that at first blush, it looks like I am coding the
database code twice, once for the expectation, and again for the
implementation. Am I doing this wrong? Is there an easier approach?
Thanks.
Jason
__________________________________
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
http://antispam.yahoo.com/whatsnewfree
|