# Coverage for Doctrine_Adapter_Mock

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Mock.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
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.phpdoctrine.org>.
20 | */
21 |
22 | /**
23 | * Doctrine_Adapter_Mock
24 | * This class is used for special testing purposes.
25 | *
26 | * @package Doctrine
27 | * @subpackage Adapter
28 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
30 | * @link www.phpdoctrine.org
31 | * @since 1.0
32 | * @version $Revision: 2963 $
33 | */
34 | class Doctrine_Adapter_Mock implements Doctrine_Adapter_Interface, Countable
35 | {
36 | private $name;
37 |
38 | private $queries = array();
39 |
40 | private $exception = array();
41 |
42 | private $lastInsertIdFail = false;
43 |
44 | public function __construct($name = null)
45 | {
46 | $this->name = $name;
47 | }
48 | public function getName()
49 | {
50 | return $this->name;
51 | }
52 | public function pop()
53 | {
54 | return array_pop($this->queries);
55 | }
56 | public function forceException($name, $message = '', $code = 0)
57 | {
58 | $this->exception = array($name, $message, $code);
59 | }
60 | public function prepare($query)
61 | {
62 | $mock = new Doctrine_Adapter_Statement_Mock($this, $query);
63 | $mock->queryString = $query;
64 |
65 | return $mock;
66 | }
67 | public function addQuery($query)
68 | {
69 | $this->queries[] = $query;
70 | }
71 | public function query($query)
72 | {
73 | $this->queries[] = $query;
74 |
75 | $e = $this->exception;
76 |
77 | if ( ! empty($e)) {
78 | $name = $e[0];
79 |
80 | $this->exception = array();
81 |
82 | throw new $name($e[1], $e[2]);
83 | }
84 |
85 | $stmt = new Doctrine_Adapter_Statement_Mock($this, $query);
86 | $stmt->queryString = $query;
87 |
88 | return $stmt;
89 | }
90 | public function getAll()
91 | {
92 | return $this->queries;
93 | }
94 | public function quote($input)
95 | {
96 | return "'" . addslashes($input) . "'";
97 | }
98 | public function exec($statement)
99 | {
100 | $this->queries[] = $statement;
101 |
102 | $e = $this->exception;
103 |
104 | if ( ! empty($e)) {
105 | $name = $e[0];
106 |
107 | $this->exception = array();
108 |
109 | throw new $name($e[1], $e[2]);
110 | }
111 |
112 | return 0;
113 | }
114 | public function forceLastInsertIdFail($fail = true)
115 | {
116 | if ($fail) {
117 | $this->lastInsertIdFail = true;
118 | } else {
119 | $this->lastInsertIdFail = false;
120 | }
121 | }
122 | public function lastInsertId()
123 | {
124 | $this->queries[] = 'LAST_INSERT_ID()';
125 | if ($this->lastInsertIdFail) {
126 | return null;
127 | } else {
128 | return 1;
129 | }
130 | }
131 | public function count()
132 | {
133 | return count($this->queries);
134 | }
135 | public function beginTransaction()
136 | {
137 | $this->queries[] = 'BEGIN TRANSACTION';
138 | }
139 | public function commit()
140 | {
141 | $this->queries[] = 'COMMIT';
142 | }
143 | public function rollBack()
144 | {
145 | $this->queries[] = 'ROLLBACK';
146 | }
147 | public function errorCode()
148 | { }
149 | public function errorInfo()
150 | { }
151 | public function getAttribute($attribute)
152 | {
153 | if ($attribute == Doctrine::ATTR_DRIVER_NAME)
154 | return strtolower($this->name);
155 | }
156 | public function setAttribute($attribute, $value)
157 | {
158 |
159 | }
160 | public function sqliteCreateFunction()
161 | { }
162 | }