packages/sql-faker/tests/Unit/Sqlite/Generation/Value/LiteralGeneratorTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\Sqlite\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\Sqlite\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
84
85
86
87
88
89
90
91 public function testDecimalStringFormat(): void
92 {
93 $faker = Factory::create();
94 $faker->seed(42);
95 $rsg = new LiteralGenerator($faker);
96 $results = array_map(static fn (): string => $rsg->decimalString(), range(1, 50));
97 self::assertCount(50, array_filter($results, static fn (string $s): bool => preg_match('/^\d+\.\d{2,}$/', $s) === 1));
98 }
99
100
101
102
103
104
105
106 public function testRawIdentifierDefaultMatchesExplicit(): void
107 {
108 $faker = Factory::create();
109 $rsg = new LiteralGenerator($faker);
110
111 $faker->seed(42);
112 $a = $rsg->rawIdentifier();
113 $faker->seed(42);
114 $b = $rsg->rawIdentifier(1, 12);
115 self::assertSame($a, $b);
116 }
117
118 public function testMixedAlnumStringDefaultMatchesExplicit(): void
119 {
120 $faker = Factory::create();
121 $rsg = new LiteralGenerator($faker);
122
123 $faker->seed(42);
124 $a = $rsg->mixedAlnumString();
125 $faker->seed(42);
126 $b = $rsg->mixedAlnumString(1, 24);
127 self::assertSame($a, $b);
128 }
129
130 public function testIntegerStringDefaultMatchesExplicit(): void
131 {
132 $faker = Factory::create();
133 $rsg = new LiteralGenerator($faker);
134
135 $faker->seed(42);
136 $a = $rsg->integerString();
137 $faker->seed(42);
138 $b = $rsg->integerString(1, 9999999999);
139 self::assertSame($a, $b);
140 }
141
142
143
144
145
146 public function testDecimalStringDefaultMatchesExplicit(): void
147 {
148 $faker = Factory::create();
149 $rsg = new LiteralGenerator($faker);
150
151 $faker->seed(42);
152 $a = $rsg->decimalString();
153 $faker->seed(42);
154 $b = $rsg->decimalString(13, 6);
155 self::assertSame($a, $b);
156 }
157
158
159
160
161
162
163
164
165
166
167
168 public function testRawIdentifierCustomLength(): void
169 {
170 $faker = Factory::create();
171 $faker->seed(42);
172 $rsg = new LiteralGenerator($faker);
173 $lengths = array_map(static fn (): int => strlen($rsg->rawIdentifier(3, 5)), range(1, 100));
174 self::assertGreaterThanOrEqual(3, min($lengths));
175 self::assertLessThanOrEqual(5, max($lengths));
176 }
177
178 public function testMixedAlnumStringCustomLength(): void
179 {
180 $faker = Factory::create();
181 $faker->seed(42);
182 $rsg = new LiteralGenerator($faker);
183 $lengths = array_map(static fn (): int => strlen($rsg->mixedAlnumString(10, 15)), range(1, 100));
184 self::assertGreaterThanOrEqual(10, min($lengths));
185 self::assertLessThanOrEqual(15, max($lengths));
186 }
187
188 public function testIntegerStringCustomRange(): void
189 {
190 $faker = Factory::create();
191 $faker->seed(42);
192 $rsg = new LiteralGenerator($faker);
193 $values = array_map(static fn (): int => (int) $rsg->integerString(100, 200), range(1, 100));
194 self::assertGreaterThanOrEqual(100, min($values));
195 self::assertLessThanOrEqual(200, max($values));
196 }
197
198
199
200
201
202 public function testDecimalStringCustomPrecision(): void
203 {
204 $faker = Factory::create();
205 $faker->seed(42);
206 $rsg = new LiteralGenerator($faker);
207 $result = $rsg->decimalString(5, 2);
208 self::assertMatchesRegularExpression('/^\d+\.\d{2,}$/', $result);
209 }
210
211 public function testDecimalStringWithEqualPrecisionAndScale(): void
212 {
213 $faker = Factory::create();
214 $faker->seed(42);
215 $rsg = new LiteralGenerator($faker);
216 $result = $rsg->decimalString(2, 2);
217 self::assertMatchesRegularExpression('/^\d\.\d{2}$/', $result);
218 }
219
220 public function testDecimalStringFracDigitsMatchScale(): void
221 {
222 $faker = Factory::create();
223 $faker->seed(42);
224 $rsg = new LiteralGenerator($faker);
225 $result = $rsg->decimalString(8, 4);
226 $parts = explode('.', $result);
227 self::assertSame(4, strlen($parts[1]));
228 }
229
230 public function testDecimalStringWithScaleOneHasTwoFracDigits(): void
231 {
232 $faker = Factory::create();
233 $faker->seed(42);
234 $rsg = new LiteralGenerator($faker);
235 $result = $rsg->decimalString(5, 1);
236 $parts = explode('.', $result);
237 self::assertSame(2, strlen($parts[1]));
238 }
239
240 public function testDecimalStringPrecisionLimitsIntPartDigits(): void
241 {
242 $faker = Factory::create();
243 $faker->seed(42);
244 $rsg = new LiteralGenerator($faker);
245 $result = $rsg->decimalString(4, 2);
246 $parts = explode('.', $result);
247 $trimmed = ltrim($parts[0], '0');
248 $intPartDigits = $trimmed === '' ? 1 : strlen($trimmed);
249 self::assertLessThanOrEqual(2, $intPartDigits);
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272}
273