diff options
author | Jamie Matthews <[email protected]> | 2010-10-22 02:18:24 +0100 |
---|---|---|
committer | Jamie Matthews <[email protected]> | 2010-10-22 02:18:24 +0100 |
commit | 0a01caf89048cbac58b144029fab2465722cc59c (patch) | |
tree | 616cc2e9c225a587eb0c41baf658b52824e2d212 /test/test_classes.php | |
parent | f93ad0a9d59e076f654da32c628743ba67284e03 (diff) |
Simplify mock testing objects and convert query tests to use logged queries
Diffstat (limited to 'test/test_classes.php')
-rw-r--r-- | test/test_classes.php | 86 |
1 files changed, 15 insertions, 71 deletions
diff --git a/test/test_classes.php b/test/test_classes.php index 3a61773..2749f9d 100644 --- a/test/test_classes.php +++ b/test/test_classes.php @@ -2,28 +2,14 @@ /** * - * Mock version of the PDOStatement class. Can be - * used to get a string representing a "bound" version - * of the query. Because PDO works using prepared statements, - * this can provide only a rough representation of the - * query, but this will usually be enough to check that - * your query has been built as expected. + * Mock version of the PDOStatement class. * */ - class DummyPDOStatement { - - private $query = ''; - private $input_parameters = array(); - private $current_row = 1; - - public function __construct($statement) { - $this->query = $statement; - } - - public function execute($input_parameters=array()) { - $this->input_parameters = $input_parameters; - } + class DummyPDOStatement extends PDOStatement { + /** + * Return some dummy data + */ public function fetch($fetch_style) { if ($this->current_row == 5) { return false; @@ -32,27 +18,6 @@ return array('name' => 'Fred', 'age' => 10, 'id' => '1'); } } - - public function get_query() { - return $this->query; - } - - public function get_parameters() { - return $this->input_parameters; - } - - public function get_bound_query() { - $sql = $this->get_query(); - $sql = str_replace("?", "%s", $sql); - - $quoted_values = array(); - $values = $this->get_parameters(); - foreach ($values as $value) { - $quoted_values[] = '"' . $value . '"'; - } - return vsprintf($sql, $quoted_values); - } - } /** @@ -61,28 +26,15 @@ * of the PDO API. * */ - class DummyPDO { - - private $last_query; - - public function __construct($connection_string="") { - } - - public function setAttribute($attribute, $value) { - } + class DummyPDO extends PDO { + /** + * Return a dummy PDO statement + */ public function prepare($statement) { $this->last_query = new DummyPDOStatement($statement); return $this->last_query; } - - public function lastInsertId() { - return 0; - } - - public function get_last_query() { - return $this->last_query->get_bound_query(); - } } /** @@ -104,14 +56,6 @@ ); /** - * Set the dummy database connection to be - * used by the class to capture the SQL strings - */ - public static function set_db($db) { - self::$db = $db; - } - - /** * Format a line for printing. Detects * if the script is being run from the command * line or from a browser. @@ -140,10 +84,10 @@ /** * Report a failed test */ - private static function report_failure($test_name, $query) { + private static function report_failure($test_name, $expected, $actual) { echo self::format_line("FAIL: $test_name", 'RED'); - echo self::format_line("Expected: $query", 'RED'); - echo self::format_line("Actual: " . self::$db->get_last_query(), 'RED'); + echo self::format_line("Expected: $expected", 'RED'); + echo self::format_line("Actual: $actual", 'RED'); self::$failed_tests[] = $test_name; } @@ -166,11 +110,11 @@ * query generated by the dummy database class. */ public static function check_equal($test_name, $query) { - $last_query = self::$db->get_last_query(); - if ($query == self::$db->get_last_query()) { + $last_query = ORM::get_last_query(); + if ($query === $last_query) { self::report_pass($test_name); } else { - self::report_failure($test_name, $query); + self::report_failure($test_name, $query, $last_query); } } } |