|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace itertools; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
class AutoTransactionBatchIteratorTest extends PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
/** @test */ |
|
12
|
|
|
public function testBasicFunctionality() |
|
13
|
|
|
{ |
|
14
|
|
|
$beginTransactionCount = 0; |
|
15
|
|
|
$commitCount = 0; |
|
16
|
|
|
$pdo = new MockPDO(array( |
|
17
|
|
|
'beginTransaction' => function() use (&$beginTransactionCount) { $beginTransactionCount += 1; }, |
|
18
|
|
|
'commit' => function() use (&$commitCount) { $commitCount += 1; }, |
|
19
|
|
|
)); |
|
20
|
|
|
|
|
21
|
|
|
$iterator = new AutoTransactionBatchIterator(range(0, 4), $pdo, 2); |
|
22
|
|
|
|
|
23
|
|
|
$iterator->rewind(); |
|
24
|
|
|
$this->assertEquals(array(0, 0), array($beginTransactionCount, $commitCount)); |
|
25
|
|
|
$this->assertTrue($iterator->valid()); |
|
26
|
|
|
$this->assertEquals(array(1, 0), array($beginTransactionCount, $commitCount)); |
|
27
|
|
|
// get first element now |
|
28
|
|
|
|
|
29
|
|
|
$iterator->next(); |
|
30
|
|
|
$this->assertTrue($iterator->valid()); |
|
31
|
|
|
$this->assertEquals(array(1, 0), array($beginTransactionCount, $commitCount)); |
|
32
|
|
|
// get second element now |
|
33
|
|
|
|
|
34
|
|
|
$iterator->next(); |
|
35
|
|
|
$this->assertTrue($iterator->valid()); |
|
36
|
|
|
$this->assertEquals(array(2, 1), array($beginTransactionCount, $commitCount)); |
|
37
|
|
|
// get third element now |
|
38
|
|
|
|
|
39
|
|
|
$iterator->next(); |
|
40
|
|
|
$this->assertTrue($iterator->valid()); |
|
41
|
|
|
$this->assertEquals(array(2, 1), array($beginTransactionCount, $commitCount)); |
|
42
|
|
|
// get forth element now |
|
43
|
|
|
|
|
44
|
|
|
$iterator->next(); |
|
45
|
|
|
$this->assertTrue($iterator->valid()); |
|
46
|
|
|
$this->assertEquals(array(3, 2), array($beginTransactionCount, $commitCount)); |
|
47
|
|
|
// get fith element now |
|
48
|
|
|
|
|
49
|
|
|
$iterator->next(); |
|
50
|
|
|
$this->assertFalse($iterator->valid()); |
|
51
|
|
|
$this->assertEquals(array(3, 3), array($beginTransactionCount, $commitCount)); |
|
52
|
|
|
// end of iteration |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** @test */ |
|
56
|
|
|
public function testEmptyIterator() |
|
57
|
|
|
{ |
|
58
|
|
|
$beginTransactionCount = 0; |
|
59
|
|
|
$commitCount = 0; |
|
60
|
|
|
$pdo = new MockPDO(array( |
|
61
|
|
|
'beginTransaction' => function() use (&$beginTransactionCount) { $beginTransactionCount += 1; }, |
|
62
|
|
|
'commit' => function() use (&$commitCount) { $commitCount += 1; }, |
|
63
|
|
|
)); |
|
64
|
|
|
|
|
65
|
|
|
$iterator = new AutoTransactionBatchIterator(array(), $pdo, 2); |
|
66
|
|
|
foreach($iterator as $i) { |
|
67
|
|
|
} |
|
68
|
|
|
$this->assertEquals(array(0, 0), array($beginTransactionCount, $commitCount)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** @test */ |
|
72
|
|
|
public function testForeach() |
|
73
|
|
|
{ |
|
74
|
|
|
$beginTransactionCount = 0; |
|
75
|
|
|
$commitCount = 0; |
|
76
|
|
|
$pdo = new MockPDO(array( |
|
77
|
|
|
'beginTransaction' => function() use (&$beginTransactionCount) { $beginTransactionCount += 1; }, |
|
78
|
|
|
'commit' => function() use (&$commitCount) { $commitCount += 1; }, |
|
79
|
|
|
)); |
|
80
|
|
|
|
|
81
|
|
|
$iterator = new AutoTransactionBatchIterator(range(0, 4), $pdo, 2); |
|
82
|
|
|
foreach($iterator as $i => $element) { |
|
83
|
|
|
switch($i) { |
|
84
|
|
|
case 0: |
|
85
|
|
|
$this->assertEquals(array(1, 0), array($beginTransactionCount, $commitCount)); break; |
|
86
|
|
|
case 1: |
|
87
|
|
|
$this->assertEquals(array(1, 0), array($beginTransactionCount, $commitCount)); break; |
|
88
|
|
|
case 2: |
|
89
|
|
|
$this->assertEquals(array(2, 1), array($beginTransactionCount, $commitCount)); break; |
|
90
|
|
|
case 3: |
|
91
|
|
|
$this->assertEquals(array(2, 1), array($beginTransactionCount, $commitCount)); break; |
|
92
|
|
|
case 4: |
|
93
|
|
|
$this->assertEquals(array(3, 2), array($beginTransactionCount, $commitCount)); break; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
$this->assertEquals(array(3, 3), array($beginTransactionCount, $commitCount)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** @test */ |
|
100
|
|
|
public function testExceptionInForeach() |
|
101
|
|
|
{ |
|
102
|
|
|
$beginTransactionCount = 0; |
|
103
|
|
|
$commitCount = 0; |
|
104
|
|
|
$rollBackCount = 0; |
|
105
|
|
|
$pdo = new MockPDO(array( |
|
106
|
|
|
'beginTransaction' => function() use (&$beginTransactionCount) { $beginTransactionCount += 1; }, |
|
107
|
|
|
'commit' => function() use (&$commitCount) { $commitCount += 1; }, |
|
108
|
|
|
'rollBack' => function() use (&$rollBackCount) { $rollBackCount += 1; }, |
|
109
|
|
|
)); |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
|
|
foreach(new AutoTransactionBatchIterator(range(0, 4), $pdo) as $i) { |
|
113
|
|
|
throw new Exception(); |
|
114
|
|
|
} |
|
115
|
|
|
} catch (Exception $e) { |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
$this->assertEquals(1, $rollBackCount); |
|
118
|
|
|
$this->assertEquals($commitCount + $rollBackCount, $beginTransactionCount); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|