Completed
Push — master ( ef5eaa...6d70d2 )
by Lars
03:21 queued 30s
created

MockPDO   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 10
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
wmc 1
lcom 1
cbo 1
dl 0
loc 10
ccs 3
cts 3
cp 1
rs 10
1
<?php
2
3
require_once dirname(__FILE__) . '/../vendor/autoload.php';
4
5
/**
6
 *
7
 * Mock version of the PDOStatement class.
8
 *
9
 */
10
class MockPDOStatement extends PDOStatement {
11
   private $current_row = 0;
12
   private $statement = NULL;
13
   private $bindParams = array();
14
   
15
   /**
16
    * Store the statement that gets passed to the constructor
17
    */
18
   public function __construct($statement) {
19
       $this->statement = $statement;
20
   }
21
22
   /**
23
    * Check that the array
24
    */
25
   public function execute($params = NULL) {
26
       $count = 0;
27
       $m = array();
28
       if (is_null($params)) $params = $this->bindParams;
29
       if (preg_match_all('/"[^"\\\\]*(?:\\?)[^"\\\\]*"|\'[^\'\\\\]*(?:\\?)[^\'\\\\]*\'|(\\?)/', $this->statement, $m, PREG_SET_ORDER)) {
30
           $count = count($m);
31
           for ($v = 0; $v < $count; $v++) {
32
               if (count($m[$v]) == 1) unset($m[$v]);
33
           }
34
           $count = count($m);
35
           for ($i = 0; $i < $count; $i++) {
36
               if (!isset($params[$i])) {
37
                   ob_start();
38
                   var_dump($m, $params);
39
                   $output = ob_get_clean();
40
                   throw new Exception('Incorrect parameter count. Expected ' . $count . ' got ' . count($params) . ".\n" . $this->statement . "\n" . $output);
41
               }
42
           }
43
       }
44
   }
45
46
   /**
47
    * Add data to arrays
48
    */
49
   public function bindParam($index, $value, $type)
50
   {
51
       // Do check on index, and type
52
       if (!is_int($index)) throw new Exception('Incorrect parameter type. Expected $index to be an integer.');
53
       if (!is_int($type) || ($type != PDO::PARAM_STR && $type != PDO::PARAM_NULL && $type != PDO::PARAM_BOOL && $type != PDO::PARAM_INT))
54
           throw new Exception('Incorrect parameter type. Expected $type to be an integer.');
55
56
       // Add param to array
57
       $this->bindParams[$index - 1] = $value;
58
   }
59
   
60
   /**
61
    * Return some dummy data
62
    */
63
   public function fetch($fetch_style=PDO::FETCH_BOTH, $cursor_orientation=PDO::FETCH_ORI_NEXT, $cursor_offset=0) {
64
       if ($this->current_row == 5) {
65
           return false;
66
       } else {
67
           return array('name' => 'Fred', 'age' => 10, 'id' => ++$this->current_row);
68
       }
69
   }
70
}
71
72
/**
73
 * Another mock PDOStatement class, used for testing multiple connections
74
 */
75
class MockDifferentPDOStatement extends MockPDOStatement { }
76
77
/**
78
 *
79
 * Mock database class implementing a subset
80
 * of the PDO API.
81
 *
82
 */
83
class MockPDO extends PDO {
84
   
85
   /**
86
    * Return a dummy PDO statement
87
    */
88
   public function prepare($statement, $driver_options=array()) {
89
       $this->last_query = new MockPDOStatement($statement);
90
       return $this->last_query;
91
   }
92
}
93
94
/**
95
 * A different mock database class, for testing multiple connections
96
 * Mock database class implementing a subset of the PDO API.
97
 */
98
class MockDifferentPDO extends MockPDO {
99
100
    /**
101
     * Return a dummy PDO statement
102
     */
103
    public function prepare($statement, $driver_options = array()) {
104
        $this->last_query = new MockDifferentPDOStatement($statement);
105
        return $this->last_query;
106
    }
107
}
108
109
class MockMsSqlPDO extends MockPDO {
110
111
   public $fake_driver = 'mssql';
112
113
   /**
114
    * If we are asking for the name of the driver, check if a fake one
115
    * has been set.
116
    */
117
    public function getAttribute($attribute) {
118
        if ($attribute == self::ATTR_DRIVER_NAME) {
119
            if (!is_null($this->fake_driver)) {
120
                return $this->fake_driver;
121
            }
122
        }
123
        
124
        return parent::getAttribute($attribute);
125
    }
126
    
127
}
128