|
From: Jason S. <jsw...@ya...> - 2003-11-23 06:28:36
|
Have to problems still getting at me. The first I solved by adding a method to
the SimpleMock class. It may be that I still don't know how to use them, but
here is the method and my reason for adding it:
/**
* Resets the Mock to an initial state
* All returns and expectations are cleared
* @public
* @return void
*/
function reset() {
$this->_returns = array();
$this->_return_sequence = array();
$this->_expected_counts = array();
$this->_max_counts = array();
$this->_min_counts = array();
$this->_expected_args = array();
$this->_args_sequence = array();
$this->clearHistory();
}
I am using the mock to simulate a database connection. There is some
initialization that I have to go through when I create the object I am testing.
I do not want to stub in these expectations and results at each step, so
instead of always creating new objects to test, I decided to create a member of
the test case and set it to an instance of the class I want to test. After
that, I want to continue to use the same Mock object to run further tests, but
it always wanted to remember the first tests. My hack was to add the reset()
method. Not sure how you would feel about adding that to the real project.
My second problem may just be a pure PHP reference problem, but I can't seem to
solve it. I want to test a database based DAO for my project. I had a factory
method that is passing in the database connection object by reference to the
constructor function for my db DAO object. The DAO's constructs sets a member
var _by reference_ to the connection object. In my test case, I then wanted to
create the Mock connection. set it _by ref_ to a member var of the test case,
and then pass the member var as the connection parameter to the DAO being
tested.
My though process was then that I could continue to manipulate the member var
for the connection in the unit test case, and this would be the exact same
object that was the connection for the DAO being tested. Somewhere along the
way I am loosing the reference because the only way I can get things to work is
to reference the private var for the connection of the object being tested.
Here is the relevant parts of my test code:
function SetUp() {
require_once DATACACHE_ADODB_LIB_PATH.'adodb.inc.php';
Mock::Generate('ADOConnection');
Mock::Generate('ADORecordSet');
$this->_moConn =& new MockADOConnection($this);
$o_rs_appl =& new MockADORecordSet($this);
$o_rs_appl->SetReturnValueAt(0, 'FetchRow', array('get_appl_id' =>
TEST_DAOPGDB_APPL_ID));
$this->_moConn->SetReturnReferenceAt(0, 'Execute', $o_rs_appl);
$this->_moConn->ExpectArgumentsAt(0, 'Execute'
,array(DATACACHE_DAOPGDB_SEL_APPL_ID
,array(TEST_APPLICATION)));
}
function TestInstanciate() {
$this->_moCache =& new DataCacheDaoPgDb(TEST_APPLICATION, $this->_moConn);
$this->AssertNoErrors();
$this->_moConn->Tally();
}
function TestEmptyCache() {
//$o_conn =& new MockADOConnection($this);
//$o_conn =& $this->_moConn;
$o_conn =& $this->_moCache->_moConn;
$o_test =& $this->_moCache;
$id = TEST_DAOPGDB_BLANK;
$o_rs =& new MockADORecordSet($this);
$o_rs->SetReturnValueAt(0, 'FetchRow', array('is_cached' => '0'));
// $o_conn->ClearHistory();
$o_conn->Reset();
$o_conn->SetReturnReferenceAt(0, 'Execute', $o_rs);
$o_conn->ExpectCallCount('Execute', 1);
$o_conn->ExpectArgumentsAt(0, 'Execute'
,array(DATACACHE_DAOPGDB_CHK_DATA
,array(TEST_DAOPGDB_APPL_ID, $id)));
//$this->_moConn =& $o_conn;
//$o_test->_moConn =& $o_conn;
$this->AssertFalse($o_test->IsCached($id));
$o_conn->Tally();
}
You can see that TestEmptyCache() is the test case I am fighting, with the
commented out line being some of the variations I have worked with.
And here is the relevant parts of the code it is testing:
class DataCacheDaoPgDb
{
function DataCacheDaoPgDb($psAppl, &$poConn)
{
$this->_moConn =& $poConn;
$this->_miAppl = $this->_GetApplId($psAppl);
}
function _GetApplId($psAppl)
{
$o_rs =& $this->_moConn->Execute(DATACACHE_DAOPGDB_SEL_APPL_ID,
array($psAppl));
if ($o_rs) {
$a_row = $o_rs->FetchRow();
return (int)$a_row['get_appl_id'];
} else {
trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
return false;
}
}
function IsCached($psId)
{
$o_rs =& $this->_moConn->Execute(DATACACHE_DAOPGDB_CHK_DATA,
array($this->_miAppl, $psId));
if ($o_rs) {
$a_row = $o_rs->FetchRow();
return ('1' == $a_row['is_cached']) ? true : false;
} else {
trigger_error('Database Error: '.$this->_moConn->ErrorMsg());
return false;
}
}
Can you see some mistake I am making with the references? Or perhaps with the
approach?
Thanks.
__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/
|