packages/ztd-query-core/tests/Unit/Sql/Profile/SqlParameterProfileTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Profile;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use Tests\Fake\FakeSqlLexerProfiles;
11use ZtdQuery\Sql\LexicalDelimiters;
12use ZtdQuery\Sql\LexicalPattern;
13use ZtdQuery\Sql\Profile\SqlParameterProfile;
14
15#[CoversClass(SqlParameterProfile::class)]
16#[UsesClass(LexicalDelimiters::class)]
17#[UsesClass(LexicalPattern::class)]
18final class SqlParameterProfileTest extends TestCase
19{
20    public function testPositionalParameterLengthAtMeasuresTheFirstPatternThatMatches(): void
21    {
22        $profile = FakeSqlLexerProfiles::parameters(positionalParameterPatterns: ['/^\$[0-9]+/', '/^\?/']);
23
24        self::assertSame([3, 1, 0], [
25            $profile->positionalParameterLengthAt('$12,', 0),
26            $profile->positionalParameterLengthAt('?,', 0),
27            $profile->positionalParameterLengthAt('a', 0),
28        ]);
29    }
30
31    public function testNamedParameterPrefixAtAnswersThePrefixAPlaceholderIsWrittenWith(): void
32    {
33        $profile = FakeSqlLexerProfiles::parameters(namedParameterSeparators: [':' => []]);
34
35        self::assertSame([':', null], [$profile->namedParameterPrefixAt(':a', 0), $profile->namedParameterPrefixAt('a', 0)]);
36    }
37
38    public function testNamedParameterPrefixAtAnswersNothingAfterWhatTheDialectForbids(): void
39    {
40        $profile = FakeSqlLexerProfiles::parameters(
41            namedParameterSeparators: [':' => []],
42            namedParameterForbiddenPredecessors: [':' => [':']],
43        );
44
45        self::assertNull($profile->namedParameterPrefixAt('a::b', 2));
46    }
47
48    public function testParameterNameSeparatorAtAnswersWhatJoinsAPrefixToItsName(): void
49    {
50        $profile = FakeSqlLexerProfiles::parameters(namedParameterSeparators: [':' => ['::']]);
51
52        self::assertSame(['::', null], [
53            $profile->parameterNameSeparatorAt(':', ':a::b', 2),
54            $profile->parameterNameSeparatorAt(':', ':ab', 2),
55        ]);
56    }
57
58    public function testParameterSuffixLengthMeasuresWhatAPlaceholderCarriesAfterItsName(): void
59    {
60        $profile = FakeSqlLexerProfiles::parameters(
61            namedParameterSeparators: [':' => []],
62            namedParameterSuffixPatterns: [':' => '/^\([^)]*\)/'],
63        );
64
65        self::assertSame([3, 0], [
66            $profile->parameterSuffixLength(':', ':a(1)', 2),
67            $profile->parameterSuffixLength(':', ':a', 2),
68        ]);
69    }
70}
71