Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
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 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use Phexium\Domain\Id\TimestampId; |
| 11 | use Phexium\Domain\Specification\BinaryAndSpecification; |
| 12 | use Phexium\Domain\Specification\BinaryOrSpecification; |
| 13 | use Phexium\Domain\Specification\UnaryNotSpecification; |
| 14 | use Tests\Phexium\Fake\Domain\Specification\IdSpecification as FakeIdSpecification; |
| 15 | use Tests\Phexium\Fake\Domain\Specification\NameSpecification as FakeNameSpecification; |
| 16 | |
| 17 | test('Complex specification combines AND, OR and NOT operators: (name = Foo AND id = 123) OR (NOT name = Bar)', function (): void { |
| 18 | $nameIsFoo = new FakeNameSpecification('Foo'); |
| 19 | $idIs123 = new FakeIdSpecification(new TimestampId(123)); |
| 20 | $nameIsBar = new FakeNameSpecification('Bar'); |
| 21 | |
| 22 | $fooAnd123 = new BinaryAndSpecification($nameIsFoo, $idIs123); |
| 23 | $notBar = new UnaryNotSpecification($nameIsBar); |
| 24 | $complexSpec = new BinaryOrSpecification($fooAnd123, $notBar); |
| 25 | |
| 26 | $result = $complexSpec->toSql(); |
| 27 | |
| 28 | expect($result)->toBeArray(); |
| 29 | expect($result)->toHaveKey('sql'); |
| 30 | expect($result)->toHaveKey('params'); |
| 31 | expect($result['sql'])->toBe('((name = :name_0_0) AND (id = :id_1_0)) OR (NOT (name = :name_1))'); |
| 32 | expect($result['params'])->toBe(['name_0_0' => 'Foo', 'id_1_0' => 123, 'name_1' => 'Bar']); |
| 33 | |
| 34 | $filter = $complexSpec->toInMemoryFilter(); |
| 35 | expect($filter)->toBeCallable(); |
| 36 | |
| 37 | $rowFoo123 = ['id' => 123, 'name' => 'Foo']; |
| 38 | expect($filter($rowFoo123))->toBeTrue(); |
| 39 | |
| 40 | $rowFoo456 = ['id' => 456, 'name' => 'Foo']; |
| 41 | expect($filter($rowFoo456))->toBeTrue(); |
| 42 | |
| 43 | $rowBar123 = ['id' => 123, 'name' => 'Bar']; |
| 44 | expect($filter($rowBar123))->toBeFalse(); |
| 45 | |
| 46 | $rowBar456 = ['id' => 456, 'name' => 'Bar']; |
| 47 | expect($filter($rowBar456))->toBeFalse(); |
| 48 | |
| 49 | $rowBaz123 = ['id' => 123, 'name' => 'Baz']; |
| 50 | expect($filter($rowBaz123))->toBeTrue(); |
| 51 | |
| 52 | $rowBaz456 = ['id' => 456, 'name' => 'Baz']; |
| 53 | expect($filter($rowBaz456))->toBeTrue(); |
| 54 | }); |