php - Should I keep a similar tests for Unit Testing and DBUnit testing in a Data Mapper? -
i've been practicing , reading test driven development. i've been doing far since of straightforward have questions test classes such 1 below.
public function testpersonenrollmentdateisset() { //for sake of simplicity i've abstracted pdo/connection mocking $pdo = $this->getpdomock(); $pdostatement = $this->getpdostatementmock(); $pdo->method('prepare')->willreturn($pdostatement); $pdostatement->method('fetch')->willreturn('2000-01-01'); $accountmapper = $this->mapperfactory->build( 'accountmapper', array($pdo) ); $person = $this->entityfactory->build('person'); $account = $this->entityfactory->build('account'); $person->setaccount($account); $accountmapper->getaccountenrollmentdate($person); $this->assertequals( '2001-01-01', $person->getaccountenrollmentdate() ); }
i'm little unsure if should testing @ 2 reasons.
unit testing logic
in example above i'm testing if value mapped correctly. logic alone, mocking connection no database. awesome because can run test of exclusively business logic without other developers having install or configure database dependency.
dbunit testing result
however, separate configuration can run on demand test sql queries type of test altogether. while consolidating sql tests run separately unit tests.
the test same above except pdo
connection not mocked out , real database connection.
cause concern
i'm torn because although testing different things, it's duplicate code.
if rid of unit test, introduce required database dependency @ times. codebase grows, tests become more slow on time, no out-of-box testing; effort other developers set configuration.
if rid of database test can't assure sql return expected information.
my questions are:
is legitimate reason keep both tests?
is worth or possible may become maintenance nightmare?
according comments, seems want test pdo doing. (are prepare
, execute
etc ... being called , doing things expect them ?)
it's not want here unit testing. not want test pdo or other library, , not supposed test logic or behavior of pdo. sure these things have been done people in charge of pdo.
what want test correct execution of code , logic of code. want test getaccountexpirationdate
, , results expect it. not results expect pdo calls.
therefore, , if understand code right, have test expiration date of $person
give parameter has been set expected value $result
-> if didn't use pdo in right way, test fail anyway !
a correct test :
- create
usermapper
object , open db transaction - insert data in db retrieved in
$result
- create
person
object correcttaxid
,clientid
wanted$result
query - run
usermapper->getaccountexpirationdate($person)
- assert
$person->expirationdate
equals whatever settled expected$result
- rollback db transaction
you may check format of date, etc ... related logic implemented.
if going test such things in many tests, use setup
, teardown
init , rollback db after each test.
Comments
Post a Comment