Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
57 / 57
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\SqlDriver\Adapter\InMemoryDriver;
11
12// @codeCoverageIgnoreStart
13require_once __DIR__.'/BaseSqlDriverTests.php';
14registerBaseSqlDriverTests();
15// @codeCoverageIgnoreEnd
16
17beforeEach(function (): void {
18    $this->driver = new InMemoryDriver();
19});
20
21test('reset clears table storage', function (): void {
22    $this->driver->save('sample', ['id' => 1, 'name' => 'Book 1']);
23    $this->driver->save('sample', ['id' => 2, 'name' => 'Book 2']);
24
25    $this->driver->reset('sample');
26
27    expect($this->driver->findAll('sample'))->toBe([]);
28});
29
30test('reset only affects specified table', function (): void {
31    $this->driver->save('sample', ['id' => 1, 'name' => 'Sample']);
32    $this->driver->save('other', ['id' => 1, 'name' => 'Other']);
33
34    $this->driver->reset('sample');
35
36    expect($this->driver->findAll('sample'))->toBe([])
37        ->and($this->driver->findAll('other'))->toHaveCount(1)
38    ;
39});
40
41test('save throws exception when row has no id', function (): void {
42    expect(fn () => $this->driver->save('sample', ['name' => 'No ID']))
43        ->toThrow(InvalidArgumentException::class, 'Row must have an "id" key')
44    ;
45});
46
47test('findBy applies offset without limit', function (): void {
48    $this->driver->save('sample', ['id' => 1, 'name' => 'A']);
49    $this->driver->save('sample', ['id' => 2, 'name' => 'A']);
50    $this->driver->save('sample', ['id' => 3, 'name' => 'A']);
51
52    $spec = createSqlSpecStub('name = :name', ['name' => 'A']);
53
54    $result = $this->driver->findBy('sample', $spec, null, 1, null);
55
56    expect($result)->toHaveCount(2);
57});
58
59test('findBy with offset skips correct number of rows', function (): void {
60    $this->driver->save('sample', ['id' => 1, 'name' => 'First']);
61    $this->driver->save('sample', ['id' => 2, 'name' => 'Second']);
62    $this->driver->save('sample', ['id' => 3, 'name' => 'Third']);
63
64    $spec = createSqlSpecStub('1=1', []);
65
66    $result = $this->driver->findBy('sample', $spec, ['id' => 'ASC'], 1, null);
67
68    expect($result[0]['name'])->toBe('Second');
69});
70
71test('findBy sorting compares both row values', function (): void {
72    $this->driver->save('sample', ['id' => 1, 'priority' => 3]);
73    $this->driver->save('sample', ['id' => 2, 'priority' => 1]);
74    $this->driver->save('sample', ['id' => 3, 'priority' => 2]);
75
76    $spec = createSqlSpecStub('1=1', []);
77
78    $result = $this->driver->findBy('sample', $spec, ['priority' => 'ASC']);
79
80    expect($result[0]['priority'])->toBe(1)
81        ->and($result[1]['priority'])->toBe(2)
82        ->and($result[2]['priority'])->toBe(3)
83    ;
84});
85
86test('findBy with limit but no offset starts from first element', function (): void {
87    $this->driver->save('sample', ['id' => 1, 'name' => 'First']);
88    $this->driver->save('sample', ['id' => 2, 'name' => 'Second']);
89    $this->driver->save('sample', ['id' => 3, 'name' => 'Third']);
90
91    $spec = createSqlSpecStub('1=1', []);
92
93    $result = $this->driver->findBy('sample', $spec, ['id' => 'ASC'], null, 1);
94
95    expect($result[0]['name'])->toBe('First');
96});