Skip to content

Commit a6d7572

Browse files
committed
test: refactor to use disableDBDebug() and enableDBDebug()
1 parent 05f4768 commit a6d7572

7 files changed

Lines changed: 23 additions & 42 deletions

File tree

tests/system/Database/Live/BadQueryTest.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,10 @@ 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();
4634
// expect an exception, class and message varies by DBMS
4735
$this->expectException(Exception::class);
4836
$this->db->query('SELECT * FROM table_does_not_exist');
@@ -53,12 +41,11 @@ public function testBadQueryDebugTrue()
5341
public function testBadQueryDebugFalse()
5442
{
5543
// WARNING this value will persist! take care to roll it back.
56-
$this->setPrivateProperty($this->db, 'DBDebug', false);
44+
$this->disableDBDebug();
5745
// this throws an exception when DBDebug is true, but it'll return FALSE when DBDebug is false
5846
$query = $this->db->query('SELECT * FROM table_does_not_exist');
5947
$this->assertFalse($query);
6048

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

tests/system/Database/Live/DbDebugTest.php

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

2828
public function testDBDebugTrue()
2929
{
30-
$this->setPrivateProperty($this->db, 'DBDebug', true);
30+
$this->enableDBDebug();
3131
$this->expectException('Exception');
3232
$this->db->simpleQuery('SELECT * FROM db_error');
3333
}
3434

3535
public function testDBDebugFalse()
3636
{
37-
$this->setPrivateProperty($this->db, 'DBDebug', false);
37+
// WARNING this value will persist! take care to roll it back.
38+
$this->disableDBDebug();
3839
$result = $this->db->simpleQuery('SELECT * FROM db_error');
3940
$this->assertFalse($result);
4041
}
4142

4243
protected function tearDown(): void
4344
{
44-
$this->setPrivateProperty($this->db, 'DBDebug', true);
45+
$this->enableDBDebug();
4546
parent::tearDown();
4647
}
4748
}

tests/system/Database/Live/DbUtilsTest.php

Lines changed: 4 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,8 +114,7 @@ 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.');
@@ -140,14 +128,13 @@ public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugFalse()
140128
$util = (new Database())->loadUtils($this->db);
141129
$this->setPrivateProperty($util, 'optimizeTable', false);
142130

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

146134
$result = $util->optimizeDatabase();
147135
$this->assertFalse($result);
148136

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

153140
public function testUtilsOptimizeTable()

tests/system/Models/DeleteModelTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ 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();
3839
$this->createModel(JobModel::class);
3940
$this->seeInDatabase('job', ['name' => 'Developer']);
4041

@@ -68,7 +69,8 @@ public function testDeleteWithSoftDeletes(): void
6869

6970
public function testDeleteWithSoftDeleteFail(): void
7071
{
71-
$this->setPrivateProperty($this->db, 'DBDebug', false);
72+
// WARNING this value will persist! take care to roll it back.
73+
$this->disableDBDebug();
7274
$this->createModel(UserModel::class);
7375
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]);
7476

tests/system/Models/InsertModelTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public function testInsertResult(): void
147147

148148
public function testInsertResultFail(): void
149149
{
150-
$this->setPrivateProperty($this->db, 'DBDebug', false);
150+
// WARNING this value will persist! take care to roll it back.
151+
$this->disableDBDebug();
151152

152153
$data = [
153154
'name123' => 'Apprentice',

tests/system/Models/SaveModelTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ 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 = [
@@ -84,7 +85,8 @@ public function testSaveUpdateRecordArray(): void
8485

8586
public function testSaveUpdateRecordArrayFail(): void
8687
{
87-
$this->setPrivateProperty($this->db, 'DBDebug', false);
88+
// WARNING this value will persist! take care to roll it back.
89+
$this->disableDBDebug();
8890
$this->createModel(JobModel::class);
8991

9092
$data = [

tests/system/Models/UpdateModelTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ 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();
100101

101102
$data = [
102103
'name' => 'Foo',

0 commit comments

Comments
 (0)