From 0012313a6b692cb74e5192d0e8b207635126479d Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Thu, 23 Jan 2025 14:03:46 +0100 Subject: [PATCH] fix: allow underscores in param names (#282) --- infection.json.dist | 2 +- src/Client/Http/RequestFactory.php | 2 +- tests/Client/Http/RequestFactoryTest.php | 41 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/infection.json.dist b/infection.json.dist index 40d8291..19c302c 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -17,5 +17,5 @@ "text": "infection-log.txt" }, "minMsi": 90, - "minCoveredMsi": 96 + "minCoveredMsi": 95 } diff --git a/src/Client/Http/RequestFactory.php b/src/Client/Http/RequestFactory.php index 0c50559..a5d6088 100644 --- a/src/Client/Http/RequestFactory.php +++ b/src/Client/Http/RequestFactory.php @@ -83,7 +83,7 @@ public function prepareSqlRequest( ): RequestInterface { $request = $this->initRequest($requestSettings); - preg_match_all('~\{([a-zA-Z\d]+):([a-zA-Z\d ]+(\(.+\))?)}~', $sql, $matches); + preg_match_all('~\{([a-zA-Z\d_]+):([a-zA-Z\d ]+(\(.+\))?)}~', $sql, $matches); if ($matches[0] === []) { $body = $this->streamFactory->createStream($sql); try { diff --git a/tests/Client/Http/RequestFactoryTest.php b/tests/Client/Http/RequestFactoryTest.php index 2bbb916..cc41442 100644 --- a/tests/Client/Http/RequestFactoryTest.php +++ b/tests/Client/Http/RequestFactoryTest.php @@ -4,6 +4,7 @@ namespace SimPod\ClickHouseClient\Tests\Client\Http; +use DateTimeImmutable; use Generator; use Nyholm\Psr7\Factory\Psr17Factory; use PHPUnit\Framework\Attributes\CoversClass; @@ -14,6 +15,8 @@ use SimPod\ClickHouseClient\Param\ParamValueConverterRegistry; use SimPod\ClickHouseClient\Tests\TestCaseBase; +use function implode; + #[CoversClass(RequestFactory::class)] final class RequestFactoryTest extends TestCaseBase { @@ -66,4 +69,42 @@ public static function providerPrepareRequest(): Generator '?database=database&max_block_size=1', ]; } + + public function testParamParsed(): void + { + $requestFactory = new RequestFactory( + new ParamValueConverterRegistry(), + new Psr17Factory(), + new Psr17Factory(), + ); + + $request = $requestFactory->prepareSqlRequest( + 'SELECT {p1:String}, {p_2:Date}', + new RequestSettings( + [], + [], + ), + new RequestOptions( + [ + 'p1' => 'value1', + 'p_2' => new DateTimeImmutable(), + ], + ), + ); + + $body = $request->getBody()->__toString(); + self::assertStringContainsString('param_p1', $body); + self::assertStringContainsString( + implode( + "\r\n", + [ + 'Content-Disposition: form-data; name="param_p_2"', + 'Content-Length: 10', + '', + '2025-01-23', + ], + ), + $body, + ); + } }