packages/sql-fixture/tests/Unit/Platform/PostgreSql/Value/StructuredGeneratorTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Platform\PostgreSql\Value;
6
7use Faker\Factory;
8use PHPUnit\Framework\Attributes\CoversClass;
9use PHPUnit\Framework\TestCase;
10use SqlFixture\Platform\PostgreSql\Value\StructuredGenerator as Subject;
11
12#[CoversClass(Subject::class)]
13final class StructuredGeneratorTest extends TestCase
14{
15    public function testGenerateByteaReturnsHexBytea(): void
16    {
17        self::assertMatchesRegularExpression('/^\\\\x[0-9a-f]+$/', (new Subject())->generateBytea(Factory::create()));
18    }
19
20    public function testGenerateIntervalIncludesCalendarAndClockUnits(): void
21    {
22        self::assertMatchesRegularExpression('/^\d+ (years?|months?|days?|hours?|minutes?|seconds?)$/', (new Subject())->generateInterval(Factory::create()));
23    }
24
25    public function testGenerateJsonReturnsAJsonObject(): void
26    {
27        $json = (new Subject())->generateJson(Factory::create());
28        self::assertIsArray(json_decode($json, true));
29        self::assertSame(JSON_ERROR_NONE, json_last_error());
30    }
31
32    public function testGenerateIntArrayUsesPostgresArraySyntax(): void
33    {
34        self::assertMatchesRegularExpression('/^\{(?:-?\d+(?:,-?\d+)*)?\}$/', (new Subject())->generateIntArray(Factory::create()));
35    }
36
37    public function testGenerateTextArrayUsesPostgresArraySyntax(): void
38    {
39        $value = (new Subject())->generateTextArray(Factory::create());
40        self::assertStringStartsWith('{', $value);
41        self::assertStringEndsWith('}', $value);
42        self::assertStringContainsString('"', $value);
43    }
44}
45