Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix forgetting to restore DBDebug value #6115

Merged
merged 5 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions system/Test/DatabaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
23 changes: 7 additions & 16 deletions tests/system/Database/Live/BadQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
}
12 changes: 9 additions & 3 deletions tests/system/Database/Live/DbDebugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
22 changes: 5 additions & 17 deletions tests/system/Database/Live/DbUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
16 changes: 14 additions & 2 deletions tests/system/Models/DeleteModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions tests/system/Models/InsertModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions tests/system/Models/SaveModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -84,18 +87,21 @@ 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 = [
'id' => 1,
'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
Expand Down
11 changes: 8 additions & 3 deletions tests/system/Models/UpdateModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,26 @@ 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',
'email' => 'foo@example.com',
'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
Expand Down Expand Up @@ -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;

Expand Down