3
* $Id: Mock.php 4520 2008-06-13 22:29:22Z jwage $
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.
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.phpdoctrine.org>.
23
* Doctrine mock connection adapter. This class is used for special testing purposes.
27
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
28
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29
* @link www.phpdoctrine.org
31
* @version $Revision: 4520 $
33
class Doctrine_Adapter_Mock implements Doctrine_Adapter_Interface, Countable
36
* Name of the dbms to mock
43
* Array of queries executed through this instance of the mock adapter
47
private $_queries = array();
50
* Array of exceptions thrown
52
* @var array $exceptions
54
private $_exception = array();
57
* Bool true/false variable for whether or not the last insert failed
59
* @var boolean $lastInsertIdFail
61
private $_lastInsertIdFail = false;
64
* Doctrine mock adapter constructor
67
* $conn = new Doctrine_Adapter_Mock('mysql');
73
public function __construct($name = null)
79
* Get the name of the dbms used in this instance of the mock adapter
81
* @return string $name Name of the dbms
83
public function getName()
89
* Pop the last executed query from the array of executed queries and return it
91
* @return string $sql Last executed sql string
95
return array_pop($this->_queries);
99
* Force an exception in to the array of exceptions
101
* @param string $name Name of exception
102
* @param string $message Message for the exception
103
* @param integer $code Code of the exception
106
public function forceException($name, $message = '', $code = 0)
108
$this->_exception = array($name, $message, $code);
112
* Prepare a query statement
114
* @param string $query Query to prepare
115
* @return Doctrine_Adapter_Statement_Mock $mock Mock prepared statement
117
public function prepare($query)
119
$mock = new Doctrine_Adapter_Statement_Mock($this, $query);
120
$mock->queryString = $query;
126
* Add query to the stack of executed queries
128
* @param string $query
131
public function addQuery($query)
133
$this->_queries[] = $query;
137
* Fake the execution of query and add it to the stack of executed queries
139
* @param string $query
140
* @return Doctrine_Adapter_Statement_Mock $stmt
142
public function query($query)
144
$this->_queries[] = $query;
146
$e = $this->_exception;
151
$this->_exception = array();
153
throw new $name($e[1], $e[2]);
156
$stmt = new Doctrine_Adapter_Statement_Mock($this, $query);
157
$stmt->queryString = $query;
163
* Get all the executed queries
165
* @return array $queries Array of all executed queries
167
public function getAll()
169
return $this->_queries;
173
* Quote a value for the dbms
175
* @param string $input
176
* @return string $quoted
178
public function quote($input)
180
return "'" . addslashes($input) . "'";
184
* Execute a raw sql statement
186
* @param string $statement
189
public function exec($statement)
191
$this->_queries[] = $statement;
193
$e = $this->_exception;
198
$this->_exception = array();
200
throw new $name($e[1], $e[2]);
207
* Force last insert to be failed
209
* @param boolean $fail
212
public function forceLastInsertIdFail($fail = true)
215
$this->_lastInsertIdFail = true;
217
$this->_lastInsertIdFail = false;
222
* Get the id of the last inserted record
224
* @return integer $id
226
public function lastInsertId()
228
$this->_queries[] = 'LAST_INSERT_ID()';
229
if ($this->_lastInsertIdFail) {
237
* Get the number of queries executed
239
* @return integer $count
241
public function count()
243
return count($this->_queries);
247
* Begin a transaction
251
public function beginTransaction()
253
$this->_queries[] = 'BEGIN TRANSACTION';
257
* Commit a transaction
261
public function commit()
263
$this->_queries[] = 'COMMIT';
267
* Rollback a transaction
271
public function rollBack()
273
$this->_queries[] = 'ROLLBACK';
276
public function getAttribute($attribute)
278
if ($attribute == Doctrine::ATTR_DRIVER_NAME) {
279
return strtolower($this->_name);
283
public function errorCode()
286
public function errorInfo()
289
public function setAttribute($attribute, $value)
292
public function sqliteCreateFunction()
b'\\ No newline at end of file'