1
0
mirror of synced 2024-12-15 07:36:03 +03:00
doctrine2/tests/TransactionTestCase.php

305 lines
9.6 KiB
PHP
Raw Normal View History

2006-11-23 02:35:34 +03:00
<?php
2007-06-10 23:40:14 +04:00
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
2006-12-01 02:51:44 +03:00
/**
2007-06-10 23:40:14 +04:00
* Doctrine_Transaction_TestCase
*
* @package Doctrine
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @category Object Relational Mapping
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Transaction_TestCase extends Doctrine_UnitTestCase
{
public function testInit()
{
2007-06-25 22:47:36 +04:00
$this->transaction = new Doctrine_Transaction_Mock();
$this->listener = new TransactionListener();
$this->conn->setListener($this->listener);
2007-06-10 23:40:14 +04:00
}
2007-06-25 22:47:36 +04:00
public function testCreateSavepointListenersGetInvoked()
{
2006-11-23 02:35:34 +03:00
try {
2006-12-01 02:51:44 +03:00
$this->transaction->beginTransaction('point');
2007-06-25 22:47:36 +04:00
$this->pass();
} catch(Doctrine_Transaction_Exception $e) {
2006-11-23 02:35:34 +03:00
$this->fail();
2007-06-25 22:47:36 +04:00
}
$this->assertEqual($this->listener->pop(), 'postSavepointCreate');
$this->assertEqual($this->listener->pop(), 'preSavepointCreate');
}
public function testCommitSavepointListenersGetInvoked()
{
try {
$this->transaction->commit('point');
$this->pass();
2006-11-23 02:35:34 +03:00
} catch(Doctrine_Transaction_Exception $e) {
2007-06-25 22:47:36 +04:00
$this->fail();
}
$this->assertEqual($this->listener->pop(), 'postSavepointCommit');
$this->assertEqual($this->listener->pop(), 'preSavepointCommit');
$this->assertEqual($this->transaction->getTransactionLevel(), 0);
}
public function testNestedSavepoints()
{
$this->assertEqual($this->transaction->getTransactionLevel(), 0);
$this->transaction->beginTransaction();
$this->assertEqual($this->transaction->getTransactionLevel(), 1);
$this->transaction->beginTransaction('point 1');
$this->assertEqual($this->transaction->getTransactionLevel(), 2);
$this->transaction->beginTransaction('point 2');
$this->assertEqual($this->transaction->getTransactionLevel(), 3);
$this->transaction->commit('point 2');
$this->assertEqual($this->transaction->getTransactionLevel(), 2);
$this->transaction->commit('point 1');
$this->assertEqual($this->transaction->getTransactionLevel(), 1);
$this->transaction->commit();
$this->assertEqual($this->transaction->getTransactionLevel(), 0);
}
public function testRollbackSavepointListenersGetInvoked()
{
try {
$this->transaction->beginTransaction('point');
$this->transaction->rollback('point');
2006-11-23 02:35:34 +03:00
$this->pass();
2007-06-25 22:47:36 +04:00
} catch(Doctrine_Transaction_Exception $e) {
$this->fail();
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
$this->assertEqual($this->listener->pop(), 'postSavepointRollback');
$this->assertEqual($this->listener->pop(), 'preSavepointRollback');
$this->assertEqual($this->listener->pop(), 'postSavepointCreate');
$this->assertEqual($this->listener->pop(), 'preSavepointCreate');
$this->assertEqual($this->transaction->getTransactionLevel(), 0);
$this->listener = new Doctrine_Eventlistener();
$this->conn->setListener($this->listener);
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
public function testCreateSavepointIsOnlyImplementedAtDriverLevel()
{
2006-11-23 02:35:34 +03:00
try {
2007-06-25 22:47:36 +04:00
$this->transaction->beginTransaction('savepoint');
2006-11-23 02:35:34 +03:00
$this->fail();
} catch(Doctrine_Transaction_Exception $e) {
$this->pass();
}
}
2007-06-10 23:40:14 +04:00
2007-06-25 22:47:36 +04:00
public function testReleaseSavepointIsOnlyImplementedAtDriverLevel()
{
2006-11-23 02:35:34 +03:00
try {
2007-06-25 22:47:36 +04:00
$this->transaction->setTransactionLevel(1);
2006-12-01 02:51:44 +03:00
2007-06-25 22:47:36 +04:00
$this->transaction->commit('savepoint');
2006-11-23 02:35:34 +03:00
$this->fail();
} catch(Doctrine_Transaction_Exception $e) {
$this->pass();
}
2007-06-25 22:47:36 +04:00
$this->transaction->setTransactionLevel(0);
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
public function testRollbackSavepointIsOnlyImplementedAtDriverLevel()
{
try {
$this->transaction->setTransactionLevel(1);
$this->transaction->rollback('savepoint');
$this->fail();
} catch(Doctrine_Transaction_Exception $e) {
$this->pass();
}
$this->transaction->setTransactionLevel(0);
}
public function testSetIsolationIsOnlyImplementedAtDriverLevel()
{
2006-11-23 02:35:34 +03:00
try {
$this->transaction->setIsolation('READ UNCOMMITTED');
$this->fail();
} catch(Doctrine_Transaction_Exception $e) {
$this->pass();
}
}
2007-06-25 22:47:36 +04:00
public function testGetIsolationIsOnlyImplementedAtDriverLevel()
{
2006-11-23 02:35:34 +03:00
try {
$this->transaction->GetIsolation('READ UNCOMMITTED');
$this->fail();
} catch(Doctrine_Transaction_Exception $e) {
$this->pass();
}
}
2007-06-25 22:47:36 +04:00
public function testTransactionLevelIsInitiallyZero()
{
2006-11-23 02:35:34 +03:00
$this->assertEqual($this->transaction->getTransactionLevel(), 0);
}
2007-06-25 22:47:36 +04:00
public function testGetStateReturnsStateConstant()
{
$this->assertEqual($this->transaction->getState(), Doctrine_Transaction::STATE_SLEEP);
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
public function testCommittingNotActiveTransactionReturnsFalse()
{
2006-12-01 02:51:44 +03:00
$this->assertEqual($this->transaction->commit(), false);
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
public function testExceptionIsThrownWhenUsingRollbackOnNotActiveTransaction()
{
2006-12-01 02:51:44 +03:00
$this->assertEqual($this->transaction->rollback(), false);
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
public function testBeginTransactionStartsNewTransaction()
{
2006-11-23 02:35:34 +03:00
$this->transaction->beginTransaction();
2007-06-25 22:47:36 +04:00
$this->assertEqual($this->adapter->pop(), 'BEGIN TRANSACTION');
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
public function testCommitMethodCommitsCurrentTransaction()
{
2006-11-23 02:35:34 +03:00
$this->transaction->commit();
$this->assertEqual($this->adapter->pop(), 'COMMIT');
}
2007-08-11 01:00:40 +04:00
public function testNestedTransaction()
{
$conn = Doctrine_Manager::connection();
try {
$conn->beginTransaction();
// Create new client
$user = new User();
$user->set('name', 'Test User');
$user->save();
// Create new credit card
$phonenumber = new Phonenumber();
$phonenumber->set('entity_id', $user->get('id'));
$phonenumber->set('phonenumber', '123 123');
$phonenumber->save();
$conn->commit();
} catch (Exception $e) {
$conn->rollback();
}
$this->assertTrue($user->id > 0);
$this->assertTrue($phonenumber->id > 0);
}
2006-11-23 02:35:34 +03:00
}
2007-06-25 22:47:36 +04:00
class TransactionListener extends Doctrine_EventListener
{
protected $_messages = array();
public function preTransactionCommit(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
$event->skipOperation();
}
public function postTransactionCommit(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
}
public function preTransactionRollback(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
$event->skipOperation();
}
public function postTransactionRollback(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
}
public function preTransactionBegin(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
$event->skipOperation();
}
public function postTransactionBegin(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
}
public function preSavepointCommit(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
$event->skipOperation();
}
public function postSavepointCommit(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
}
public function preSavepointRollback(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
$event->skipOperation();
}
public function postSavepointRollback(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
}
public function preSavepointCreate(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
$event->skipOperation();
}
public function postSavepointCreate(Doctrine_Event $event)
{
$this->_messages[] = __FUNCTION__;
}
public function pop()
{
return array_pop($this->_messages);
}
}