Skip to content

Commit 89223f7

Browse files
authored
Merge pull request #6115 from kenjis/fix-DBDebug-false-tests
test: fix forgetting to restore DBDebug value
2 parents c4901f4 + 3e373b5 commit 89223f7

8 files changed

Lines changed: 78 additions & 53 deletions

File tree

system/Test/DatabaseTestTrait.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,22 @@ public function seeNumRecords(int $expected, string $table, array $where)
323323

324324
$this->assertEquals($expected, $count, 'Wrong number of matching rows in database.');
325325
}
326+
327+
/**
328+
* Sets $DBDebug to false.
329+
*
330+
* WARNING: this value will persist! take care to roll it back.
331+
*/
332+
protected function disableDBDebug(): void
333+
{
334+
$this->setPrivateProperty($this->db, 'DBDebug', false);
335+
}
336+
337+
/**
338+
* Sets $DBDebug to true.
339+
*/
340+
protected function enableDBDebug(): void
341+
{
342+
$this->setPrivateProperty($this->db, 'DBDebug', true);
343+
}
326344
}

tests/system/Database/Live/BadQueryTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,14 @@ final class BadQueryTest extends CIUnitTestCase
2727

2828
protected $refresh = true;
2929
protected $seed = CITestSeeder::class;
30-
private static $origDebug;
31-
32-
/**
33-
* This test must run first to store the inital debug value before we tinker with it below
34-
*/
35-
public function testFirst()
36-
{
37-
$this::$origDebug = $this->getPrivateProperty($this->db, 'DBDebug');
38-
39-
$this->assertIsBool($this::$origDebug);
40-
}
4130

4231
public function testBadQueryDebugTrue()
4332
{
44-
// WARNING this value will persist! take care to roll it back.
45-
$this->setPrivateProperty($this->db, 'DBDebug', true);
33+
$this->enableDBDebug();
34+
4635
// expect an exception, class and message varies by DBMS
4736
$this->expectException(Exception::class);
37+
4838
$this->db->query('SELECT * FROM table_does_not_exist');
4939

5040
// this code is never executed
@@ -53,12 +43,13 @@ public function testBadQueryDebugTrue()
5343
public function testBadQueryDebugFalse()
5444
{
5545
// WARNING this value will persist! take care to roll it back.
56-
$this->setPrivateProperty($this->db, 'DBDebug', false);
46+
$this->disableDBDebug();
47+
5748
// this throws an exception when DBDebug is true, but it'll return FALSE when DBDebug is false
5849
$query = $this->db->query('SELECT * FROM table_does_not_exist');
50+
5951
$this->assertFalse($query);
6052

61-
// restore the DBDebug value in effect when this unit test began
62-
$this->setPrivateProperty($this->db, 'DBDebug', self::$origDebug);
53+
$this->enableDBDebug();
6354
}
6455
}

tests/system/Database/Live/DbDebugTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,27 @@ final class DbDebugTest extends CIUnitTestCase
2727

2828
public function testDBDebugTrue()
2929
{
30-
$this->setPrivateProperty($this->db, 'DBDebug', true);
30+
$this->enableDBDebug();
31+
3132
$this->expectException('Exception');
33+
3234
$this->db->simpleQuery('SELECT * FROM db_error');
3335
}
3436

3537
public function testDBDebugFalse()
3638
{
37-
$this->setPrivateProperty($this->db, 'DBDebug', false);
39+
// WARNING this value will persist! take care to roll it back.
40+
$this->disableDBDebug();
41+
3842
$result = $this->db->simpleQuery('SELECT * FROM db_error');
43+
3944
$this->assertFalse($result);
4045
}
4146

4247
protected function tearDown(): void
4348
{
44-
$this->setPrivateProperty($this->db, 'DBDebug', true);
49+
$this->enableDBDebug();
50+
4551
parent::tearDown();
4652
}
4753
}

tests/system/Database/Live/DbUtilsTest.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ final class DbUtilsTest extends CIUnitTestCase
2828

2929
protected $refresh = true;
3030
protected $seed = CITestSeeder::class;
31-
private static $origDebug;
32-
33-
/**
34-
* This test must run first to store the inital debug value before we tinker with it below
35-
*/
36-
public function testFirst()
37-
{
38-
$this::$origDebug = $this->getPrivateProperty($this->db, 'DBDebug');
39-
40-
$this->assertIsBool($this::$origDebug);
41-
}
4231

4332
public function testUtilsBackup()
4433
{
@@ -125,11 +114,11 @@ public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugTrue()
125114
$util = (new Database())->loadUtils($this->db);
126115
$this->setPrivateProperty($util, 'optimizeTable', false);
127116

128-
// set debug to true -- WARNING this change will persist!
129-
$this->setPrivateProperty($this->db, 'DBDebug', true);
117+
$this->enableDBDebug();
130118

131119
$this->expectException(DatabaseException::class);
132120
$this->expectExceptionMessage('Unsupported feature of the database platform you are using.');
121+
133122
$util->optimizeDatabase();
134123

135124
// this point in code execution will never be reached
@@ -140,14 +129,13 @@ public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugFalse()
140129
$util = (new Database())->loadUtils($this->db);
141130
$this->setPrivateProperty($util, 'optimizeTable', false);
142131

143-
// set debug to false -- WARNING this change will persist!
144-
$this->setPrivateProperty($this->db, 'DBDebug', false);
132+
// WARNING this value will persist! take care to roll it back.
133+
$this->disableDBDebug();
145134

146135
$result = $util->optimizeDatabase();
147136
$this->assertFalse($result);
148137

149-
// restore original value grabbed from testFirst -- WARNING this change will persist!
150-
$this->setPrivateProperty($this->db, 'DBDebug', self::$origDebug);
138+
$this->enableDBDebug();
151139
}
152140

153141
public function testUtilsOptimizeTable()

tests/system/Models/DeleteModelTest.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,19 @@ public function testDeleteBasics(): void
3434

3535
public function testDeleteFail(): void
3636
{
37-
$this->setPrivateProperty($this->db, 'DBDebug', false);
37+
// WARNING this value will persist! take care to roll it back.
38+
$this->disableDBDebug();
39+
3840
$this->createModel(JobModel::class);
41+
3942
$this->seeInDatabase('job', ['name' => 'Developer']);
4043

4144
$result = $this->model->where('name123', 'Developer')->delete();
45+
4246
$this->assertFalse($result);
4347
$this->seeInDatabase('job', ['name' => 'Developer']);
48+
49+
$this->enableDBDebug();
4450
}
4551

4652
public function testDeleteStringPrimaryKey(): void
@@ -68,13 +74,19 @@ public function testDeleteWithSoftDeletes(): void
6874

6975
public function testDeleteWithSoftDeleteFail(): void
7076
{
71-
$this->setPrivateProperty($this->db, 'DBDebug', false);
77+
// WARNING this value will persist! take care to roll it back.
78+
$this->disableDBDebug();
79+
7280
$this->createModel(UserModel::class);
81+
7382
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
7483

7584
$result = $this->model->where('name123', 'Derek Jones')->delete();
85+
7686
$this->assertFalse($result);
7787
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
88+
89+
$this->enableDBDebug();
7890
}
7991

8092
public function testDeleteWithSoftDeletesPurge(): void

tests/system/Models/InsertModelTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@
2626
*/
2727
final class InsertModelTest extends LiveModelTestCase
2828
{
29-
protected function tearDown(): void
30-
{
31-
parent::tearDown();
32-
$this->setPrivateProperty($this->db, 'DBDebug', true);
33-
}
34-
3529
public function testSetWorksWithInsert(): void
3630
{
3731
$this->dontSeeInDatabase('user', [
@@ -147,20 +141,25 @@ public function testInsertResult(): void
147141

148142
public function testInsertResultFail(): void
149143
{
150-
$this->setPrivateProperty($this->db, 'DBDebug', false);
144+
// WARNING this value will persist! take care to roll it back.
145+
$this->disableDBDebug();
151146

152147
$data = [
153148
'name123' => 'Apprentice',
154149
'description' => 'That thing you do.',
155150
];
156-
157151
$this->createModel(JobModel::class);
152+
158153
$result = $this->model->protect(false)->insert($data, false);
154+
159155
$this->assertFalse($result);
160156

161157
$lastInsertId = $this->model->getInsertID();
158+
162159
$this->assertSame(0, $lastInsertId);
163160
$this->dontSeeInDatabase('job', ['id' => $lastInsertId]);
161+
162+
$this->enableDBDebug();
164163
}
165164

166165
public function testInsertBatchNewEntityWithDateTime(): void

tests/system/Models/SaveModelTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,20 @@ public function testSaveNewRecordArray(): void
5555

5656
public function testSaveNewRecordArrayFail(): void
5757
{
58-
$this->setPrivateProperty($this->db, 'DBDebug', false);
58+
// WARNING this value will persist! take care to roll it back.
59+
$this->disableDBDebug();
5960
$this->createModel(JobModel::class);
6061

6162
$data = [
6263
'name123' => 'Apprentice',
6364
'description' => 'That thing you do.',
6465
];
65-
6666
$result = $this->model->protect(false)->save($data);
67+
6768
$this->assertFalse($result);
6869
$this->dontSeeInDatabase('job', ['name' => 'Apprentice']);
70+
71+
$this->enableDBDebug();
6972
}
7073

7174
public function testSaveUpdateRecordArray(): void
@@ -84,18 +87,21 @@ public function testSaveUpdateRecordArray(): void
8487

8588
public function testSaveUpdateRecordArrayFail(): void
8689
{
87-
$this->setPrivateProperty($this->db, 'DBDebug', false);
90+
// WARNING this value will persist! take care to roll it back.
91+
$this->disableDBDebug();
8892
$this->createModel(JobModel::class);
8993

9094
$data = [
9195
'id' => 1,
9296
'name123' => 'Apprentice',
9397
'description' => 'That thing you do.',
9498
];
95-
9699
$result = $this->model->protect(false)->save($data);
100+
97101
$this->assertFalse($result);
98102
$this->dontSeeInDatabase('job', ['name' => 'Apprentice']);
103+
104+
$this->enableDBDebug();
99105
}
100106

101107
public function testSaveUpdateRecordObject(): void

tests/system/Models/UpdateModelTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,26 @@ public function testUpdateArray(): void
9696

9797
public function testUpdateResultFail(): void
9898
{
99-
$this->setPrivateProperty($this->db, 'DBDebug', false);
99+
// WARNING this value will persist! take care to roll it back.
100+
$this->disableDBDebug();
101+
$this->createModel(UserModel::class);
100102

101103
$data = [
102104
'name' => 'Foo',
103105
'email' => 'foo@example.com',
104106
'country' => 'US',
105107
'deleted' => 0,
106108
];
107-
108-
$this->createModel(UserModel::class);
109109
$this->model->insert($data);
110110

111111
$this->setPrivateProperty($this->model, 'allowedFields', ['name123']);
112+
112113
$result = $this->model->update(1, ['name123' => 'Foo Bar 1']);
114+
113115
$this->assertFalse($result);
114116
$this->dontSeeInDatabase('user', ['id' => 1, 'name' => 'Foo Bar 1']);
117+
118+
$this->enableDBDebug();
115119
}
116120

117121
public function testUpdateBatchSuccess(): void
@@ -326,6 +330,7 @@ public function testUpdateWithEntityNoAllowedFields(): void
326330

327331
$entity->id = 1;
328332
$entity->name = 'Jones Martin';
333+
$entity->email = 'jones@example.org';
329334
$entity->country = 'India';
330335
$entity->deleted = 0;
331336

0 commit comments

Comments
 (0)