Skip to content

Commit 4c47e1d

Browse files
committed
dont start transaction if already within one
1 parent 0911d11 commit 4c47e1d

File tree

7 files changed

+93
-76
lines changed

7 files changed

+93
-76
lines changed

src/PHPixie/ORM/Plans.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
class Plans
66
{
7-
/**
8-
* @type Plans\Transaction
9-
*/
10-
protected $transaction;
11-
127
/**
138
* @return Plans\Plan\Steps
149
*/
@@ -48,16 +43,8 @@ public function loader($resultStep, $loader)
4843
/**
4944
* @return Plans\Transaction
5045
*/
51-
public function transaction()
52-
{
53-
if ($this->transaction === null)
54-
$this->transaction = $this->buildTransaction();
55-
56-
return $this->transaction;
57-
}
58-
59-
protected function buildTransaction()
46+
public function transaction($connections)
6047
{
61-
return new Plans\Transaction();
48+
return new Plans\Transaction($connections);
6249
}
6350
}

src/PHPixie/ORM/Plans/Plan.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ public function __construct($plans)
1313

1414
public function execute()
1515
{
16-
$transaction = $this->plans->transaction();
1716
$steps = $this->steps();
1817
$connections = $this->usedConnections();
19-
$transaction->begin($connections);
18+
19+
$transaction = $this->plans->transaction($connections);
20+
$transaction->begin();
2021

2122
try {
2223
foreach($steps as $step) {
2324
$step->execute();
2425
}
25-
$transaction->commit($connections);
26+
$transaction->commit();
2627

2728
} catch (\Exception $exception) {
28-
$transaction->rollback($connections);
29+
$transaction->rollback();
2930
throw $exception;
3031
}
3132
}

src/PHPixie/ORM/Plans/Transaction.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,53 @@
44

55
class Transaction
66
{
7-
public function begin($connections)
7+
protected $connections;
8+
protected $transactions = array();
9+
10+
public function __construct($connections)
811
{
9-
foreach($this->getTransactable($connections) as $connection)
10-
$connection->beginTransaction();
12+
$this->connections = $connections;
13+
}
14+
15+
public function begin()
16+
{
17+
$transactions = array();
18+
foreach($this->getTransactable() as $connection) {
19+
if(!$connection->inTransaction()) {
20+
$connection->beginTransaction();
21+
$transactions[] = $connection;
22+
}
23+
}
24+
25+
$this->transactions = $transactions;
1126
}
1227

13-
public function commit($connections)
28+
public function commit()
1429
{
15-
foreach($this->getTransactable($connections) as $connection)
30+
foreach($this->transactions as $connection) {
1631
$connection->commitTransaction();
32+
}
33+
34+
$this->transactions = array();
1735
}
1836

19-
public function rollback($connections)
37+
public function rollback()
2038
{
21-
foreach($this->getTransactable($connections) as $connection)
39+
foreach($this->transactions as $connection) {
2240
$connection->rollbackTransaction();
41+
}
42+
43+
$this->transactions = array();
2344
}
2445

25-
protected function getTransactable($connections)
46+
protected function getTransactable()
2647
{
2748
$transactable = array();
28-
foreach($connections as $connection)
29-
if ($connection instanceof \PHPixie\Database\Connection\Transactable)
49+
foreach($this->connections as $connection) {
50+
if ($connection instanceof \PHPixie\Database\Connection\Transactable) {
3051
$transactable[] = $connection;
52+
}
53+
}
3154

3255
return $transactable;
3356
}

tests/PHPixie/Tests/ORM/Plans/PlanTest.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,13 @@
88
abstract class PlanTest extends \PHPixie\Test\Testcase
99
{
1010
protected $plans;
11-
protected $transaction;
1211
protected $connections;
1312
protected $plan;
1413

1514
public function setUp()
1615
{
1716
$this->plans = $this->quickMock('\PHPixie\ORM\Plans');
1817

19-
$this->transaction = $this->quickMock('\PHPixie\ORM\Plans\Transaction');
20-
$this->method($this->plans, 'transaction', $this->transaction, array());
21-
2218
$this->connections = array(
2319
$this->getConnection(),
2420
$this->getConnection()
@@ -82,8 +78,12 @@ public function testExecute()
8278
public function testExecuteRollback()
8379
{
8480
$this->addSteps(true);
85-
$this->method($this->transaction, 'begin', null, array($this->connections), 0);
86-
$this->method($this->transaction, 'rollback', null, array($this->connections), 1);
81+
82+
$transaction = $this->quickMock('\PHPixie\ORM\Plans\Transaction');
83+
$this->method($this->plans, 'transaction', $transaction, array($this->connections));
84+
85+
$this->method($transaction, 'begin', null, array(), 0);
86+
$this->method($transaction, 'rollback', null, array(), 1);
8787
foreach($this->steps as $step)
8888
$this->method($step, 'execute', function() {
8989
throw new \Exception("test");
@@ -103,8 +103,12 @@ protected function step($connections)
103103
protected function prepareExecute()
104104
{
105105
$this->addSteps(true);
106-
$this->method($this->transaction, 'begin', null, array($this->connections), 0);
107-
$this->method($this->transaction, 'commit', null, array($this->connections), 1);
106+
107+
$transaction = $this->quickMock('\PHPixie\ORM\Plans\Transaction');
108+
$this->method($this->plans, 'transaction', $transaction, array($this->connections));
109+
110+
$this->method($transaction, 'begin', null, array(), 0);
111+
$this->method($transaction, 'commit', null, array(), 1);
108112
foreach($this->steps as $step) {
109113
$step
110114
->expects($this->once())

tests/PHPixie/Tests/ORM/Plans/TransactionTest.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,33 @@ public function setUp()
1717
$this->connection(),
1818
$this->connection(true),
1919
);
20-
$this->transaction = new \PHPixie\ORM\Plans\Transaction;
20+
$this->transaction = new \PHPixie\ORM\Plans\Transaction($this->connections);
2121
}
2222

2323
protected function connection($transactable = false)
2424
{
25-
$class = $transactable ? '\PHPixie\Database\Connection\Transactable' : '\PHPixie\Database\Connection';
26-
return $this->abstractMock($class, array(
27-
'beginTransaction',
28-
'commitTransaction',
29-
'rollbackTransaction',
30-
));
25+
$class = $transactable ? '\PHPixie\Database\Driver\PDO\Connection' : '\PHPixie\Database\Connection';
26+
return $this->quickMock($class);
3127
}
3228

3329
/**
3430
* @covers ::begin
35-
* @covers ::<protected>
36-
*/
37-
public function testBeginTransaction()
38-
{
39-
$this->transactionTest('begin');
40-
}
41-
42-
/**
4331
* @covers ::commit
4432
* @covers ::<protected>
4533
*/
46-
public function testCommitTransaction()
34+
public function testTransaction()
4735
{
48-
$this->transactionTest('commit');
36+
$this->method($this->connections[0], 'inTransaction', false, array(), 0);
37+
$this->method($this->connections[2], 'inTransaction', false, array(), 0);
38+
39+
$this->method($this->connections[0], 'beginTransaction', null, array(), 1);
40+
$this->method($this->connections[2], 'beginTransaction', null, array(), 1);
41+
42+
$this->method($this->connections[0], 'commitTransaction', null, array(), 2);
43+
$this->method($this->connections[2], 'commitTransaction', null, array(), 2);
44+
45+
$this->transaction->begin();
46+
$this->transaction->commit();
4947
}
5048

5149
/**
@@ -54,17 +52,16 @@ public function testCommitTransaction()
5452
*/
5553
public function testRollbackTransaction()
5654
{
57-
$this->transactionTest('rollback');
58-
}
59-
60-
protected function transactionTest($method)
61-
{
62-
$connectionMethod = $method.'Transaction';
63-
$this->method($this->connections[0], $connectionMethod, null, array(), 0);
64-
$this->connections[1]
65-
->expects($this->never())
66-
->method($connectionMethod);
67-
$this->method($this->connections[2], $connectionMethod, null, array(), 0);
68-
$this->transaction->$method($this->connections);
55+
$this->method($this->connections[0], 'inTransaction', false, array(), 0);
56+
$this->method($this->connections[2], 'inTransaction', false, array(), 0);
57+
58+
$this->method($this->connections[0], 'beginTransaction', null, array(), 1);
59+
$this->method($this->connections[2], 'beginTransaction', null, array(), 1);
60+
61+
$this->method($this->connections[0], 'rollbackTransaction', null, array(), 2);
62+
$this->method($this->connections[2], 'rollbackTransaction', null, array(), 2);
63+
64+
$this->transaction->begin();
65+
$this->transaction->rollback();
6966
}
7067
}

tests/PHPixie/Tests/ORM/PlansTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public function testLoader()
7676
*/
7777
public function testTransaction()
7878
{
79-
$transaction = $this->plans->transaction();
80-
$this->assertInstanceOf('\PHPixie\ORM\Plans\Transaction', $transaction);
81-
$this->assertSame($transaction, $this->plans->transaction());
79+
$connections = array(5);
80+
$transaction = $this->plans->transaction($connections);
81+
$this->assertInstance($transaction, '\PHPixie\ORM\Plans\Transaction', array(
82+
'connections' => $connections
83+
));
8284
}
8385

8486
}

tests/PHPixie/Tests/ORM/Relationships/Relationship/Implementation/HandlerTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,16 @@ protected function side($type, $map = array(), $sideMethodMap = array(), $config
108108
protected function config($map, $methodMap = array())
109109
{
110110
$config = $this->getConfig();
111-
$config
112-
->expects($this->any())
113-
->method('get')
114-
->will($this->returnCallback(function($key) use($map){
115-
return $map[$key];
116-
}));
117-
111+
112+
if(method_exists($config, 'get')) {
113+
$config
114+
->expects($this->any())
115+
->method('get')
116+
->will($this->returnCallback(function($key) use($map){
117+
return $map[$key];
118+
}));
119+
}
120+
118121
foreach($map as $key => $value)
119122
$config->$key = $value;
120123

0 commit comments

Comments
 (0)