summaryrefslogtreecommitdiff
path: root/test/test_classes.php
blob: 15f3c774f3f9f9230b86ce5108422a75567cedab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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);
            }
        }
    }