packages/ztd-query-mysql/tests/Unit/Rewrite/LoadData/InputFileTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rewrite\LoadData;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use ZtdQuery\Platform\MySql\Rewrite\LoadData\InputFile;
10
11#[CoversClass(InputFile::class)]
12final class InputFileTest extends TestCase
13{
14    public function testRead(): void
15    {
16        $path = tempnam(sys_get_temp_dir(), 'mysql-load-');
17        self::assertNotFalse($path);
18        try {
19            file_put_contents($path, "1,hello\n2,world\n");
20            $sql = "LOAD DATA LOCAL INFILE '" . $path . "' INTO TABLE t";
21            $statement = (new \PhpMyAdmin\SqlParser\Parser($sql))->statements[0];
22            self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\LoadStatement::class, $statement);
23            self::assertSame("1,hello\n2,world\n", (new InputFile())->read($statement, $sql));
24        } finally {
25            unlink($path);
26        }
27    }
28
29    public function testReadRejectsMissingFile(): void
30    {
31        $sql = "LOAD DATA INFILE 'input.csv' INTO TABLE t";
32        $statement = (new \PhpMyAdmin\SqlParser\Parser($sql))->statements[0];
33        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\LoadStatement::class, $statement);
34        $statement->file_name = null;
35        $this->expectException(\ZtdQuery\Exception\UnsupportedSqlException::class);
36        (new InputFile())->read($statement, $sql);
37    }
38
39}
40