diff options
Diffstat (limited to 'test/test_classes.php')
-rw-r--r-- | test/test_classes.php | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/test/test_classes.php b/test/test_classes.php new file mode 100644 index 0000000..6af1a76 --- /dev/null +++ b/test/test_classes.php @@ -0,0 +1,170 @@ +<?php + + /** + * + * Mock version of the PDOStatement class. + * + */ + class MockPDOStatement extends PDOStatement { + + private $current_row = 0; + /** + * Return some dummy data + */ + public function fetch( + $fetch_style = PDO::FETCH_BOTH, + $cursor_orientation = PDO::FETCH_ORI_NEXT, + $cursor_offset = 0 + ) { + if ($this->current_row == 5) { + return false; + } else { + $this->current_row++; + return array('name' => 'Fred', 'age' => 10, 'id' => '1'); + } + } + } + + /** + * + * Mock database class implementing a subset + * of the PDO API. + * + */ + class MockPDO extends PDO { + + /** + * Return a dummy PDO statement + */ + public function prepare($statement, $driver_options=array()) { + $this->last_query = new MockPDOStatement($statement); + return $this->last_query; + } + } + + /** + * Another mock PDOStatement class, for testing multiple connections + */ + class MockDifferentPDOStatement extends PDOStatement { + + private $current_row = 0; + /** + * Return some dummy data + */ + public function fetch( + $fetch_style = PDO::FETCH_BOTH, + $cursor_orientation = PDO::FETCH_ORI_NEXT, + $cursor_offset = 0 + ) { + if ($this->current_row == 5) { + return false; + } else { + $this->current_row++; + return array('name' => 'Steve', 'age' => 80, 'id' => "{$this->current_row}"); + } + } + } + + /** + * A different mock database class, for testing multiple connections + * Mock database class implementing a subset of the PDO API. + */ + class MockDifferentPDO extends PDO { + + /** + * Return a dummy PDO statement + */ + public function prepare($statement, $driver_options = array()) { + $this->last_query = new MockDifferentPDOStatement($statement); + return $this->last_query; + } + } + + /** + * + * Class to provide simple testing functionality + * + */ + class Tester { + + private static $passed_tests = array(); + private static $failed_tests = array(); + private static $db; + + private static $term_colours = array( + 'BLACK' => "30", + 'RED' => "31", + 'GREEN' => "32", + 'DEFAULT' => "00", + ); + + /** + * Format a line for printing. Detects + * if the script is being run from the command + * line or from a browser. + * + * Colouring code loosely based on + * http://www.zend.com//code/codex.php?ozid=1112&single=1 + */ + private static function format_line($line, $colour='DEFAULT') { + if (isset($_SERVER['HTTP_USER_AGENT'])) { + $colour = strtolower($colour); + return "<p style=\"color: $colour;\">$line</p>\n"; + } else { + $colour = self::$term_colours[$colour]; + return chr(27) . "[0;{$colour}m{$line}" . chr(27) . "[00m\n"; + } + } + + /** + * Report a passed test + */ + public static function report_pass($test_name) { + echo self::format_line("PASS: $test_name", 'GREEN'); + self::$passed_tests[] = $test_name; + } + + /** + * Report a failed test + */ + public static function report_failure($test_name, $expected, $actual) { + echo self::format_line("FAIL: $test_name", 'RED'); + echo self::format_line("Expected: $expected", 'RED'); + echo self::format_line("Actual: $actual", 'RED'); + self::$failed_tests[] = $test_name; + } + + /** + * Print a summary of passed and failed test counts + */ + public static function report() { + $passed_count = count(self::$passed_tests); + $failed_count = count(self::$failed_tests); + echo self::format_line(''); + echo self::format_line("$passed_count tests passed. $failed_count tests failed."); + + if ($failed_count != 0) { + echo self::format_line("Failed tests: " . join(", ", self::$failed_tests)); + } + } + + /** + * Check the provided string is equal to the last + * query generated by the dummy database class. + */ + public static function check_equal_query($test_name, $query) { + $last_query = ORM::get_last_query(); + self::check_equal_string($test_name, $query, $last_query); + } + + /** + * Check the provided strings are equal + */ + public static function check_equal_string($test_name, $s1, $s2) { + if ($s1 === $s2) { + self::report_pass($test_name); + } else { + self::report_failure($test_name, $s1, $s2); + } + } + } |