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