From bcc27ff478763cc78f4ad3e94c18c7601c974d9e Mon Sep 17 00:00:00 2001 From: nsossonko Date: Tue, 12 Apr 2016 11:11:18 -0400 Subject: [PATCH] Fixes #11554 - Transaction Manager doesn't keep proper account of transactions --- CHANGELOG.md | 1 + phalcon/mvc/model/transaction/manager.zep | 8 +- .../Mvc/Model/Transaction/Manager.php | 34 ++++++++ .../Mvc/Model/Transaction/ManagerTest.php | 87 +++++++++++++++++++ 4 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 tests/_proxies/Mvc/Model/Transaction/Manager.php create mode 100644 tests/unit/Mvc/Model/Transaction/ManagerTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e1437cb450e..8bd16978398 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -64,6 +64,7 @@ - Removed methods setMode(), getMode(), getAvailableModes() in `Phalcon\CryptInterface` - Added `Phalcon\Assets\Manager::exists()` to check if collection exists - `Phalcon\Mvc\Model\Manager::load()` now can load models from aliased namespaces +- `Phalcon\Mvc\Model\Transaction\Manager` now correctly keeps account of transactions [#11554](https://github.com/phalcon/cphalcon/issues/11554) # [2.0.11](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.11) (????-??-??) - Fix Model magic set functionality to maintain variable visibility and utilize setter methods.[#11286](https://github.com/phalcon/cphalcon/issues/11286) diff --git a/phalcon/mvc/model/transaction/manager.zep b/phalcon/mvc/model/transaction/manager.zep index fdfd62a7138..7a32ceed951 100644 --- a/phalcon/mvc/model/transaction/manager.zep +++ b/phalcon/mvc/model/transaction/manager.zep @@ -284,9 +284,11 @@ class Manager implements ManagerInterface, InjectionAwareInterface if count(transactions) { let newTransactions = []; for managedTransaction in transactions { - if managedTransaction == transaction { - let newTransactions[] = transaction, - this->_number--; + if managedTransaction != transaction { + let newTransactions[] = transaction; + } + else { + let this->_number--; } } let this->_transactions = newTransactions; diff --git a/tests/_proxies/Mvc/Model/Transaction/Manager.php b/tests/_proxies/Mvc/Model/Transaction/Manager.php new file mode 100644 index 00000000000..6f8a6b3eadc --- /dev/null +++ b/tests/_proxies/Mvc/Model/Transaction/Manager.php @@ -0,0 +1,34 @@ + + * @package Phalcon\Test\Proxy\Mvc\Model\Transaction + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class Manager extends PhManager +{ + public function getTransactions() + { + return $this->_transactions; + } + + public function count() + { + return $this->_number; + } +} diff --git a/tests/unit/Mvc/Model/Transaction/ManagerTest.php b/tests/unit/Mvc/Model/Transaction/ManagerTest.php new file mode 100644 index 00000000000..ad631f23256 --- /dev/null +++ b/tests/unit/Mvc/Model/Transaction/ManagerTest.php @@ -0,0 +1,87 @@ + + * @package Phalcon\Test\Unit\Mvc\Model\Transaction + * + * The contents of this file are subject to the New BSD License that is + * bundled with this package in the file docs/LICENSE.txt + * + * If you did not receive a copy of the license and are unable to obtain it + * through the world-wide-web, please send an email to license@phalconphp.com + * so that we can send you a copy immediately. + */ +class ManagerTest extends UnitTest +{ + /** + * Tests transaction is removed when rolled back. + * + * @author Nochum Sossonko + * @since 2016-04-09 + */ + public function testTransactionRemovedOnRollback() + { + $this->specify( + "Test does not remove transaction when rolled back", + function () { + $tm = new TransactionManager(); + $transaction = $tm->get(true); + $select = new Select(); + $select->setTransaction($transaction); + $select->create( + array( + 'name' => 'Crack of Dawn' + ) + ); + expect($tm->count())->equals(1); + expect(count($tm->getTransactions()))->equals(1); + try { + $transaction->rollback(); + } + catch(\Phalcon\Mvc\Model\Transaction\Failed $e) {} + expect($tm->count())->equals(0); + expect(count($tm->getTransactions()))->equals(0); + } + ); + } + + /** + * Tests transaction is removed when committed. + * + * @author Nochum Sossonko + * @since 2016-04-09 + */ + public function testTransactionRemovedOnCommit() + { + $this->specify( + "Test does not remove transaction when committed", + function () { + $tm = new TransactionManager(); + $transaction = $tm->get(true); + $select = new Select(); + $select->setTransaction($transaction); + $select->create( + array( + 'name' => 'Crack of Dawn' + ) + ); + expect($tm->count())->equals(1); + expect(count($tm->getTransactions()))->equals(1); + $transaction->commit(); + expect($tm->count())->equals(0); + expect(count($tm->getTransactions()))->equals(0); + } + ); + } +}