packages/sql-catalog/tests/Unit/Core/Catalog/EntryIdentityTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Core\Catalog;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlCatalog\Core\Catalog\CallSite;
11use SqlCatalog\Core\Catalog\EntryIdentity;
12use SqlCatalog\Core\Text\LiteralText;
13use SqlCatalog\Core\Text\TextPattern;
14
15#[CoversClass(EntryIdentity::class)]
16#[UsesClass(CallSite::class)]
17#[UsesClass(LiteralText::class)]
18#[UsesClass(TextPattern::class)]
19final class EntryIdentityTest extends TestCase
20{
21    public function testComputeIsStableForTheSameStatement(): void
22    {
23        $identity = new EntryIdentity();
24        $site = new CallSite('a.php', 3, 'f', 'pdo.query');
25        self::assertSame(
26            $identity->compute($site, TextPattern::fromText('SELECT 1')),
27            $identity->compute($site, TextPattern::fromText('SELECT 1')),
28        );
29    }
30
31    public function testComputeIgnoresTheLineNumber(): void
32    {
33        $identity = new EntryIdentity();
34        self::assertSame(
35            $identity->compute(new CallSite('a.php', 3, 'f', 'pdo.query'), TextPattern::fromText('SELECT 1')),
36            $identity->compute(new CallSite('a.php', 90, 'f', 'pdo.query'), TextPattern::fromText('SELECT 1')),
37        );
38    }
39
40    public function testComputeSeparatesDifferentStatementsAndSites(): void
41    {
42        $identity = new EntryIdentity();
43        $site = new CallSite('a.php', 3, 'f', 'pdo.query');
44        self::assertNotSame(
45            $identity->compute($site, TextPattern::fromText('SELECT 1')),
46            $identity->compute($site, TextPattern::fromText('SELECT 2')),
47        );
48        self::assertNotSame(
49            $identity->compute($site, TextPattern::fromText('SELECT 1')),
50            $identity->compute(new CallSite('b.php', 3, 'f', 'pdo.query'), TextPattern::fromText('SELECT 1')),
51        );
52    }
53
54    public function testComputeSeparatesStatementsItCannotOtherwiseTellApart(): void
55    {
56        $identity = new EntryIdentity();
57        $site = new CallSite('a.php', 3, 'f', 'unreached');
58
59        self::assertNotSame(
60            $identity->compute($site, TextPattern::fromText('SELECT 1')),
61            $identity->compute($site, TextPattern::fromText('SELECT 1'), 1),
62        );
63    }
64
65    public function testComputeIsAsLongAsItSaysItIs(): void
66    {
67        $computed = (new EntryIdentity())->compute(
68            new CallSite('a.php', 3, 'f', 'pdo.query'),
69            TextPattern::fromText('SELECT 1'),
70        );
71        self::assertSame(EntryIdentity::LENGTH, strlen($computed));
72    }
73}
74