Skip to content

Commit

Permalink
WIP DatabaseTransactionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Oct 13, 2024
1 parent ab24469 commit d1d4eb6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions tests/WordPress/Orm/DatabaseTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,28 @@ public function testTransactionRollback(): void
*/
$this->db->delete(sprintf('DELETE FROM %s WHERE fake_column = %d;', $this->tableName, $query));
});
} catch (\Exception $exception) {
} catch (\Exception) {
// Off exception
$this->assertInstanceOf(QueryException::class, $exception);
}

$this->assertTransaction('rollback');

$items = $this->model::all();
$this->assertCount(2, $items->toArray(), 'There must be only 2 items because the transaction was rollback.');
$this->assertEquals(['deposit-1', 'deposit-2'], $items->pluck('url'));
$this->assertEquals(['deposit-1', 'deposit-2'], $items->pluck('url')->toArray());
}

/**
* @return void
* @throws \Throwable
* @covers Database::transaction
*/
public function testTransactionThrowsQueryException(): void
{
$this->expectException(QueryException::class);
$this->db->transaction(function () {
$this->db->delete('DELETE FROM fake_table;');
});
}

/**
Expand All @@ -125,6 +137,7 @@ private function assertTransaction(string $mode): void
$firstQuery = reset($query)[0] ?? '';
$lastQuery = end($query)[0] ?? '';
$this->assertEquals('START TRANSACTION;', $firstQuery);
$this->assertEquals(0, $this->db->transactionCount);

if ($mode === 'commit') {
$this->assertEquals('COMMIT;', $lastQuery);
Expand Down

0 comments on commit d1d4eb6

Please sign in to comment.