Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3// ╔════════════════════════════════════════════════════════════╗
4// ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║
5// ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com>     ║
6// ╚════════════════════════════════════════════════════════════╝
7
8declare(strict_types=1);
9
10use Phexium\Plugin\Transaction\Adapter\MysqlTransaction;
11use Tests\Phexium\Integration\Support\PdoRegistry;
12
13// @codeCoverageIgnoreStart
14$dbName = null;
15beforeAll(function () use (&$dbName): void {
16    $dbName = PdoRegistry::initializeMysql();
17});
18afterAll(function () use (&$dbName): void {
19    PdoRegistry::cleanupMysql($dbName);
20});
21// @codeCoverageIgnoreEnd
22
23beforeEach(function (): void {
24    $this->pdo = PdoRegistry::getConnection();
25});
26
27afterEach(function (): void {
28    if ($this->pdo->inTransaction()) {
29        $this->pdo->rollBack();
30    }
31    $this->pdo->exec('DELETE FROM sample');
32});
33
34test('Begin starts transaction', function (): void {
35    $transaction = new MysqlTransaction($this->pdo);
36    expect($transaction->inTransaction())->toBeFalse();
37
38    $transaction->begin();
39
40    expect($transaction->inTransaction())->toBeTrue();
41});
42
43test('Commit ends transaction', function (): void {
44    $transaction = new MysqlTransaction($this->pdo);
45    $transaction->begin();
46    expect($transaction->inTransaction())->toBeTrue();
47
48    $transaction->commit();
49
50    expect($transaction->inTransaction())->toBeFalse();
51});
52
53test('Rollback ends transaction', function (): void {
54    $transaction = new MysqlTransaction($this->pdo);
55    $transaction->begin();
56    expect($transaction->inTransaction())->toBeTrue();
57
58    $transaction->rollback();
59
60    expect($transaction->inTransaction())->toBeFalse();
61});
62
63test('Changes are persisted after commit', function (): void {
64    $transaction = new MysqlTransaction($this->pdo);
65    $transaction->begin();
66    $this->pdo->exec('INSERT INTO sample (id, name) VALUES (1, "test")');
67    expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(1);
68
69    $transaction->commit();
70
71    expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(1);
72});
73
74test('Changes are rolled back after rollback', function (): void {
75    $transaction = new MysqlTransaction($this->pdo);
76    $transaction->begin();
77    $this->pdo->exec('INSERT INTO sample (id, name) VALUES (1, "test")');
78    expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(1);
79
80    $transaction->rollback();
81
82    expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(0);
83});