packages/ztd-query-mysql/tests/Unit/Sql/Value/StringCoercionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Value;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use RuntimeException;
10use SplFileInfo;
11use ZtdQuery\Platform\MySql\Sql\Value\StringCoercion;
12
13#[CoversClass(StringCoercion::class)]
14final class StringCoercionTest extends TestCase
15{
16    public function testStringValueAcceptsScalarsAndStringableObjects(): void
17    {
18        $coercion = new StringCoercion();
19        self::assertSame('12', $coercion->stringValue(12));
20        self::assertSame('1', $coercion->stringValue(true));
21        self::assertSame('', $coercion->stringValue(false));
22        self::assertSame('notes.sql', $coercion->stringValue(new SplFileInfo('notes.sql')));
23    }
24
25    public function testStringValueRejectsUnsupportedFixtureCells(): void
26    {
27        $this->expectException(RuntimeException::class);
28        $this->expectExceptionMessage('Unsupported value type for CTE shadowing.');
29        (new StringCoercion())->stringValue(['nested']);
30    }
31
32    public function testReadStreamPreservesTheOriginalPosition(): void
33    {
34        $stream = fopen('php://memory', 'w+');
35        self::assertIsResource($stream);
36        try {
37            fwrite($stream, "a\0bc");
38            fseek($stream, 2);
39            $coercion = new StringCoercion();
40            self::assertSame("a\0bc", $coercion->readStream($stream));
41            self::assertSame(2, ftell($stream));
42            self::assertSame("a\0bc", $coercion->stringValue($stream));
43            self::assertSame(2, ftell($stream));
44        } finally {
45            fclose($stream);
46        }
47    }
48
49}
50