packages/sql-faker/tests/Unit/MySql/Generation/Value/LiteralGeneratorTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\MySql\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\MySql\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 public function testHostnameStringFormat(): void
131 {
132 $faker = Factory::create();
133 $faker->seed(42);
134 $rsg = new LiteralGenerator($faker);
135 $results = array_map(static fn (): string => $rsg->hostnameString(), range(1, 50));
136 self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^[a-z0-9]+(\.[a-z0-9]+)*$/', $s) === 1));
137 }
138
139 public function testUnsignedBigIntStringFormat(): void
140 {
141 $faker = Factory::create();
142 $faker->seed(42);
143 $rsg = new LiteralGenerator($faker);
144 $results = array_map(static fn (): string => $rsg->unsignedBigIntString(), range(1, 50));
145 self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^(0|[1-9][0-9]*)$/', $s) === 1));
146 }
147
148 public function testUnsignedBigIntStringLengthRange(): void
149 {
150 $faker = Factory::create();
151 $faker->seed(42);
152 $rsg = new LiteralGenerator($faker);
153 $lengths = array_map(static fn (): int => strlen($rsg->unsignedBigIntString()), range(1, 200));
154 self::assertGreaterThanOrEqual(1, min($lengths));
155 self::assertLessThanOrEqual(20, max($lengths));
156 }
157
158 public function testRawIdentifierDefaultMatchesExplicit(): void
159 {
160 $faker = Factory::create();
161 $rsg = new LiteralGenerator($faker);
162
163 $faker->seed(42);
164 $a = $rsg->rawIdentifier();
165 $faker->seed(42);
166 $b = $rsg->rawIdentifier(1, 12);
167 self::assertSame($a, $b);
168 }
169
170 public function testMixedAlnumStringDefaultMatchesExplicit(): void
171 {
172 $faker = Factory::create();
173 $rsg = new LiteralGenerator($faker);
174
175 $faker->seed(42);
176 $a = $rsg->mixedAlnumString();
177 $faker->seed(42);
178 $b = $rsg->mixedAlnumString(1, 24);
179 self::assertSame($a, $b);
180 }
181
182 public function testIntegerStringDefaultMatchesExplicit(): void
183 {
184 $faker = Factory::create();
185 $rsg = new LiteralGenerator($faker);
186
187 $faker->seed(42);
188 $a = $rsg->integerString();
189 $faker->seed(42);
190 $b = $rsg->integerString(1, 9999999999);
191 self::assertSame($a, $b);
192 }
193
194 public function testHexStringDefaultMatchesExplicit(): void
195 {
196 $faker = Factory::create();
197 $rsg = new LiteralGenerator($faker);
198
199 $faker->seed(42);
200 $a = $rsg->hexString();
201 $faker->seed(42);
202 $b = $rsg->hexString(1, 16);
203 self::assertSame($a, $b);
204 }
205
206 public function testBinaryStringDefaultMatchesExplicit(): void
207 {
208 $faker = Factory::create();
209 $rsg = new LiteralGenerator($faker);
210
211 $faker->seed(42);
212 $a = $rsg->binaryString();
213 $faker->seed(42);
214 $b = $rsg->binaryString(1, 32);
215 self::assertSame($a, $b);
216 }
217
218 public function testDecimalStringDefaultMatchesExplicit(): void
219 {
220 $faker = Factory::create();
221 $rsg = new LiteralGenerator($faker);
222
223 $faker->seed(42);
224 $a = $rsg->decimalString();
225 $faker->seed(42);
226 $b = $rsg->decimalString(13, 6);
227 self::assertSame($a, $b);
228 }
229
230 public function testHostnameStringDefaultMatchesExplicit(): void
231 {
232 $faker = Factory::create();
233 $rsg = new LiteralGenerator($faker);
234
235 $faker->seed(42);
236 $a = $rsg->hostnameString();
237 $faker->seed(42);
238 $b = $rsg->hostnameString(1, 3, 1, 12);
239 self::assertSame($a, $b);
240 }
241
242 public function testUnsignedBigIntStringDefaultMatchesExplicit(): void
243 {
244 $faker = Factory::create();
245 $rsg = new LiteralGenerator($faker);
246
247 $faker->seed(42);
248 $a = $rsg->unsignedBigIntString();
249 $faker->seed(42);
250 $b = $rsg->unsignedBigIntString(1, 20);
251 self::assertSame($a, $b);
252 }
253
254 public function testLongIntStringDefaultMatchesExplicit(): void
255 {
256 $faker = Factory::create();
257 $rsg = new LiteralGenerator($faker);
258
259 $faker->seed(42);
260 $a = $rsg->longIntString();
261 $faker->seed(42);
262 $b = $rsg->longIntString(0, 2147483647);
263 self::assertSame($a, $b);
264 }
265
266 public function testFloatStringDefaultMatchesExplicit(): void
267 {
268 $faker = Factory::create();
269 $rsg = new LiteralGenerator($faker);
270
271 $faker->seed(42);
272 $a = $rsg->floatString('1.0');
273 $faker->seed(42);
274 $b = $rsg->floatString('1.0', -20, 20);
275 self::assertSame($a, $b);
276 }
277
278
279
280 public function testRawIdentifierCustomLength(): void
281 {
282 $faker = Factory::create();
283 $faker->seed(42);
284 $rsg = new LiteralGenerator($faker);
285 $lengths = array_map(static fn (): int => strlen($rsg->rawIdentifier(3, 5)), range(1, 100));
286 self::assertGreaterThanOrEqual(3, min($lengths));
287 self::assertLessThanOrEqual(5, max($lengths));
288 }
289
290 public function testMixedAlnumStringCustomLength(): void
291 {
292 $faker = Factory::create();
293 $faker->seed(42);
294 $rsg = new LiteralGenerator($faker);
295 $lengths = array_map(static fn (): int => strlen($rsg->mixedAlnumString(10, 15)), range(1, 100));
296 self::assertGreaterThanOrEqual(10, min($lengths));
297 self::assertLessThanOrEqual(15, max($lengths));
298 }
299
300 public function testIntegerStringCustomRange(): void
301 {
302 $faker = Factory::create();
303 $faker->seed(42);
304 $rsg = new LiteralGenerator($faker);
305 $values = array_map(static fn (): int => (int) $rsg->integerString(100, 200), range(1, 100));
306 self::assertGreaterThanOrEqual(100, min($values));
307 self::assertLessThanOrEqual(200, max($values));
308 }
309
310 public function testHexStringCustomLength(): void
311 {
312 $faker = Factory::create();
313 $faker->seed(42);
314 $rsg = new LiteralGenerator($faker);
315 $lengths = array_map(static fn (): int => strlen($rsg->hexString(4, 8)), range(1, 100));
316 self::assertGreaterThanOrEqual(4, min($lengths));
317 self::assertLessThanOrEqual(8, max($lengths));
318 }
319
320 public function testBinaryStringCustomLength(): void
321 {
322 $faker = Factory::create();
323 $faker->seed(42);
324 $rsg = new LiteralGenerator($faker);
325 $lengths = array_map(static fn (): int => strlen($rsg->binaryString(8, 16)), range(1, 100));
326 self::assertGreaterThanOrEqual(8, min($lengths));
327 self::assertLessThanOrEqual(16, max($lengths));
328 }
329
330 public function testDecimalStringCustomPrecision(): void
331 {
332 $faker = Factory::create();
333 $faker->seed(42);
334 $rsg = new LiteralGenerator($faker);
335 $result = $rsg->decimalString(5, 2);
336 self::assertMatchesRegularExpression('/^\d+\.\d{2,}$/', $result);
337 }
338
339 public function testDecimalStringWithEqualPrecisionAndScale(): void
340 {
341 $faker = Factory::create();
342 $faker->seed(42);
343 $rsg = new LiteralGenerator($faker);
344 $result = $rsg->decimalString(2, 2);
345 self::assertMatchesRegularExpression('/^\d\.\d{2}$/', $result);
346 }
347
348 public function testDecimalStringFracDigitsMatchScale(): void
349 {
350 $faker = Factory::create();
351 $faker->seed(42);
352 $rsg = new LiteralGenerator($faker);
353 $result = $rsg->decimalString(8, 4);
354 $parts = explode('.', $result);
355 self::assertSame(4, strlen($parts[1]));
356 }
357
358 public function testDecimalStringWithScaleOneHasTwoFracDigits(): void
359 {
360 $faker = Factory::create();
361 $faker->seed(42);
362 $rsg = new LiteralGenerator($faker);
363 $result = $rsg->decimalString(5, 1);
364 $parts = explode('.', $result);
365 self::assertSame(2, strlen($parts[1]));
366 }
367
368 public function testDecimalStringPrecisionLimitsIntPartDigits(): void
369 {
370 $faker = Factory::create();
371 $faker->seed(42);
372 $rsg = new LiteralGenerator($faker);
373 $result = $rsg->decimalString(4, 2);
374 $parts = explode('.', $result);
375 $trimmed = ltrim($parts[0], '0');
376 $intPartDigits = $trimmed === '' ? 1 : strlen($trimmed);
377 self::assertLessThanOrEqual(2, $intPartDigits);
378 }
379
380 public function testUnsignedBigIntStringCustomLength(): void
381 {
382 $faker = Factory::create();
383 $faker->seed(42);
384 $rsg = new LiteralGenerator($faker);
385 $lengths = array_map(static fn (): int => strlen($rsg->unsignedBigIntString(5, 10)), range(1, 100));
386 self::assertGreaterThanOrEqual(1, min($lengths));
387 self::assertLessThanOrEqual(10, max($lengths));
388 }
389
390 public function testLongIntStringCustomRange(): void
391 {
392 $faker = Factory::create();
393 $faker->seed(42);
394 $rsg = new LiteralGenerator($faker);
395 $values = array_map(static fn (): int => (int) $rsg->longIntString(50, 100), range(1, 100));
396 self::assertGreaterThanOrEqual(50, min($values));
397 self::assertLessThanOrEqual(100, max($values));
398 }
399
400 public function testFloatStringCustomExponent(): void
401 {
402 $faker = Factory::create();
403 $faker->seed(42);
404 $rsg = new LiteralGenerator($faker);
405 $result = $rsg->floatString('1.5', -5, 5);
406 self::assertMatchesRegularExpression('/^1\.5e-?\d+$/', $result);
407 }
408
409
410
411 public function testHostnameStringCustomParts(): void
412 {
413 $faker = Factory::create();
414 $faker->seed(42);
415 $rsg = new LiteralGenerator($faker);
416 $result = $rsg->hostnameString(2, 2, 3, 3);
417 self::assertMatchesRegularExpression('/^[a-z0-9]{3}\.[a-z0-9]{3}$/', $result);
418 }
419
420
421
422
423
424
425
426
427
428
429
430}
431