Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
49 / 49
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
10pest()->group('integration');
11
12use Phexium\Plugin\Transaction\Adapter\SqliteTransaction;
13use Tests\Phexium\Component\Support\PdoRegistry;
14
15// @codeCoverageIgnoreStart
16$dbName = null;
17beforeAll(function () use (&$dbName): void {
18    $dbName = PdoRegistry::initializeSqlite();
19});
20afterAll(function (): void {
21    PdoRegistry::cleanupSqlite();
22});
23// @codeCoverageIgnoreEnd
24
25beforeEach(function (): void {
26    $this->pdo = PdoRegistry::getConnection();
27});
28
29afterEach(function (): void {
30    if ($this->pdo->inTransaction()) {
31        $this->pdo->rollBack();
32    }
33    $this->pdo->exec('DELETE FROM sample');
34});
35
36describe('Transaction lifecycle', function (): void {
37    it('starts a transaction on begin', function (): void {
38        $transaction = new SqliteTransaction($this->pdo);
39        expect($this->pdo->inTransaction())->toBeFalse();
40
41        $transaction->begin();
42
43        expect($this->pdo->inTransaction())->toBeTrue();
44    });
45
46    it('ends the transaction on commit', function (): void {
47        $transaction = new SqliteTransaction($this->pdo);
48        $transaction->begin();
49        expect($this->pdo->inTransaction())->toBeTrue();
50
51        $transaction->commit();
52
53        expect($this->pdo->inTransaction())->toBeFalse();
54    });
55
56    it('ends the transaction on rollback', function (): void {
57        $transaction = new SqliteTransaction($this->pdo);
58        $transaction->begin();
59        expect($this->pdo->inTransaction())->toBeTrue();
60
61        $transaction->rollback();
62
63        expect($this->pdo->inTransaction())->toBeFalse();
64    });
65});
66
67describe('Data persistence', function (): void {
68    it('persists changes after commit', function (): void {
69        $transaction = new SqliteTransaction($this->pdo);
70        $transaction->begin();
71        $this->pdo->exec('INSERT INTO sample (id, name) VALUES (1, "test")');
72        expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(1);
73
74        $transaction->commit();
75
76        expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(1);
77    });
78
79    it('rolls back changes after rollback', function (): void {
80        $transaction = new SqliteTransaction($this->pdo);
81        $transaction->begin();
82        $this->pdo->exec('INSERT INTO sample (id, name) VALUES (1, "test")');
83        expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(1);
84
85        $transaction->rollback();
86
87        expect($this->pdo->query('SELECT COUNT(*) FROM sample')->fetchColumn())->toBe(0);
88    });
89});