Skip to content

Commit

Permalink
Merge pull request #11556 from nsossonko/2.1.x
Browse files Browse the repository at this point in the history
Fixes #11554 - Transaction Manager transaction accounting
  • Loading branch information
sergeyklay committed Apr 12, 2016
2 parents fd5ac6c + bcc27ff commit 039ea73
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions phalcon/mvc/model/transaction/manager.zep
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions tests/_proxies/Mvc/Model/Transaction/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Phalcon\Test\Proxy\Mvc\Model\Transaction;

use Phalcon\Mvc\Model\Transaction\Manager as PhManager;

/**
* \Phalcon\Test\Proxy\Mvc\Manager
* Url proxy class for \Phalcon\Mvc\Manager
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Nochum Sossonko <nsossonko@hotmail.com>
* @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;
}
}
87 changes: 87 additions & 0 deletions tests/unit/Mvc/Model/Transaction/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Phalcon\Test\Unit\Mvc\Model\Transaction;

use Phalcon\Test\Module\UnitTest;
use Phalcon\Test\Proxy\Mvc\Model\Transaction\Manager as TransactionManager;
use Phalcon\Test\Models\Select;

/**
* \Phalcon\Test\Unit\Mvc\CollectionTest
* Tests the Phalcon\Mvc\Collection component
*
* @copyright (c) 2011-2016 Phalcon Team
* @link http://www.phalconphp.com
* @author Nochum Sossonko <nsossonko@hotmail.com>
* @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 <nsossonko@hotmail.com>
* @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 <nsossonko@hotmail.com>
* @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);
}
);
}
}

0 comments on commit 039ea73

Please sign in to comment.