Skip to content

Commit 1616e44

Browse files
authored
Merge pull request #6912 from kenjis/test-db-transactions-4.2
test: add DB transaction tests
2 parents 96e996a + a0765a3 commit 1616e44

File tree

1 file changed

+238
-0
lines changed

1 file changed

+238
-0
lines changed
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
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\Test\CIUnitTestCase;
15+
use CodeIgniter\Test\DatabaseTestTrait;
16+
use Config\Database;
17+
use Exception;
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 (Exception $e) {
82+
// Do nothing.
83+
84+
// MySQLi
85+
// mysqli_sql_exception: Duplicate entry '1' for key 'PRIMARY'
86+
87+
// SQLite3
88+
// ErrorException: SQLite3::exec(): UNIQUE constraint failed: db_job.id
89+
90+
// Postgres
91+
// ErrorException: pg_query(): Query failed: ERROR: duplicate key value violates unique constraint "pk_db_job"
92+
// DETAIL: Key (id)=(1) already exists.
93+
94+
// SQLSRV
95+
// Exception: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Cannot insert explicit
96+
// value for identity column in table 'db_job' when IDENTITY_INSERT is set to OFF.
97+
98+
// OCI8
99+
// ErrorException: oci_execute(): ORA-00001: unique constraint (ORACLE.pk_db_job) violated
100+
}
101+
102+
$this->assertInstanceOf(Exception::class, $e);
103+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
104+
}
105+
106+
public function testTransStartDBDebugFalse()
107+
{
108+
$this->disableDBDebug();
109+
110+
$builder = $this->db->table('job');
111+
112+
$this->db->transStart();
113+
114+
$jobData = [
115+
'name' => 'Grocery Sales',
116+
'description' => 'Discount!',
117+
];
118+
$builder->insert($jobData);
119+
120+
$this->assertTrue($this->db->transStatus());
121+
122+
// Duplicate entry '1' for key 'PRIMARY'
123+
$jobData = [
124+
'id' => 1,
125+
'name' => 'Comedian',
126+
'description' => 'Theres something in your teeth',
127+
];
128+
$builder->insert($jobData);
129+
130+
$this->assertFalse($this->db->transStatus());
131+
132+
$this->db->transComplete();
133+
134+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
135+
136+
$this->enableDBDebug();
137+
}
138+
139+
public function testTransStrictTrueAndDBDebugFalse()
140+
{
141+
$this->disableDBDebug();
142+
143+
$builder = $this->db->table('job');
144+
145+
// The first transaction group
146+
$this->db->transStart();
147+
148+
$jobData = [
149+
'name' => 'Grocery Sales',
150+
'description' => 'Discount!',
151+
];
152+
$builder->insert($jobData);
153+
154+
$this->assertTrue($this->db->transStatus());
155+
156+
// Duplicate entry '1' for key 'PRIMARY'
157+
$jobData = [
158+
'id' => 1,
159+
'name' => 'Comedian',
160+
'description' => 'Theres something in your teeth',
161+
];
162+
$builder->insert($jobData);
163+
164+
$this->assertFalse($this->db->transStatus());
165+
166+
$this->db->transComplete();
167+
168+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
169+
170+
// The second transaction group
171+
$this->db->transStart();
172+
173+
$jobData = [
174+
'name' => 'Comedian',
175+
'description' => 'Theres something in your teeth',
176+
];
177+
$builder->insert($jobData);
178+
179+
$this->assertFalse($this->db->transStatus());
180+
181+
$this->db->transComplete();
182+
183+
$this->dontSeeInDatabase('job', ['name' => 'Comedian']);
184+
185+
$this->enableDBDebug();
186+
}
187+
188+
public function testTransStrictFalseAndDBDebugFalse()
189+
{
190+
$this->disableDBDebug();
191+
192+
$builder = $this->db->table('job');
193+
194+
$this->db->transStrict(false);
195+
196+
// The first transaction group
197+
$this->db->transStart();
198+
199+
$jobData = [
200+
'name' => 'Grocery Sales',
201+
'description' => 'Discount!',
202+
];
203+
$builder->insert($jobData);
204+
205+
$this->assertTrue($this->db->transStatus());
206+
207+
// Duplicate entry '1' for key 'PRIMARY'
208+
$jobData = [
209+
'id' => 1,
210+
'name' => 'Comedian',
211+
'description' => 'Theres something in your teeth',
212+
];
213+
$builder->insert($jobData);
214+
215+
$this->assertFalse($this->db->transStatus());
216+
217+
$this->db->transComplete();
218+
219+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
220+
221+
// The second transaction group
222+
$this->db->transStart();
223+
224+
$jobData = [
225+
'name' => 'Comedian',
226+
'description' => 'Theres something in your teeth',
227+
];
228+
$builder->insert($jobData);
229+
230+
$this->assertTrue($this->db->transStatus());
231+
232+
$this->db->transComplete();
233+
234+
$this->seeInDatabase('job', ['name' => 'Comedian']);
235+
236+
$this->enableDBDebug();
237+
}
238+
}

0 commit comments

Comments
 (0)