Merge pull request #6115 from kenjis/fix-DBDebug-false-tests

test: fix forgetting to restore DBDebug value
This commit is contained in:
kenjis 2022-06-15 09:10:17 +09:00 committed by GitHub
commit 89223f7a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 78 additions and 53 deletions

View File

@ -323,4 +323,22 @@ trait DatabaseTestTrait
$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);
}
}

View File

@ -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 @@ final class BadQueryTest extends CIUnitTestCase
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();
}
}

View File

@ -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();
}
}

View File

@ -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 @@ final class DbUtilsTest extends CIUnitTestCase
$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 @@ final class DbUtilsTest extends CIUnitTestCase
$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()

View File

@ -34,13 +34,19 @@ final class DeleteModelTest extends LiveModelTestCase
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 @@ final class DeleteModelTest extends LiveModelTestCase
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

View File

@ -26,12 +26,6 @@ use Tests\Support\Models\WithoutAutoIncrementModel;
*/
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 @@ final class InsertModelTest extends LiveModelTestCase
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

View File

@ -55,17 +55,20 @@ final class SaveModelTest extends LiveModelTestCase
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 @@ final class SaveModelTest extends LiveModelTestCase
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 @@ final class SaveModelTest extends LiveModelTestCase
'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

View File

@ -96,7 +96,9 @@ final class UpdateModelTest extends LiveModelTestCase
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 @@ final class UpdateModelTest extends LiveModelTestCase
'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 @@ final class UpdateModelTest extends LiveModelTestCase
$entity->id = 1;
$entity->name = 'Jones Martin';
$entity->email = 'jones@example.org';
$entity->country = 'India';
$entity->deleted = 0;