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