Skip to content

Commit 95a845d

Browse files
committed
test: add DB transaction tests
1 parent 2a33c1f commit 95a845d

1 file changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\Live;
13+
14+
use CodeIgniter\Database\Exceptions\DatabaseException;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use CodeIgniter\Test\DatabaseTestTrait;
17+
use Config\Database;
18+
use Tests\Support\Database\Seeds\CITestSeeder;
19+
20+
/**
21+
* @group DatabaseLive
22+
*
23+
* @internal
24+
*/
25+
final class TransactionTest extends CIUnitTestCase
26+
{
27+
use DatabaseTestTrait;
28+
29+
protected $refresh = true;
30+
protected $seed = CITestSeeder::class;
31+
32+
protected function setUp(): void
33+
{
34+
// Reset connection instance.
35+
$this->db = Database::connect($this->DBGroup, false);
36+
37+
parent::setUp();
38+
}
39+
40+
/**
41+
* Sets $DBDebug to false.
42+
*
43+
* WARNING: this value will persist! take care to roll it back.
44+
*/
45+
protected function disableDBDebug(): void
46+
{
47+
$this->setPrivateProperty($this->db, 'DBDebug', false);
48+
}
49+
50+
/**
51+
* Sets $DBDebug to true.
52+
*/
53+
protected function enableDBDebug(): void
54+
{
55+
$this->setPrivateProperty($this->db, 'DBDebug', true);
56+
}
57+
58+
public function testTransStartDBDebugTrue()
59+
{
60+
$builder = $this->db->table('job');
61+
$e = null;
62+
63+
try {
64+
$this->db->transStart();
65+
66+
$jobData = [
67+
'name' => 'Grocery Sales',
68+
'description' => 'Discount!',
69+
];
70+
$builder->insert($jobData);
71+
72+
// Duplicate entry '1' for key 'PRIMARY'
73+
$jobData = [
74+
'id' => 1,
75+
'name' => 'Comedian',
76+
'description' => 'Theres something in your teeth',
77+
];
78+
$builder->insert($jobData);
79+
80+
$this->db->transComplete();
81+
} catch (DatabaseException $e) {
82+
// Do nothing.
83+
}
84+
85+
$this->assertInstanceOf(DatabaseException::class, $e);
86+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
87+
}
88+
89+
public function testTransStartDBDebugFalse()
90+
{
91+
$this->disableDBDebug();
92+
93+
$builder = $this->db->table('job');
94+
95+
$this->db->transStart();
96+
97+
$jobData = [
98+
'name' => 'Grocery Sales',
99+
'description' => 'Discount!',
100+
];
101+
$builder->insert($jobData);
102+
103+
$this->assertTrue($this->db->transStatus());
104+
105+
// Duplicate entry '1' for key 'PRIMARY'
106+
$jobData = [
107+
'id' => 1,
108+
'name' => 'Comedian',
109+
'description' => 'Theres something in your teeth',
110+
];
111+
$builder->insert($jobData);
112+
113+
$this->assertFalse($this->db->transStatus());
114+
115+
$this->db->transComplete();
116+
117+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
118+
119+
$this->enableDBDebug();
120+
}
121+
122+
public function testTransStrictTrueAndDBDebugFalse()
123+
{
124+
$this->disableDBDebug();
125+
126+
$builder = $this->db->table('job');
127+
128+
// The first transaction group
129+
$this->db->transStart();
130+
131+
$jobData = [
132+
'name' => 'Grocery Sales',
133+
'description' => 'Discount!',
134+
];
135+
$builder->insert($jobData);
136+
137+
$this->assertTrue($this->db->transStatus());
138+
139+
// Duplicate entry '1' for key 'PRIMARY'
140+
$jobData = [
141+
'id' => 1,
142+
'name' => 'Comedian',
143+
'description' => 'Theres something in your teeth',
144+
];
145+
$builder->insert($jobData);
146+
147+
$this->assertFalse($this->db->transStatus());
148+
149+
$this->db->transComplete();
150+
151+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
152+
153+
// The second transaction group
154+
$this->db->transStart();
155+
156+
$jobData = [
157+
'name' => 'Comedian',
158+
'description' => 'Theres something in your teeth',
159+
];
160+
$builder->insert($jobData);
161+
162+
$this->assertFalse($this->db->transStatus());
163+
164+
$this->db->transComplete();
165+
166+
$this->dontSeeInDatabase('job', ['name' => 'Comedian']);
167+
168+
$this->enableDBDebug();
169+
}
170+
171+
public function testTransStrictFalseAndDBDebugFalse()
172+
{
173+
$this->disableDBDebug();
174+
175+
$builder = $this->db->table('job');
176+
177+
$this->db->transStrict(false);
178+
179+
// The first transaction group
180+
$this->db->transStart();
181+
182+
$jobData = [
183+
'name' => 'Grocery Sales',
184+
'description' => 'Discount!',
185+
];
186+
$builder->insert($jobData);
187+
188+
$this->assertTrue($this->db->transStatus());
189+
190+
// Duplicate entry '1' for key 'PRIMARY'
191+
$jobData = [
192+
'id' => 1,
193+
'name' => 'Comedian',
194+
'description' => 'Theres something in your teeth',
195+
];
196+
$builder->insert($jobData);
197+
198+
$this->assertFalse($this->db->transStatus());
199+
200+
$this->db->transComplete();
201+
202+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
203+
204+
// The second transaction group
205+
$this->db->transStart();
206+
207+
$jobData = [
208+
'name' => 'Comedian',
209+
'description' => 'Theres something in your teeth',
210+
];
211+
$builder->insert($jobData);
212+
213+
$this->assertTrue($this->db->transStatus());
214+
215+
$this->db->transComplete();
216+
217+
$this->seeInDatabase('job', ['name' => 'Comedian']);
218+
219+
$this->enableDBDebug();
220+
}
221+
}

0 commit comments

Comments
 (0)