Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: Mock.php 7490 2010-03-29 19:53:27Z jwage $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.doctrine-project.org>.
20
 */
21
 
22
/**
23
 * Doctrine mock connection adapter. This class is used for special testing purposes.
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Adapter
27
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
29
 * @link        www.doctrine-project.org
30
 * @since       1.0
31
 * @version     $Revision: 7490 $
32
 */
33
class Doctrine_Adapter_Mock implements Doctrine_Adapter_Interface, Countable
34
{
35
    /**
36
     * Name of the dbms to mock
37
     *
38
     * @var string
39
     */
40
    private $_name;
41
 
42
    /**
43
     * Array of queries executed through this instance of the mock adapter
44
     *
45
     * @var array $queries
46
     */
47
    private $_queries = array();
48
 
49
    /**
50
     * Array of exceptions thrown
51
     *
52
     * @var array $exceptions
53
     */
54
    private $_exception = array();
55
 
56
    /**
57
     * Bool true/false variable for whether or not the last insert failed
58
     *
59
     * @var boolean $lastInsertIdFail
60
     */
61
    private $_lastInsertIdFail = false;
62
 
63
    /**
64
     * Doctrine mock adapter constructor
65
     *
66
     * <code>
67
     * $conn = new Doctrine_Adapter_Mock('mysql');
68
     * </code>
69
     *
70
     * @param string $name
71
     * @return void
72
     */
73
    public function __construct($name = null)
74
    {
75
        $this->_name = $name;
76
    }
77
 
78
    /**
79
     * Get the name of the dbms used in this instance of the mock adapter
80
     *
81
     * @return string $name Name of the dbms
82
     */
83
    public function getName()
84
    {
85
        return $this->_name;
86
    }
87
 
88
    /**
89
     * Pop the last executed query from the array of executed queries and return it
90
     *
91
     * @return string $sql Last executed sql string
92
     */
93
    public function pop()
94
    {
95
        return array_pop($this->_queries);
96
    }
97
 
98
    /**
99
     * Force an exception in to the array of exceptions
100
     *
101
     * @param string $name     Name of exception
102
     * @param string $message  Message for the exception
103
     * @param integer $code    Code of the exception
104
     * @return void
105
     */
106
    public function forceException($name, $message = '', $code = 0)
107
    {
108
        $this->_exception = array($name, $message, $code);
109
    }
110
 
111
    /**
112
     * Prepare a query statement
113
     *
114
     * @param string $query   Query to prepare
115
     * @return Doctrine_Adapter_Statement_Mock $mock Mock prepared statement
116
     */
117
    public function prepare($query)
118
    {
119
        $mock = new Doctrine_Adapter_Statement_Mock($this, $query);
120
        $mock->queryString = $query;
121
 
122
        return $mock;
123
    }
124
 
125
    /**
126
     * Add query to the stack of executed queries
127
     *
128
     * @param string $query
129
     * @return void
130
     */
131
    public function addQuery($query)
132
    {
133
        $this->_queries[] = $query;
134
    }
135
 
136
    /**
137
     * Fake the execution of query and add it to the stack of executed queries
138
     *
139
     * @param string $query
140
     * @return Doctrine_Adapter_Statement_Mock $stmt
141
     */
142
    public function query($query)
143
    {
144
        $this->_queries[] = $query;
145
 
146
        $e    = $this->_exception;
147
 
148
        if ( ! empty($e)) {
149
            $name = $e[0];
150
 
151
            $this->_exception = array();
152
 
153
            throw new $name($e[1], $e[2]);
154
        }
155
 
156
        $stmt = new Doctrine_Adapter_Statement_Mock($this, $query);
157
        $stmt->queryString = $query;
158
 
159
        return $stmt;
160
    }
161
 
162
    /**
163
     * Get all the executed queries
164
     *
165
     * @return array $queries Array of all executed queries
166
     */
167
    public function getAll()
168
    {
169
        return $this->_queries;
170
    }
171
 
172
    /**
173
     * Quote a value for the dbms
174
     *
175
     * @param string $input
176
     * @return string $quoted
177
     */
178
    public function quote($input)
179
    {
180
        return "'" . addslashes($input) . "'";
181
    }
182
 
183
    /**
184
     * Execute a raw sql statement
185
     *
186
     * @param string $statement
187
     * @return void
188
     */
189
    public function exec($statement)
190
    {
191
        $this->_queries[] = $statement;
192
 
193
        $e    = $this->_exception;
194
 
195
        if ( ! empty($e)) {
196
            $name = $e[0];
197
 
198
            $this->_exception = array();
199
 
200
            throw new $name($e[1], $e[2]);
201
        }
202
 
203
        return 0;
204
    }
205
 
206
    /**
207
     * Force last insert to be failed
208
     *
209
     * @param boolean $fail
210
     * @return void
211
     */
212
    public function forceLastInsertIdFail($fail = true)
213
    {
214
        if ($fail) {
215
            $this->_lastInsertIdFail = true;
216
        } else {
217
            $this->_lastInsertIdFail = false;
218
        }
219
    }
220
 
221
    /**
222
     * Get the id of the last inserted record
223
     *
224
     * @return integer $id
225
     */
226
    public function lastInsertId()
227
    {
228
        $this->_queries[] = 'LAST_INSERT_ID()';
229
        if ($this->_lastInsertIdFail) {
230
            return null;
231
        } else {
232
            return 1;
233
        }
234
    }
235
 
236
    /**
237
     * Get the number of queries executed
238
     *
239
     * @return integer $count
240
     */
241
    public function count()
242
    {
243
        return count($this->_queries);
244
    }
245
 
246
    /**
247
     * Begin a transaction
248
     *
249
     * @return void
250
     */
251
    public function beginTransaction()
252
    {
253
        $this->_queries[] = 'BEGIN TRANSACTION';
254
    }
255
 
256
    /**
257
     * Commit a transaction
258
     *
259
     * @return void
260
     */
261
    public function commit()
262
    {
263
        $this->_queries[] = 'COMMIT';
264
    }
265
 
266
    /**
267
     * Rollback a transaction
268
     *
269
     * @return void
270
     */
271
    public function rollBack()
272
    {
273
        $this->_queries[] = 'ROLLBACK';
274
    }
275
 
276
    public function getAttribute($attribute)
277
    {
278
        if ($attribute == Doctrine_Core::ATTR_DRIVER_NAME) {
279
            return strtolower($this->_name);
280
        }
281
    }
282
 
283
    public function errorCode()
284
    { }
285
 
286
    public function errorInfo()
287
    { }
288
 
289
    public function setAttribute($attribute, $value)
290
    { }
291
 
292
    public function sqliteCreateFunction()
293
    { }
294
}