diff options
author | Jamie Matthews <[email protected]> | 2010-02-13 02:59:59 +0000 |
---|---|---|
committer | Jamie Matthews <[email protected]> | 2010-02-13 02:59:59 +0000 |
commit | 4973256bb5a2d280619025aabe4f0c045bbe6e38 (patch) | |
tree | 0ad3351c67d155ca61c4086464e8a7665fff9222 | |
parent | 3a4226da73bf0b4a375d003616d94e119056fff9 (diff) |
Initial commit of tests
-rw-r--r-- | test/test_classes.php | 122 | ||||
-rw-r--r-- | test/test_queries.php | 74 |
2 files changed, 196 insertions, 0 deletions
diff --git a/test/test_classes.php b/test/test_classes.php new file mode 100644 index 0000000..15f3c77 --- /dev/null +++ b/test/test_classes.php @@ -0,0 +1,122 @@ +<?php + + /** + * + * Mock database classes implementing a subset + * of the PDO API. Used for testing Idiorm. + * + */ + + class DummyStatement { + + 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; + } + + public function fetch($fetch_style) { + if ($this->current_row == 5) { + return false; + } else { + $this->current_row++; + 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); + } + + } + + class DummyPDO { + + private $last_query; + + public function __construct($connection_string="") { + } + + public function setAttribute($attribute, $value) { + } + + public function prepare($statement) { + $this->last_query = new DummyStatement($statement); + return $this->last_query; + } + + public function lastInsertId() { + return 0; + } + + public function get_last_query() { + return $this->last_query->get_bound_query(); + } + } + + /** + * Class to provide simple testing functionality + */ + class Tester { + + private static $passed_tests = array(); + private static $failed_tests = array(); + private static $db; + + public static function set_db($db) { + self::$db = $db; + } + + private static function report_pass($test_name) { + echo "<p>PASS: $test_name</p>"; + self::$passed_tests[] = $test_name; + } + + private static function report_failure($test_name, $query) { + echo "<p>FAIL: $test_name</p>"; + echo "<p>Expected: $query</p>"; + echo "<p>Actual: " . self::$db->get_last_query() . "</p>"; + self::$failed_tests[] = $test_name; + } + + public static function report() { + $passed_count = count(self::$passed_tests); + $failed_count = count(self::$failed_tests); + echo "<p>$passed_count tests passed. $failed_count tests failed.</p>"; + + if ($failed_count != 0) { + echo "<p>Failed tests: " . join(", ", self::$failed_tests) . "</p>"; + } + } + + public static function check_equal($test_name, $query) { + $last_query = self::$db->get_last_query(); + if ($query == self::$db->get_last_query()) { + self::report_pass($test_name); + } else { + self::report_failure($test_name, $query); + } + } + } diff --git a/test/test_queries.php b/test/test_queries.php new file mode 100644 index 0000000..de85023 --- /dev/null +++ b/test/test_queries.php @@ -0,0 +1,74 @@ +<?php + // Basic testing for Idiorm + + require_once "../idiorm.php"; + require_once "test_classes.php"; + + // Set up the dummy database connection + $db = new DummyPDO(); + ORM::set_db($db); + Tester::set_db($db); + + ORM::for_table('widget')->find_many(); + $expected = 'SELECT * FROM widget'; + Tester::check_equal("Basic unfiltered query", $expected); + + ORM::for_table('widget')->find_one(5); + $expected = 'SELECT * FROM widget WHERE id = "5"'; + Tester::check_equal("Filtering on ID", $expected); + + ORM::for_table('widget')->where('name', 'Fred')->find_one(); + $expected = 'SELECT * FROM widget WHERE name = "Fred"'; + Tester::check_equal("Single where clause", $expected); + + ORM::for_table('widget')->where('name', 'Fred')->where('age', 10)->find_one(); + $expected = 'SELECT * FROM widget WHERE name = "Fred" AND age = "10"'; + Tester::check_equal("Multiple WHERE clauses", $expected); + + ORM::for_table('widget')->limit(5)->find_one(); + $expected = 'SELECT * FROM widget LIMIT "5"'; + Tester::check_equal("LIMIT clause", $expected); + + ORM::for_table('widget')->limit(5)->offset(5)->find_one(); + $expected = 'SELECT * FROM widget LIMIT "5" OFFSET "5"'; + Tester::check_equal("LIMIT and OFFSET clause", $expected); + + ORM::for_table('widget')->order_by_desc('name')->find_one(); + $expected = 'SELECT * FROM widget ORDER BY "name" DESC'; + Tester::check_equal("ORDER BY DESC", $expected); + + ORM::for_table('widget')->order_by_asc('name')->find_one(); + $expected = 'SELECT * FROM widget ORDER BY "name" ASC'; + Tester::check_equal("ORDER BY ASC", $expected); + + ORM::for_table('widget')->order_by_asc('name')->order_by_desc('age')->find_one(); + $expected = 'SELECT * FROM widget ORDER BY "name" ASC, "age" DESC'; + Tester::check_equal("Multiple ORDER BY", $expected); + + ORM::for_table('widget')->where('name', 'Fred')->limit(5)->offset(5)->order_by_asc('name')->find_many(); + $expected = 'SELECT * FROM widget WHERE name = "Fred" LIMIT "5" OFFSET "5" ORDER BY "name" ASC'; + Tester::check_equal("Complex query", $expected); + + $widget = ORM::for_table('widget')->create(); + $widget->name = "Fred"; + $widget->age = 10; + $widget->save(); + $expected = 'INSERT INTO widget (name, age) VALUES ("Fred", "10")'; + Tester::check_equal("Insert data", $expected); + + $widget = ORM::for_table('widget')->find_one(1); + $widget->name = "Fred"; + $widget->age = 10; + $widget->save(); + $expected = 'UPDATE widget SET name = "Fred", age = "10" WHERE id = "1"'; + Tester::check_equal("Update data", $expected); + + $widget = ORM::for_table('widget')->find_one(1); + $widget->delete(); + $expected = 'DELETE FROM widget WHERE id = "1"'; + Tester::check_equal("Delete data", $expected); + + + + Tester::report(); +?> |