However, I was wondering what the most efficient way is to do this kind of testing. First thing: should I use a helper method to insert dummy data (just as I have done with the table setup). I don't feel like repeating what I wrote in testInsertMultipleClimbs() again and again for all methods that follow.
Second, are there other ways to write these tests? I know setting up a clean db with each test (method) is the best, but it does seem a bit cumbersome.
Code: Select all
<?php
include('../MC/models/RouteClimbModel.php');
include('../lib/DB.php');
define('ROUTECLIMBS_TABLE_DDL', <<<EOS
CREATE TABLE `mc_routeclimbs` (
`climb_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`route_id` INT UNSIGNED NOT NULL,
`user_id` INT UNSIGNED NOT NULL,
`climb_date` DATE NOT NULL,
`climb_style` ENUM('OS','FL','RP') NOT NULL,
`climb_tries` SMALLINT UNSIGNED NOT NULL DEFAULT '1',
`climb_value` TINYINT UNSIGNED,
`climb_notes` TEXT NOT NULL,
`climb_dateposted` DATETIME NOT NULL,
PRIMARY KEY (`climb_id`)
)
EOS
);
class TestOfRouteClimbModel extends UnitTestCase {
protected $conn;
function __construct($name=''){
$this->UnitTestCase($name);
$this->conn = DB::conn();
}
function setup(){
$this->conn->query('drop table mc_routeclimbs');
$this->conn->query(ROUTECLIMBS_TABLE_DDL);
}
function testInsertNewRouteClimb(){
$climb = new RouteClimbModel;
$climb->route_id = '4';
$climb->user_id = '3';
$climb->climb_date = '2006-10-22';
$climb->climb_style = 'RP';
$climb->climb_tries = '4';
$climb->climb_value = '3';
$climb->climb_notes = 'Nice route!!';
$climb->save();
$this->assertEqual(1, $climb->getId());
$rs = $this->conn->query("select * from mc_routeclimbs");
$this->assertEqual(1, count($rs), 'returned 1 row');
}
function testInsertMultipleClimbs(){
$climb = new RouteClimbModel;
$climb->route_id = '4';
$climb->user_id = '3';
$climb->climb_date = '2006-10-22';
$climb->climb_style = 'RP';
$climb->climb_tries = '4';
$climb->climb_value = '3';
$climb->climb_notes = 'Nice route!!';
$climb->save();
$climb->route_id = '8';
$climb->user_id = '2';
$climb->climb_date = '2006-03-12';
$climb->climb_style = 'OS';
$climb->climb_tries = '1';
$climb->climb_value = '1';
$climb->climb_notes = 'very hard';
$climb->save();
$this->assertEqual(2, $climb->getId());
$rs = $this->conn->query("select * from mc_routeclimbs");
$this->assertEqual(1, count($rs), 'returned 1 row');
}
function testGetClimbsByUser(){
}
}