diff --git a/system/Test/DatabaseTestTrait.php b/system/Test/DatabaseTestTrait.php index d15a5fb44480..539627e29189 100644 --- a/system/Test/DatabaseTestTrait.php +++ b/system/Test/DatabaseTestTrait.php @@ -323,4 +323,22 @@ public function seeNumRecords(int $expected, string $table, array $where) $this->assertEquals($expected, $count, 'Wrong number of matching rows in database.'); } + + /** + * Sets $DBDebug to false. + * + * WARNING: this value will persist! take care to roll it back. + */ + protected function disableDBDebug(): void + { + $this->setPrivateProperty($this->db, 'DBDebug', false); + } + + /** + * Sets $DBDebug to true. + */ + protected function enableDBDebug(): void + { + $this->setPrivateProperty($this->db, 'DBDebug', true); + } } diff --git a/tests/system/Database/Live/BadQueryTest.php b/tests/system/Database/Live/BadQueryTest.php index 7a0c81ff8b2c..86189c8b9f87 100644 --- a/tests/system/Database/Live/BadQueryTest.php +++ b/tests/system/Database/Live/BadQueryTest.php @@ -27,24 +27,14 @@ final class BadQueryTest extends CIUnitTestCase protected $refresh = true; protected $seed = CITestSeeder::class; - private static $origDebug; - - /** - * This test must run first to store the inital debug value before we tinker with it below - */ - public function testFirst() - { - $this::$origDebug = $this->getPrivateProperty($this->db, 'DBDebug'); - - $this->assertIsBool($this::$origDebug); - } public function testBadQueryDebugTrue() { - // WARNING this value will persist! take care to roll it back. - $this->setPrivateProperty($this->db, 'DBDebug', true); + $this->enableDBDebug(); + // expect an exception, class and message varies by DBMS $this->expectException(Exception::class); + $this->db->query('SELECT * FROM table_does_not_exist'); // this code is never executed @@ -53,12 +43,13 @@ public function testBadQueryDebugTrue() public function testBadQueryDebugFalse() { // WARNING this value will persist! take care to roll it back. - $this->setPrivateProperty($this->db, 'DBDebug', false); + $this->disableDBDebug(); + // this throws an exception when DBDebug is true, but it'll return FALSE when DBDebug is false $query = $this->db->query('SELECT * FROM table_does_not_exist'); + $this->assertFalse($query); - // restore the DBDebug value in effect when this unit test began - $this->setPrivateProperty($this->db, 'DBDebug', self::$origDebug); + $this->enableDBDebug(); } } diff --git a/tests/system/Database/Live/DbDebugTest.php b/tests/system/Database/Live/DbDebugTest.php index 8caadf5144fe..0322ce192961 100644 --- a/tests/system/Database/Live/DbDebugTest.php +++ b/tests/system/Database/Live/DbDebugTest.php @@ -27,21 +27,27 @@ final class DbDebugTest extends CIUnitTestCase public function testDBDebugTrue() { - $this->setPrivateProperty($this->db, 'DBDebug', true); + $this->enableDBDebug(); + $this->expectException('Exception'); + $this->db->simpleQuery('SELECT * FROM db_error'); } public function testDBDebugFalse() { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); + $result = $this->db->simpleQuery('SELECT * FROM db_error'); + $this->assertFalse($result); } protected function tearDown(): void { - $this->setPrivateProperty($this->db, 'DBDebug', true); + $this->enableDBDebug(); + parent::tearDown(); } } diff --git a/tests/system/Database/Live/DbUtilsTest.php b/tests/system/Database/Live/DbUtilsTest.php index 964e024b24c9..d4750044b64c 100644 --- a/tests/system/Database/Live/DbUtilsTest.php +++ b/tests/system/Database/Live/DbUtilsTest.php @@ -28,17 +28,6 @@ final class DbUtilsTest extends CIUnitTestCase protected $refresh = true; protected $seed = CITestSeeder::class; - private static $origDebug; - - /** - * This test must run first to store the inital debug value before we tinker with it below - */ - public function testFirst() - { - $this::$origDebug = $this->getPrivateProperty($this->db, 'DBDebug'); - - $this->assertIsBool($this::$origDebug); - } public function testUtilsBackup() { @@ -125,11 +114,11 @@ public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugTrue() $util = (new Database())->loadUtils($this->db); $this->setPrivateProperty($util, 'optimizeTable', false); - // set debug to true -- WARNING this change will persist! - $this->setPrivateProperty($this->db, 'DBDebug', true); + $this->enableDBDebug(); $this->expectException(DatabaseException::class); $this->expectExceptionMessage('Unsupported feature of the database platform you are using.'); + $util->optimizeDatabase(); // this point in code execution will never be reached @@ -140,14 +129,13 @@ public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugFalse() $util = (new Database())->loadUtils($this->db); $this->setPrivateProperty($util, 'optimizeTable', false); - // set debug to false -- WARNING this change will persist! - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); $result = $util->optimizeDatabase(); $this->assertFalse($result); - // restore original value grabbed from testFirst -- WARNING this change will persist! - $this->setPrivateProperty($this->db, 'DBDebug', self::$origDebug); + $this->enableDBDebug(); } public function testUtilsOptimizeTable() diff --git a/tests/system/Models/DeleteModelTest.php b/tests/system/Models/DeleteModelTest.php index 1d25181068d6..0e47d8535a90 100644 --- a/tests/system/Models/DeleteModelTest.php +++ b/tests/system/Models/DeleteModelTest.php @@ -34,13 +34,19 @@ public function testDeleteBasics(): void public function testDeleteFail(): void { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); + $this->createModel(JobModel::class); + $this->seeInDatabase('job', ['name' => 'Developer']); $result = $this->model->where('name123', 'Developer')->delete(); + $this->assertFalse($result); $this->seeInDatabase('job', ['name' => 'Developer']); + + $this->enableDBDebug(); } public function testDeleteStringPrimaryKey(): void @@ -68,13 +74,19 @@ public function testDeleteWithSoftDeletes(): void public function testDeleteWithSoftDeleteFail(): void { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); + $this->createModel(UserModel::class); + $this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]); $result = $this->model->where('name123', 'Derek Jones')->delete(); + $this->assertFalse($result); $this->seeInDatabase('user', ['name' => 'Derek Jones', 'deleted_at IS NULL' => null]); + + $this->enableDBDebug(); } public function testDeleteWithSoftDeletesPurge(): void diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index 19a8c6d8dfb5..a648c73da5b3 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -26,12 +26,6 @@ */ final class InsertModelTest extends LiveModelTestCase { - protected function tearDown(): void - { - parent::tearDown(); - $this->setPrivateProperty($this->db, 'DBDebug', true); - } - public function testSetWorksWithInsert(): void { $this->dontSeeInDatabase('user', [ @@ -147,20 +141,25 @@ public function testInsertResult(): void public function testInsertResultFail(): void { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); $data = [ 'name123' => 'Apprentice', 'description' => 'That thing you do.', ]; - $this->createModel(JobModel::class); + $result = $this->model->protect(false)->insert($data, false); + $this->assertFalse($result); $lastInsertId = $this->model->getInsertID(); + $this->assertSame(0, $lastInsertId); $this->dontSeeInDatabase('job', ['id' => $lastInsertId]); + + $this->enableDBDebug(); } public function testInsertBatchNewEntityWithDateTime(): void diff --git a/tests/system/Models/SaveModelTest.php b/tests/system/Models/SaveModelTest.php index 48b63c14b211..0ff142375fde 100644 --- a/tests/system/Models/SaveModelTest.php +++ b/tests/system/Models/SaveModelTest.php @@ -55,17 +55,20 @@ public function testSaveNewRecordArray(): void public function testSaveNewRecordArrayFail(): void { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); $this->createModel(JobModel::class); $data = [ 'name123' => 'Apprentice', 'description' => 'That thing you do.', ]; - $result = $this->model->protect(false)->save($data); + $this->assertFalse($result); $this->dontSeeInDatabase('job', ['name' => 'Apprentice']); + + $this->enableDBDebug(); } public function testSaveUpdateRecordArray(): void @@ -84,7 +87,8 @@ public function testSaveUpdateRecordArray(): void public function testSaveUpdateRecordArrayFail(): void { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); $this->createModel(JobModel::class); $data = [ @@ -92,10 +96,12 @@ public function testSaveUpdateRecordArrayFail(): void 'name123' => 'Apprentice', 'description' => 'That thing you do.', ]; - $result = $this->model->protect(false)->save($data); + $this->assertFalse($result); $this->dontSeeInDatabase('job', ['name' => 'Apprentice']); + + $this->enableDBDebug(); } public function testSaveUpdateRecordObject(): void diff --git a/tests/system/Models/UpdateModelTest.php b/tests/system/Models/UpdateModelTest.php index 5b6c9ca75972..1c7b4f6b1cf1 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -96,7 +96,9 @@ public function testUpdateArray(): void public function testUpdateResultFail(): void { - $this->setPrivateProperty($this->db, 'DBDebug', false); + // WARNING this value will persist! take care to roll it back. + $this->disableDBDebug(); + $this->createModel(UserModel::class); $data = [ 'name' => 'Foo', @@ -104,14 +106,16 @@ public function testUpdateResultFail(): void 'country' => 'US', 'deleted' => 0, ]; - - $this->createModel(UserModel::class); $this->model->insert($data); $this->setPrivateProperty($this->model, 'allowedFields', ['name123']); + $result = $this->model->update(1, ['name123' => 'Foo Bar 1']); + $this->assertFalse($result); $this->dontSeeInDatabase('user', ['id' => 1, 'name' => 'Foo Bar 1']); + + $this->enableDBDebug(); } public function testUpdateBatchSuccess(): void @@ -326,6 +330,7 @@ public function testUpdateWithEntityNoAllowedFields(): void $entity->id = 1; $entity->name = 'Jones Martin'; + $entity->email = 'jones@example.org'; $entity->country = 'India'; $entity->deleted = 0;