DriverTestCase.php 5.38 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6
<?php
class AdapterMock implements Doctrine_Adapter_Interface {
    private $name;
    
    private $queries = array();
    
zYne's avatar
zYne committed
7
    private $exception = array();
zYne's avatar
zYne committed
8 9
    
    private $lastInsertIdFail = false;
zYne's avatar
zYne committed
10

zYne's avatar
zYne committed
11 12
    public function __construct($name) 
    {
zYne's avatar
zYne committed
13 14
        $this->name = $name;
    }
zYne's avatar
zYne committed
15 16
    public function getName() 
    {
zYne's avatar
zYne committed
17 18
        return $this->name;
    }
zYne's avatar
zYne committed
19 20
    public function pop() 
    {
zYne's avatar
zYne committed
21 22
        return array_pop($this->queries);
    }
zYne's avatar
zYne committed
23 24
    public function forceException($name, $message = '', $code = 0) 
    {
zYne's avatar
zYne committed
25 26
        $this->exception = array($name, $message, $code);
    }
zYne's avatar
zYne committed
27 28 29 30 31 32 33
    public function prepare($query)
    {
        return new AdapterStatementMock($this, $query);
    }
    public function addQuery($query)
    {
        $this->queries[] = $query;
zYne's avatar
zYne committed
34
    }
zYne's avatar
zYne committed
35 36
    public function query($query) {
        $this->queries[] = $query;
zYne's avatar
zYne committed
37 38 39 40 41 42 43 44 45 46 47

        $e    = $this->exception;

        if( ! empty($e)) {
            $name = $e[0];

            $this->exception = array();

            throw new $name($e[1], $e[2]);
        }

zYne's avatar
zYne committed
48
        return new AdapterStatementMock($this, $query);
zYne's avatar
zYne committed
49
    }
50 51 52
    public function getAll() {
        return $this->queries;
    }
53 54 55
    public function quote($input) {
        return "'" . addslashes($input) . "'";
    }
zYne's avatar
zYne committed
56 57
    public function exec($statement) { 
        $this->queries[] = $statement;
zYne's avatar
zYne committed
58 59 60 61 62 63 64 65 66 67 68

        $e    = $this->exception;

        if( ! empty($e)) {
            $name = $e[0];

            $this->exception = array();

            throw new $name($e[1], $e[2]);
        }

zYne's avatar
zYne committed
69 70
        return 0;
    }
71 72 73 74 75
    public function forceLastInsertIdFail($fail = true) 
    {
        if ($fail) {
            $this->lastInsertIdFail = true;
        } else {
zYne's avatar
zYne committed
76
            $this->lastInsertIdFail = false;
77
        }
zYne's avatar
zYne committed
78
    }
zYne's avatar
zYne committed
79 80
    public function lastInsertId()
    {
81 82
        $this->queries[] = 'LAST_INSERT_ID()';
        if ($this->lastInsertIdFail) {
zYne's avatar
zYne committed
83
            return null;
84
        } else {
zYne's avatar
zYne committed
85 86
            return 1;
        }
zYne's avatar
zYne committed
87
    }
zYne's avatar
zYne committed
88 89 90 91 92 93
    public function beginTransaction(){ 
        $this->queries[] = 'BEGIN TRANSACTION';
    }
    public function commit(){ 
        $this->queries[] = 'COMMIT';
    }
zYne's avatar
zYne committed
94 95 96 97
    public function rollBack() 
    { 
        $this->queries[] = 'ROLLBACK';
    }
zYne's avatar
zYne committed
98 99
    public function errorCode(){ }
    public function errorInfo(){ }
zYne's avatar
zYne committed
100
    public function getAttribute($attribute) {
zYne's avatar
zYne committed
101
        if($attribute == PDO::ATTR_DRIVER_NAME)
zYne's avatar
zYne committed
102
            return strtolower($this->name);
zYne's avatar
zYne committed
103 104
    }
    public function setAttribute($attribute, $value) {
105
                                       
zYne's avatar
zYne committed
106 107 108
    }
}
class AdapterStatementMock {
zYne's avatar
zYne committed
109 110 111 112 113 114 115 116 117
    
    private $mock;
    
    private $query;

    public function __construct(AdapterMock $mock, $query) {
        $this->mock  = $mock;
        $this->query = $query;
    }
zYne's avatar
zYne committed
118 119 120 121 122 123
    public function fetch($fetchMode) { 
        return array();
    }
    public function fetchAll($fetchMode) {
        return array();
    }
zYne's avatar
zYne committed
124
    public function execute() {
zYne's avatar
zYne committed
125
        $this->mock->addQuery($this->query);
zYne's avatar
zYne committed
126 127
        return true;
    }
zYne's avatar
zYne committed
128
    public function fetchColumn($colnum = 0) {
zYne's avatar
zYne committed
129 130
        return 0;
    }
zYne's avatar
zYne committed
131
}
zYne's avatar
zYne committed
132

zYne's avatar
zYne committed
133 134 135 136 137 138 139 140 141 142 143
class Doctrine_Driver_UnitTestCase extends UnitTestCase {
    protected $driverName = false;
    protected $generic = false;
    protected $manager;
    protected $conn;
    protected $adapter;
    protected $export;
    protected $dataDict;
    protected $transaction;

    public function __construct($driverName, $generic = false) {
zYne's avatar
zYne committed
144

zYne's avatar
zYne committed
145 146 147
        $this->driverName = $driverName;
        $this->generic    = $generic;
    }
zYne's avatar
zYne committed
148 149 150 151 152 153 154
    public function assertDeclarationType($type, $type2) {
        $dec = $this->getDeclaration($type);
        if( ! is_array($type2))
            $type2 = array($type2);
        $this->assertEqual($dec[0], $type2);
    }
    public function getDeclaration($type) {
155
        return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
zYne's avatar
zYne committed
156
    }
zYne's avatar
zYne committed
157 158 159
    public function setDriverName($driverName) {
        $this->driverName = $driverName;
    }
zYne's avatar
zYne committed
160 161 162 163 164
    public function init() {
        $this->adapter = new AdapterMock($this->driverName);
        $this->manager = Doctrine_Manager::getInstance();
        $this->manager->setDefaultAttributes();
        $this->conn = $this->manager->openConnection($this->adapter);
zYne's avatar
zYne committed
165

zYne's avatar
zYne committed
166 167 168
        if( ! $this->generic) {
            $this->export   = $this->conn->export;

zYne's avatar
zYne committed
169 170
            $name = $this->adapter->getName();

zYne's avatar
zYne committed
171
            if($this->adapter->getName() == 'oci')
zYne's avatar
zYne committed
172 173 174 175
                $name = 'Oracle';
            
            $tx = 'Doctrine_Transaction_' . ucwords($name);
            $dataDict = 'Doctrine_DataDict_' . ucwords($name);
zYne's avatar
zYne committed
176 177 178 179
            
            $exc  = 'Doctrine_Connection_' . ucwords($name) . '_Exception';
            
            $this->exc = new $exc();
zYne's avatar
zYne committed
180
            if(class_exists($tx))
zYne's avatar
zYne committed
181 182 183
                $this->transaction = new $tx($this->conn);
            if(class_exists($dataDict)) {
                $this->dataDict = new $dataDict($this->conn);
zYne's avatar
zYne committed
184
            }
zYne's avatar
zYne committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
            //$this->dataDict = $this->conn->dataDict;
        } else {
            $this->export   = new Doctrine_Export($this->conn);
            $this->transaction = new Doctrine_Transaction($this->conn);
        }
    }

    public function setUp() {
        static $init = false;
        if( ! $init) {
            $this->init();
            $init = true;
        }
    }
}