From 05f4768e43c42bc56088ee852eac4dbc156e015b Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 13 Jun 2022 11:19:42 +0900 Subject: [PATCH 1/5] test: add disableDBDebug() and enableDBDebug() --- system/Test/DatabaseTestTrait.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); + } } From a6d75726593f238c184a4fba5e940d97f22121d7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 13 Jun 2022 11:26:33 +0900 Subject: [PATCH 2/5] test: refactor to use disableDBDebug() and enableDBDebug() --- tests/system/Database/Live/BadQueryTest.php | 19 +++---------------- tests/system/Database/Live/DbDebugTest.php | 7 ++++--- tests/system/Database/Live/DbUtilsTest.php | 21 ++++----------------- tests/system/Models/DeleteModelTest.php | 6 ++++-- tests/system/Models/InsertModelTest.php | 3 ++- tests/system/Models/SaveModelTest.php | 6 ++++-- tests/system/Models/UpdateModelTest.php | 3 ++- 7 files changed, 23 insertions(+), 42 deletions(-) diff --git a/tests/system/Database/Live/BadQueryTest.php b/tests/system/Database/Live/BadQueryTest.php index 7a0c81ff8b2c..195afe04751f 100644 --- a/tests/system/Database/Live/BadQueryTest.php +++ b/tests/system/Database/Live/BadQueryTest.php @@ -27,22 +27,10 @@ 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'); @@ -53,12 +41,11 @@ 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..d2a64b0ee62a 100644 --- a/tests/system/Database/Live/DbDebugTest.php +++ b/tests/system/Database/Live/DbDebugTest.php @@ -27,21 +27,22 @@ 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..e95f0118cd5e 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,8 +114,7 @@ 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.'); @@ -140,14 +128,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..8d21fde25226 100644 --- a/tests/system/Models/DeleteModelTest.php +++ b/tests/system/Models/DeleteModelTest.php @@ -34,7 +34,8 @@ 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']); @@ -68,7 +69,8 @@ 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]); diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index 19a8c6d8dfb5..85bf753d1070 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -147,7 +147,8 @@ 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', diff --git a/tests/system/Models/SaveModelTest.php b/tests/system/Models/SaveModelTest.php index 48b63c14b211..c13212d5620e 100644 --- a/tests/system/Models/SaveModelTest.php +++ b/tests/system/Models/SaveModelTest.php @@ -55,7 +55,8 @@ 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 = [ @@ -84,7 +85,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 = [ diff --git a/tests/system/Models/UpdateModelTest.php b/tests/system/Models/UpdateModelTest.php index 5b6c9ca75972..d8a054f247f5 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -96,7 +96,8 @@ 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(); $data = [ 'name' => 'Foo', From 9e1de18f5926884648ad317fb801d54020e09bdf Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 13 Jun 2022 11:27:54 +0900 Subject: [PATCH 3/5] test: add missing $this->enableDBDebug() --- tests/system/Models/DeleteModelTest.php | 4 ++++ tests/system/Models/InsertModelTest.php | 8 ++------ tests/system/Models/SaveModelTest.php | 4 ++++ tests/system/Models/UpdateModelTest.php | 2 ++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/tests/system/Models/DeleteModelTest.php b/tests/system/Models/DeleteModelTest.php index 8d21fde25226..56c548d0ba2f 100644 --- a/tests/system/Models/DeleteModelTest.php +++ b/tests/system/Models/DeleteModelTest.php @@ -42,6 +42,8 @@ public function testDeleteFail(): void $result = $this->model->where('name123', 'Developer')->delete(); $this->assertFalse($result); $this->seeInDatabase('job', ['name' => 'Developer']); + + $this->enableDBDebug(); } public function testDeleteStringPrimaryKey(): void @@ -77,6 +79,8 @@ public function testDeleteWithSoftDeleteFail(): void $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 85bf753d1070..2db16beabc62 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', [ @@ -162,6 +156,8 @@ public function testInsertResultFail(): void $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 c13212d5620e..984ae4df1bd7 100644 --- a/tests/system/Models/SaveModelTest.php +++ b/tests/system/Models/SaveModelTest.php @@ -67,6 +67,8 @@ public function testSaveNewRecordArrayFail(): void $result = $this->model->protect(false)->save($data); $this->assertFalse($result); $this->dontSeeInDatabase('job', ['name' => 'Apprentice']); + + $this->enableDBDebug(); } public function testSaveUpdateRecordArray(): void @@ -98,6 +100,8 @@ public function testSaveUpdateRecordArrayFail(): void $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 d8a054f247f5..29d7fb12e2ad 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -113,6 +113,8 @@ public function testUpdateResultFail(): void $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 From d20d0213d241de8fb59c5c5b0a3f448b24a53a61 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 13 Jun 2022 11:28:21 +0900 Subject: [PATCH 4/5] test: add empty lines or so To make more readable. --- tests/system/Database/Live/BadQueryTest.php | 4 ++++ tests/system/Database/Live/DbDebugTest.php | 5 +++++ tests/system/Database/Live/DbUtilsTest.php | 1 + tests/system/Models/DeleteModelTest.php | 6 ++++++ tests/system/Models/InsertModelTest.php | 4 +++- tests/system/Models/SaveModelTest.php | 4 ++-- tests/system/Models/UpdateModelTest.php | 5 +++-- 7 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/system/Database/Live/BadQueryTest.php b/tests/system/Database/Live/BadQueryTest.php index 195afe04751f..86189c8b9f87 100644 --- a/tests/system/Database/Live/BadQueryTest.php +++ b/tests/system/Database/Live/BadQueryTest.php @@ -31,8 +31,10 @@ final class BadQueryTest extends CIUnitTestCase public function testBadQueryDebugTrue() { $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 @@ -42,8 +44,10 @@ public function testBadQueryDebugFalse() { // WARNING this value will persist! take care to roll it back. $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); $this->enableDBDebug(); diff --git a/tests/system/Database/Live/DbDebugTest.php b/tests/system/Database/Live/DbDebugTest.php index d2a64b0ee62a..0322ce192961 100644 --- a/tests/system/Database/Live/DbDebugTest.php +++ b/tests/system/Database/Live/DbDebugTest.php @@ -28,7 +28,9 @@ final class DbDebugTest extends CIUnitTestCase public function testDBDebugTrue() { $this->enableDBDebug(); + $this->expectException('Exception'); + $this->db->simpleQuery('SELECT * FROM db_error'); } @@ -36,13 +38,16 @@ public function testDBDebugFalse() { // 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->enableDBDebug(); + parent::tearDown(); } } diff --git a/tests/system/Database/Live/DbUtilsTest.php b/tests/system/Database/Live/DbUtilsTest.php index e95f0118cd5e..d4750044b64c 100644 --- a/tests/system/Database/Live/DbUtilsTest.php +++ b/tests/system/Database/Live/DbUtilsTest.php @@ -118,6 +118,7 @@ public function testUtilsOptimizeTableFalseOptimizeDatabaseDebugTrue() $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 diff --git a/tests/system/Models/DeleteModelTest.php b/tests/system/Models/DeleteModelTest.php index 56c548d0ba2f..0e47d8535a90 100644 --- a/tests/system/Models/DeleteModelTest.php +++ b/tests/system/Models/DeleteModelTest.php @@ -36,10 +36,13 @@ public function testDeleteFail(): void { // 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']); @@ -73,10 +76,13 @@ public function testDeleteWithSoftDeleteFail(): void { // 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]); diff --git a/tests/system/Models/InsertModelTest.php b/tests/system/Models/InsertModelTest.php index 2db16beabc62..a648c73da5b3 100644 --- a/tests/system/Models/InsertModelTest.php +++ b/tests/system/Models/InsertModelTest.php @@ -148,12 +148,14 @@ public function testInsertResultFail(): void '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]); diff --git a/tests/system/Models/SaveModelTest.php b/tests/system/Models/SaveModelTest.php index 984ae4df1bd7..0ff142375fde 100644 --- a/tests/system/Models/SaveModelTest.php +++ b/tests/system/Models/SaveModelTest.php @@ -63,8 +63,8 @@ public function testSaveNewRecordArrayFail(): void 'name123' => 'Apprentice', 'description' => 'That thing you do.', ]; - $result = $this->model->protect(false)->save($data); + $this->assertFalse($result); $this->dontSeeInDatabase('job', ['name' => 'Apprentice']); @@ -96,8 +96,8 @@ 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']); diff --git a/tests/system/Models/UpdateModelTest.php b/tests/system/Models/UpdateModelTest.php index 29d7fb12e2ad..6ebe8e9fd738 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -98,6 +98,7 @@ public function testUpdateResultFail(): void { // WARNING this value will persist! take care to roll it back. $this->disableDBDebug(); + $this->createModel(UserModel::class); $data = [ 'name' => 'Foo', @@ -105,12 +106,12 @@ 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']); From 3e373b5011811c709445122216d98bd926d75b69 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 13 Jun 2022 12:42:35 +0900 Subject: [PATCH 5/5] test: fix missing email (not null column) --- tests/system/Models/UpdateModelTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/system/Models/UpdateModelTest.php b/tests/system/Models/UpdateModelTest.php index 6ebe8e9fd738..1c7b4f6b1cf1 100644 --- a/tests/system/Models/UpdateModelTest.php +++ b/tests/system/Models/UpdateModelTest.php @@ -330,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;