packages/sql-faker/tests/Unit/PostgreSql/Generation/Value/LiteralGeneratorTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\PostgreSql\Generation\Value;
6
7use Faker\Factory;
8use Override;
9use PHPUnit\Framework\Attributes\CoversClass;
10use PHPUnit\Framework\Attributes\UsesClass;
11use PHPUnit\Framework\TestCase;
12use SqlFaker\Generation\Value\RandomCharacters;
13use SqlFaker\PostgreSql\Generation\Value\LiteralGenerator;
14
15#[CoversClass(LiteralGenerator::class)]
16#[UsesClass(RandomCharacters::class)]
17final class LiteralGeneratorTest extends TestCase
18{
19    #[Override]
20    protected function setUp(): void
21    {
22        parent::setUp();
23        gc_collect_cycles();
24    }
25
26    public function testRawIdentifierStartsWithLetterOrUnderscore(): void
27    {
28        $faker = Factory::create();
29        $faker->seed(42);
30        $rsg = new LiteralGenerator($faker);
31        $results = array_map(static fn (): string => $rsg->rawIdentifier(), range(1, 50));
32        self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^[a-z_][a-z0-9_]*$/', $s) === 1));
33    }
34
35    public function testRawIdentifierLengthRange(): void
36    {
37        $faker = Factory::create();
38        $faker->seed(42);
39        $rsg = new LiteralGenerator($faker);
40        $lengths = array_map(static fn (): int => strlen($rsg->rawIdentifier()), range(1, 200));
41        self::assertGreaterThanOrEqual(1, min($lengths));
42        self::assertLessThanOrEqual(12, max($lengths));
43    }
44
45    public function testMixedAlnumStringCharacterSet(): void
46    {
47        $faker = Factory::create();
48        $faker->seed(42);
49        $rsg = new LiteralGenerator($faker);
50        $results = array_map(static fn (): string => $rsg->mixedAlnumString(), range(1, 50));
51        self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^[a-zA-Z0-9_]+$/', $s) === 1));
52    }
53
54    public function testMixedAlnumStringLengthRange(): void
55    {
56        $faker = Factory::create();
57        $faker->seed(42);
58        $rsg = new LiteralGenerator($faker);
59        $lengths = array_map(static fn (): int => strlen($rsg->mixedAlnumString()), range(1, 200));
60        self::assertGreaterThanOrEqual(1, min($lengths));
61        self::assertLessThanOrEqual(24, max($lengths));
62    }
63
64    public function testIntegerStringFormat(): void
65    {
66        $faker = Factory::create();
67        $faker->seed(42);
68        $rsg = new LiteralGenerator($faker);
69        $results = array_map(static fn (): string => $rsg->integerString(), range(1, 50));
70        self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^[1-9][0-9]*$/', $s) === 1));
71    }
72
73    public function testIntegerStringDigitCountRange(): void
74    {
75        $faker = Factory::create();
76        $faker->seed(42);
77        $rsg = new LiteralGenerator($faker);
78        $lengths = array_map(static fn (): int => strlen($rsg->integerString()), range(1, 200));
79        self::assertGreaterThanOrEqual(1, min($lengths));
80        self::assertLessThanOrEqual(10, max($lengths));
81    }
82
83    public function testHexStringFormat(): void
84    {
85        $faker = Factory::create();
86        $faker->seed(42);
87        $rsg = new LiteralGenerator($faker);
88        $results = array_map(static fn (): string => $rsg->hexString(), range(1, 50));
89        self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^[0-9a-f]+$/', $s) === 1));
90    }
91
92    public function testHexStringLengthRange(): void
93    {
94        $faker = Factory::create();
95        $faker->seed(42);
96        $rsg = new LiteralGenerator($faker);
97        $lengths = array_map(static fn (): int => strlen($rsg->hexString()), range(1, 200));
98        self::assertGreaterThanOrEqual(1, min($lengths));
99        self::assertLessThanOrEqual(16, max($lengths));
100    }
101
102    public function testBinaryStringFormat(): void
103    {
104        $faker = Factory::create();
105        $faker->seed(42);
106        $rsg = new LiteralGenerator($faker);
107        $results = array_map(static fn (): string => $rsg->binaryString(), range(1, 50));
108        self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^[01]+$/', $s) === 1));
109    }
110
111    public function testBinaryStringLengthRange(): void
112    {
113        $faker = Factory::create();
114        $faker->seed(42);
115        $rsg = new LiteralGenerator($faker);
116        $lengths = array_map(static fn (): int => strlen($rsg->binaryString()), range(1, 200));
117        self::assertGreaterThanOrEqual(1, min($lengths));
118        self::assertLessThanOrEqual(32, max($lengths));
119    }
120
121    public function testDecimalStringFormat(): void
122    {
123        $faker = Factory::create();
124        $faker->seed(42);
125        $rsg = new LiteralGenerator($faker);
126        $results = array_map(static fn (): string => $rsg->decimalString(), range(1, 50));
127        self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^\d+\.\d{2,}$/', $s) === 1));
128    }
129
130
131
132
133
134
135
136    public function testRawIdentifierDefaultMatchesExplicit(): void
137    {
138        $faker = Factory::create();
139        $rsg = new LiteralGenerator($faker);
140
141        $faker->seed(42);
142        $a = $rsg->rawIdentifier();
143        $faker->seed(42);
144        $b = $rsg->rawIdentifier(1, 12);
145        self::assertSame($a, $b);
146    }
147
148    public function testMixedAlnumStringDefaultMatchesExplicit(): void
149    {
150        $faker = Factory::create();
151        $rsg = new LiteralGenerator($faker);
152
153        $faker->seed(42);
154        $a = $rsg->mixedAlnumString();
155        $faker->seed(42);
156        $b = $rsg->mixedAlnumString(1, 24);
157        self::assertSame($a, $b);
158    }
159
160    public function testIntegerStringDefaultMatchesExplicit(): void
161    {
162        $faker = Factory::create();
163        $rsg = new LiteralGenerator($faker);
164
165        $faker->seed(42);
166        $a = $rsg->integerString();
167        $faker->seed(42);
168        $b = $rsg->integerString(1, 9999999999);
169        self::assertSame($a, $b);
170    }
171
172    public function testHexStringDefaultMatchesExplicit(): void
173    {
174        $faker = Factory::create();
175        $rsg = new LiteralGenerator($faker);
176
177        $faker->seed(42);
178        $a = $rsg->hexString();
179        $faker->seed(42);
180        $b = $rsg->hexString(1, 16);
181        self::assertSame($a, $b);
182    }
183
184    public function testBinaryStringDefaultMatchesExplicit(): void
185    {
186        $faker = Factory::create();
187        $rsg = new LiteralGenerator($faker);
188
189        $faker->seed(42);
190        $a = $rsg->binaryString();
191        $faker->seed(42);
192        $b = $rsg->binaryString(1, 32);
193        self::assertSame($a, $b);
194    }
195
196    public function testDecimalStringDefaultMatchesExplicit(): void
197    {
198        $faker = Factory::create();
199        $rsg = new LiteralGenerator($faker);
200
201        $faker->seed(42);
202        $a = $rsg->decimalString();
203        $faker->seed(42);
204        $b = $rsg->decimalString(13, 6);
205        self::assertSame($a, $b);
206    }
207
208
209
210
211
212
213
214    public function testFloatStringDefaultMatchesExplicit(): void
215    {
216        $faker = Factory::create();
217        $rsg = new LiteralGenerator($faker);
218
219        $faker->seed(42);
220        $a = $rsg->floatString('1.0');
221        $faker->seed(42);
222        $b = $rsg->floatString('1.0', -20, 20);
223        self::assertSame($a, $b);
224    }
225
226    public function testParameterIndexDefaultMatchesExplicit(): void
227    {
228        $faker = Factory::create();
229        $rsg = new LiteralGenerator($faker);
230
231        $faker->seed(42);
232        $a = $rsg->parameterIndex();
233        $faker->seed(42);
234        $b = $rsg->parameterIndex(1, 10);
235        self::assertSame($a, $b);
236    }
237
238    public function testRawIdentifierCustomLength(): void
239    {
240        $faker = Factory::create();
241        $faker->seed(42);
242        $rsg = new LiteralGenerator($faker);
243        $lengths = array_map(static fn (): int => strlen($rsg->rawIdentifier(3, 5)), range(1, 100));
244        self::assertGreaterThanOrEqual(3, min($lengths));
245        self::assertLessThanOrEqual(5, max($lengths));
246    }
247
248    public function testMixedAlnumStringCustomLength(): void
249    {
250        $faker = Factory::create();
251        $faker->seed(42);
252        $rsg = new LiteralGenerator($faker);
253        $lengths = array_map(static fn (): int => strlen($rsg->mixedAlnumString(10, 15)), range(1, 100));
254        self::assertGreaterThanOrEqual(10, min($lengths));
255        self::assertLessThanOrEqual(15, max($lengths));
256    }
257
258    public function testIntegerStringCustomRange(): void
259    {
260        $faker = Factory::create();
261        $faker->seed(42);
262        $rsg = new LiteralGenerator($faker);
263        $values = array_map(static fn (): int => (int) $rsg->integerString(100, 200), range(1, 100));
264        self::assertGreaterThanOrEqual(100, min($values));
265        self::assertLessThanOrEqual(200, max($values));
266    }
267
268    public function testHexStringCustomLength(): void
269    {
270        $faker = Factory::create();
271        $faker->seed(42);
272        $rsg = new LiteralGenerator($faker);
273        $lengths = array_map(static fn (): int => strlen($rsg->hexString(4, 8)), range(1, 100));
274        self::assertGreaterThanOrEqual(4, min($lengths));
275        self::assertLessThanOrEqual(8, max($lengths));
276    }
277
278    public function testBinaryStringCustomLength(): void
279    {
280        $faker = Factory::create();
281        $faker->seed(42);
282        $rsg = new LiteralGenerator($faker);
283        $lengths = array_map(static fn (): int => strlen($rsg->binaryString(8, 16)), range(1, 100));
284        self::assertGreaterThanOrEqual(8, min($lengths));
285        self::assertLessThanOrEqual(16, max($lengths));
286    }
287
288    public function testDecimalStringCustomPrecision(): void
289    {
290        $faker = Factory::create();
291        $faker->seed(42);
292        $rsg = new LiteralGenerator($faker);
293        $result = $rsg->decimalString(5, 2);
294        self::assertMatchesRegularExpression('/^\d+\.\d{2,}$/', $result);
295    }
296
297    public function testDecimalStringWithEqualPrecisionAndScale(): void
298    {
299        $faker = Factory::create();
300        $faker->seed(42);
301        $rsg = new LiteralGenerator($faker);
302        $result = $rsg->decimalString(2, 2);
303        self::assertMatchesRegularExpression('/^\d\.\d{2}$/', $result);
304    }
305
306    public function testDecimalStringFracDigitsMatchScale(): void
307    {
308        $faker = Factory::create();
309        $faker->seed(42);
310        $rsg = new LiteralGenerator($faker);
311        $result = $rsg->decimalString(8, 4);
312        $parts = explode('.', $result);
313        self::assertSame(4, strlen($parts[1]));
314    }
315
316    public function testDecimalStringWithScaleOneHasTwoFracDigits(): void
317    {
318        $faker = Factory::create();
319        $faker->seed(42);
320        $rsg = new LiteralGenerator($faker);
321        $result = $rsg->decimalString(5, 1);
322        $parts = explode('.', $result);
323        self::assertSame(2, strlen($parts[1]));
324    }
325
326    public function testDecimalStringPrecisionLimitsIntPartDigits(): void
327    {
328        $faker = Factory::create();
329        $faker->seed(42);
330        $rsg = new LiteralGenerator($faker);
331        $result = $rsg->decimalString(4, 2);
332        $parts = explode('.', $result);
333        $trimmed = ltrim($parts[0], '0');
334        $intPartDigits = $trimmed === '' ? 1 : strlen($trimmed);
335        self::assertLessThanOrEqual(2, $intPartDigits);
336    }
337
338
339
340
341
342    public function testFloatStringCustomExponent(): void
343    {
344        $faker = Factory::create();
345        $faker->seed(42);
346        $rsg = new LiteralGenerator($faker);
347        $result = $rsg->floatString('1.5', -5, 5);
348        self::assertMatchesRegularExpression('/^1\.5e-?\d+$/', $result);
349    }
350
351    public function testParameterIndexCustomRange(): void
352    {
353        $faker = Factory::create();
354        $faker->seed(42);
355        $rsg = new LiteralGenerator($faker);
356        $values = array_map(static fn (): int => (int) $rsg->parameterIndex(1, 3), range(1, 100));
357        self::assertGreaterThanOrEqual(1, min($values));
358        self::assertLessThanOrEqual(3, max($values));
359    }
360
361
362
363
364
365
366
367
368
369
370
371
372
373}
374