1
0
mirror of synced 2024-12-13 06:46:03 +03:00
doctrine2/tests_old/DriverTestCase.php

200 lines
5.4 KiB
PHP
Raw Normal View History

2006-11-23 02:35:34 +03:00
<?php
class AdapterMock implements Doctrine_Adapter_Interface {
private $name;
private $queries = array();
2006-12-22 01:06:08 +03:00
private $exception = array();
2007-01-15 21:48:50 +03:00
private $lastInsertIdFail = false;
2006-11-23 02:35:34 +03:00
2007-01-15 21:48:50 +03:00
public function __construct($name)
{
2006-11-23 02:35:34 +03:00
$this->name = $name;
}
2007-01-15 21:48:50 +03:00
public function getName()
{
2006-11-23 02:35:34 +03:00
return $this->name;
}
2007-01-15 21:48:50 +03:00
public function pop()
{
2006-11-23 02:35:34 +03:00
return array_pop($this->queries);
}
2007-01-15 21:48:50 +03:00
public function forceException($name, $message = '', $code = 0)
{
2006-12-22 01:06:08 +03:00
$this->exception = array($name, $message, $code);
}
2007-01-15 21:48:50 +03:00
public function prepare($query)
{
return new AdapterStatementMock($this, $query);
}
public function addQuery($query)
{
$this->queries[] = $query;
2006-11-23 02:35:34 +03:00
}
2007-01-15 21:48:50 +03:00
public function query($query) {
$this->queries[] = $query;
2006-12-22 01:06:08 +03:00
$e = $this->exception;
if( ! empty($e)) {
$name = $e[0];
$this->exception = array();
throw new $name($e[1], $e[2]);
}
2007-01-15 21:48:50 +03:00
return new AdapterStatementMock($this, $query);
2006-11-23 02:35:34 +03:00
}
public function getAll() {
return $this->queries;
}
public function quote($input) {
return "'" . addslashes($input) . "'";
}
2006-11-23 02:35:34 +03:00
public function exec($statement) {
$this->queries[] = $statement;
2006-12-22 01:06:08 +03:00
$e = $this->exception;
if( ! empty($e)) {
$name = $e[0];
$this->exception = array();
throw new $name($e[1], $e[2]);
}
2006-11-23 02:35:34 +03:00
return 0;
}
public function forceLastInsertIdFail($fail = true)
{
if ($fail) {
$this->lastInsertIdFail = true;
} else {
2007-01-25 01:50:49 +03:00
$this->lastInsertIdFail = false;
}
2007-01-15 21:48:50 +03:00
}
2007-01-08 02:52:15 +03:00
public function lastInsertId()
{
$this->queries[] = 'LAST_INSERT_ID()';
if ($this->lastInsertIdFail) {
2007-01-15 21:48:50 +03:00
return null;
} else {
2007-01-15 21:48:50 +03:00
return 1;
}
2007-01-08 02:52:15 +03:00
}
2006-11-23 02:35:34 +03:00
public function beginTransaction(){
$this->queries[] = 'BEGIN TRANSACTION';
}
public function commit(){
$this->queries[] = 'COMMIT';
}
2007-01-15 21:48:50 +03:00
public function rollBack()
{
$this->queries[] = 'ROLLBACK';
}
2006-11-23 02:35:34 +03:00
public function errorCode(){ }
public function errorInfo(){ }
2007-01-08 02:52:15 +03:00
public function getAttribute($attribute) {
2006-11-23 02:35:34 +03:00
if($attribute == PDO::ATTR_DRIVER_NAME)
2006-12-28 00:20:26 +03:00
return strtolower($this->name);
2006-11-23 02:35:34 +03:00
}
public function setAttribute($attribute, $value) {
2006-11-23 02:35:34 +03:00
}
}
class AdapterStatementMock {
2007-01-15 21:48:50 +03:00
private $mock;
private $query;
public function __construct(AdapterMock $mock, $query) {
$this->mock = $mock;
$this->query = $query;
}
2006-11-23 02:35:34 +03:00
public function fetch($fetchMode) {
return array();
}
public function fetchAll($fetchMode) {
return array();
}
2006-12-28 00:20:26 +03:00
public function execute() {
2007-01-15 21:48:50 +03:00
$this->mock->addQuery($this->query);
2006-12-28 00:20:26 +03:00
return true;
}
2007-01-25 01:50:49 +03:00
public function fetchColumn($colnum = 0) {
2007-01-08 02:52:15 +03:00
return 0;
}
2006-11-23 02:35:34 +03:00
}
2007-01-06 01:07:50 +03:00
2006-11-23 02:35:34 +03:00
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) {
2006-11-25 02:23:52 +03:00
2006-11-23 02:35:34 +03:00
$this->driverName = $driverName;
$this->generic = $generic;
}
2006-11-25 02:23:52 +03:00
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) {
return $this->dataDict->getPortableDeclaration(array('type' => $type, 'name' => 'colname', 'length' => 1, 'fixed' => true));
2006-11-25 02:23:52 +03:00
}
2006-12-28 00:20:26 +03:00
public function setDriverName($driverName) {
$this->driverName = $driverName;
}
2006-11-23 02:35:34 +03:00
public function init() {
$this->adapter = new AdapterMock($this->driverName);
$this->manager = Doctrine_Manager::getInstance();
$this->manager->setDefaultAttributes();
$this->conn = $this->manager->openConnection($this->adapter);
2006-11-25 02:23:52 +03:00
2006-11-23 02:35:34 +03:00
if( ! $this->generic) {
$this->export = $this->conn->export;
2006-11-25 02:23:52 +03:00
$name = $this->adapter->getName();
2006-11-24 02:23:24 +03:00
if($this->adapter->getName() == 'oci')
2006-11-25 02:23:52 +03:00
$name = 'Oracle';
$tx = 'Doctrine_Transaction_' . ucwords($name);
$dataDict = 'Doctrine_DataDict_' . ucwords($name);
2006-12-28 00:20:26 +03:00
$exc = 'Doctrine_Connection_' . ucwords($name) . '_Exception';
$this->exc = new $exc();
2006-11-24 02:23:24 +03:00
if(class_exists($tx))
2006-11-25 02:23:52 +03:00
$this->transaction = new $tx($this->conn);
if(class_exists($dataDict)) {
$this->dataDict = new $dataDict($this->conn);
2006-12-22 01:06:08 +03:00
}
2006-11-23 02:35:34 +03:00
//$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;
}
}
}