Skip to content

Commit 96ee2f7

Browse files
committed
test: add DB transaction tests
1 parent f06cceb commit 96ee2f7

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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\Database\Exceptions\DatabaseException;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use CodeIgniter\Test\DatabaseTestTrait;
17+
use Tests\Support\Database\Seeds\CITestSeeder;
18+
19+
/**
20+
* @group DatabaseLive
21+
*
22+
* @internal
23+
*/
24+
final class TransactionTest extends CIUnitTestCase
25+
{
26+
use DatabaseTestTrait;
27+
28+
protected $refresh = true;
29+
protected $seed = CITestSeeder::class;
30+
31+
/**
32+
* Sets $DBDebug to false.
33+
*
34+
* WARNING: this value will persist! take care to roll it back.
35+
*/
36+
protected function disableDBDebug(): void
37+
{
38+
$this->setPrivateProperty($this->db, 'DBDebug', false);
39+
}
40+
41+
/**
42+
* Sets $DBDebug to true.
43+
*/
44+
protected function enableDBDebug(): void
45+
{
46+
$this->setPrivateProperty($this->db, 'DBDebug', true);
47+
}
48+
49+
public function testTransStartDBDebugTrue()
50+
{
51+
$builder = $this->db->table('job');
52+
53+
try {
54+
$this->db->transStart();
55+
56+
$jobData = [
57+
'name' => 'Grocery Sales',
58+
'description' => 'Discount!',
59+
];
60+
$builder->insert($jobData);
61+
62+
// Duplicate entry '1' for key 'PRIMARY'
63+
$jobData = [
64+
'id' => 1,
65+
'name' => 'Comedian',
66+
'description' => 'Theres something in your teeth',
67+
];
68+
$builder->insert($jobData);
69+
70+
$this->db->transComplete();
71+
} catch (DatabaseException $e) {
72+
// Do nothing.
73+
}
74+
75+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
76+
}
77+
78+
public function testTransStartDBDebugFalse()
79+
{
80+
$this->disableDBDebug();
81+
82+
$builder = $this->db->table('job');
83+
84+
$this->db->transStart();
85+
86+
$jobData = [
87+
'name' => 'Grocery Sales',
88+
'description' => 'Discount!',
89+
];
90+
$builder->insert($jobData);
91+
92+
// Duplicate entry '1' for key 'PRIMARY'
93+
$jobData = [
94+
'id' => 1,
95+
'name' => 'Comedian',
96+
'description' => 'Theres something in your teeth',
97+
];
98+
$builder->insert($jobData);
99+
100+
$this->db->transComplete();
101+
102+
$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
103+
104+
$this->enableDBDebug();
105+
}
106+
}

0 commit comments

Comments
 (0)