diff --git a/tests/DriverTestCase.php b/tests/DriverTestCase.php new file mode 100644 index 000000000..94b795729 --- /dev/null +++ b/tests/DriverTestCase.php @@ -0,0 +1,98 @@ +name = $name; + } + public function getName() { + return $this->name; + } + public function pop() { + return array_pop($this->queries); + } + + public function prepare($prepareString){ + return new AdapterStatementMock; + } + public function query($queryString) { + $this->queries[] = $queryString; + + return new AdapterStatementMock; + } + public function quote($input){ } + public function exec($statement) { + $this->queries[] = $statement; + + return 0; + } + public function lastInsertId(){ } + public function beginTransaction(){ + $this->queries[] = 'BEGIN TRANSACTION'; + } + public function commit(){ + $this->queries[] = 'COMMIT'; + } + public function rollBack(){ } + public function errorCode(){ } + public function errorInfo(){ } + public function getAttribute($attribute) { + if($attribute == PDO::ATTR_DRIVER_NAME) + return $this->name; + } + public function setAttribute($attribute, $value) { + + } +} +class AdapterStatementMock { + public function fetch($fetchMode) { + return array(); + } + public function fetchAll($fetchMode) { + return array(); + } +} +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) { + $this->driverName = $driverName; + $this->generic = $generic; + } + public function init() { + $this->adapter = new AdapterMock($this->driverName); + $this->manager = Doctrine_Manager::getInstance(); + $this->manager->setDefaultAttributes(); + $this->conn = $this->manager->openConnection($this->adapter); + if( ! $this->generic) { + $this->export = $this->conn->export; + + $tx = 'Doctrine_Transaction_' . ucwords($this->adapter->getName()); + if(class_exists($tx)) + $this->transaction = new $tx($this->conn); + //$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; + } + } +} +?> diff --git a/tests/ExportMysqlTestCase.php b/tests/ExportMysqlTestCase.php index 018c8e0ab..3ebb943c6 100644 --- a/tests/ExportMysqlTestCase.php +++ b/tests/ExportMysqlTestCase.php @@ -2,7 +2,26 @@ class Doctrine_Export_Mysql_TestCase extends Doctrine_Export_TestCase { public function __construct() { parent::__construct('mysql'); - } + } + + public function testAlterTableThrowsExceptionWithoutValidTableName() { + try { + $this->export->alterTable(0,0,array()); + + $this->fail(); + } catch(Doctrine_Export_Exception $e) { + $this->pass(); + } + } + public function testCreateTableExecutesSql() { + $name = 'mytable'; + + $fields = array('id' => array('type' => 'integer', 'unsigned' => 1)); + + $options = array('type' => 'foo'); + + //$this->export->createTable($name, $fields, $options); + } public function testCreateDatabaseExecutesSql() { $this->export->createDatabase('db'); diff --git a/tests/ExportOracleTestCase.php b/tests/ExportOracleTestCase.php new file mode 100644 index 000000000..7c5c6e849 --- /dev/null +++ b/tests/ExportOracleTestCase.php @@ -0,0 +1,26 @@ +export->createSequence($sequenceName, $start); + + $this->assertEqual($this->adapter->pop(), $query); + } + + public function testDropSequenceExecutesSql() { + $sequenceName = 'sequence'; + + $query = 'DROP SEQUENCE ' . $sequenceName;; + + $this->export->dropSequence($sequenceName); + + $this->assertEqual($this->adapter->pop(), $query); + } +} +?> diff --git a/tests/ExportTestCase.php b/tests/ExportTestCase.php index 576218a16..b4167e192 100644 --- a/tests/ExportTestCase.php +++ b/tests/ExportTestCase.php @@ -1,15 +1,7 @@ export->alterTable(0,0,array()); - $this->fail(); - } catch(Doctrine_Export_Exception $e) { - $this->pass(); - } - } public function testCreateTableThrowsExceptionWithoutValidTableName() { try { $this->export->createTable(0,array(),array()); @@ -28,6 +20,11 @@ class Doctrine_Export_TestCase extends Doctrine_Driver_UnitTestCase { $this->pass(); } } + public function testDropConstraintExecutesSql() { + $this->export->dropConstraint('sometable', 'relevancy'); + + $this->assertEqual($this->adapter->pop(), 'ALTER TABLE sometable DROP CONSTRAINT relevancy'); + } public function testCreateIndexExecutesSql() { $this->export->createIndex('sometable', 'relevancy', array('fields' => array('title' => array(), 'content' => array()))); diff --git a/tests/TransactionMysqlTestCase.php b/tests/TransactionMysqlTestCase.php new file mode 100644 index 000000000..a27f9c4b6 --- /dev/null +++ b/tests/TransactionMysqlTestCase.php @@ -0,0 +1,39 @@ +transaction->createSavePoint('mypoint'); + + $this->assertEqual($this->adapter->pop(), 'SAVEPOINT mypoint'); + } + public function testReleaseSavePointExecutesSql() { + $this->transaction->releaseSavePoint('mypoint'); + + $this->assertEqual($this->adapter->pop(), 'RELEASE SAVEPOINT mypoint'); + } + public function testRollbackSavePointExecutesSql() { + $this->transaction->rollbackSavePoint('mypoint'); + + $this->assertEqual($this->adapter->pop(), 'ROLLBACK TO SAVEPOINT mypoint'); + } + public function testGetIsolationExecutesSql() { + $this->transaction->getIsolation(); + + $this->assertEqual($this->adapter->pop(), 'SELECT @@tx_isolation'); + } + public function testSetIsolationThrowsExceptionOnUnknownIsolationMode() { + try { + $this->transaction->setIsolation('unknown'); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testSetIsolationExecutesSql() { + $this->transaction->setIsolation('READ UNCOMMITTED'); + + $this->assertEqual($this->adapter->pop(), 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'); + } +} diff --git a/tests/TransactionTestCase.php b/tests/TransactionTestCase.php new file mode 100644 index 000000000..556a9348d --- /dev/null +++ b/tests/TransactionTestCase.php @@ -0,0 +1,79 @@ +transaction->createSavePoint('point'); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testReleaseSavepointIsOnlyImplementedAtDriverLevel() { + try { + $this->transaction->releaseSavePoint('point'); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testRollbackSavepointIsOnlyImplementedAtDriverLevel() { + try { + $this->transaction->rollbackSavePoint('point'); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testSetIsolationIsOnlyImplementedAtDriverLevel() { + try { + $this->transaction->setIsolation('READ UNCOMMITTED'); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testGetIsolationIsOnlyImplementedAtDriverLevel() { + try { + $this->transaction->GetIsolation('READ UNCOMMITTED'); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testTransactionLevelIsInitiallyZero() { + $this->assertEqual($this->transaction->getTransactionLevel(), 0); + } + public function testGetStateReturnsStateConstant() { + $this->assertEqual($this->transaction->getState(), Doctrine_Transaction::STATE_SLEEP); + } + public function testExceptionIsThrownWhenCommittingNotActiveTransaction() { + try { + $this->transaction->commit(); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testExceptionIsThrownWhenUsingRollbackOnNotActiveTransaction() { + try { + $this->transaction->rollback(); + $this->fail(); + } catch(Doctrine_Transaction_Exception $e) { + $this->pass(); + } + } + public function testBeginTransactionStartsNewTransaction() { + $this->transaction->beginTransaction(); + + $this->assertEqual($this->adapter->pop(), 'BEGIN TRANSACTION'); + } + public function testCommitMethodCommitsCurrentTransaction() { + $this->transaction->commit(); + + $this->assertEqual($this->adapter->pop(), 'COMMIT'); + } + +}