Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| RecordingUnitOfWork | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| run | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace LambdaTwelve\OneRecord\Testing; |
| 6 | |
| 7 | use LambdaTwelve\OneRecord\Server\Spi\UnitOfWork; |
| 8 | use Throwable; |
| 9 | |
| 10 | /** |
| 11 | * Counts units and tracks nesting, the way a host's transaction manager |
| 12 | * would with a depth counter: only the outermost run() is a "transaction". |
| 13 | */ |
| 14 | final class RecordingUnitOfWork implements UnitOfWork |
| 15 | { |
| 16 | public int $transactions = 0; |
| 17 | public int $rolledBack = 0; |
| 18 | public int $maxDepth = 0; |
| 19 | private int $depth = 0; |
| 20 | |
| 21 | public function run(callable $work): mixed |
| 22 | { |
| 23 | if ($this->depth === 0) { |
| 24 | $this->transactions++; |
| 25 | } |
| 26 | $this->depth++; |
| 27 | $this->maxDepth = max($this->maxDepth, $this->depth); |
| 28 | try { |
| 29 | return $work(); |
| 30 | } catch (Throwable $e) { |
| 31 | if ($this->depth === 1) { |
| 32 | $this->rolledBack++; |
| 33 | } |
| 34 | throw $e; |
| 35 | } finally { |
| 36 | $this->depth--; |
| 37 | } |
| 38 | } |
| 39 | } |