1
0
mirror of https://github.com/danog/postgres.git synced 2024-12-11 08:59:42 +01:00
postgres/test/ArrayParserTest.php

231 lines
6.1 KiB
PHP
Raw Normal View History

2017-12-18 07:11:48 +01:00
<?php
namespace Amp\Postgres\Test;
use Amp\Postgres\Internal\ArrayParser;
2019-09-27 05:41:47 +02:00
use Amp\Postgres\ParseException;
use PHPUnit\Framework\TestCase;
2017-12-18 07:11:48 +01:00
2018-07-01 19:33:12 +02:00
class ArrayParserTest extends TestCase
{
2017-12-18 07:11:48 +01:00
/** @var \Amp\Postgres\Internal\ArrayParser */
private $parser;
2020-02-06 23:34:49 +01:00
public function setUp(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$this->parser = new ArrayParser;
}
2020-02-06 23:34:49 +01:00
public function testSingleDimensionalArray(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ["one", "two", "three"];
$string = '{' . \implode(',', $array) . '}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testMultiDimensionalArray(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ["one", "two", ["three", "four"], "five"];
$string = '{one, two, {three, four}, five}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testQuotedStrings(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ["one", "two", ["three", "four"], "five"];
$string = '{"one", "two", {"three", "four"}, "five"}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testAlternateDelimiter(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ["1,2,3", "3,4,5"];
$string = '{1,2,3;3,4,5}';
$this->assertSame($array, $this->parser->parse($string, null, ';'));
}
2020-02-06 23:34:49 +01:00
public function testEscapedQuoteDelimiter(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ['va"lue1', 'value"2'];
$string = '{"va\\"lue1", "value\\"2"}';
$this->assertSame($array, $this->parser->parse($string, null, ','));
}
2020-02-06 23:34:49 +01:00
public function testNullValue(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ["one", null, "three"];
$string = '{one, NULL, three}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testQuotedNullValue(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = ["one", "NULL", "three"];
$string = '{one, "NULL", three}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testCast(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = [1, 2, 3];
$string = '{' . \implode(',', $array) . '}';
$cast = function (string $value): int {
return (int) $value;
};
$this->assertSame($array, $this->parser->parse($string, $cast));
}
2020-02-06 23:34:49 +01:00
public function testCastWithNull(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = [1, 2, null, 3];
$string = '{1,2,NULL,3}';
$cast = function (string $value): int {
return (int) $value;
};
$this->assertSame($array, $this->parser->parse($string, $cast));
}
2020-02-06 23:34:49 +01:00
public function testCastWithMultidimensionalArray(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = [1, 2, [3, 4], [5], 6, 7, [[8, 9], 10]];
$string = '{1,2,{3,4},{5},6,7,{{8,9},10}}';
$cast = function (string $value): int {
return (int) $value;
};
$this->assertSame($array, $this->parser->parse($string, $cast));
}
2020-02-06 23:34:49 +01:00
public function testRandomWhitespace(): void
2018-07-01 19:33:12 +02:00
{
2017-12-18 07:11:48 +01:00
$array = [1, 2, [3, 4], [5], 6, 7, [[8, 9], 10]];
$string = " {1, 2, { 3 ,\r 4 },{ 5} \n\t ,6 , 7, { {8,\t 9}, 10} } \n";
$cast = function (string $value): int {
return (int) $value;
};
$this->assertSame($array, $this->parser->parse($string, $cast));
}
2020-02-06 23:34:49 +01:00
public function testEscapedBackslashesInQuotedValue(): void
2018-07-01 19:33:12 +02:00
{
$array = ["test\\ing", "esca\\ped\\"];
$string = '{"test\\\\ing", "esca\\\\ped\\\\"}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testEmptyArray(): void
2020-01-16 21:12:26 +01:00
{
$array = [];
$string = '{}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testArrayContainingEmptyArray(): void
2020-01-16 21:12:26 +01:00
{
$array = [[], [1], []];
$string = '{{},{1},{}}';
$cast = function (string $value): int {
return (int) $value;
};
$this->assertSame($array, $this->parser->parse($string, $cast));
}
2020-02-06 23:34:49 +01:00
public function testArrayWithEmptyString(): void
2020-01-16 21:12:26 +01:00
{
$array = [''];
$string = '{""}';
$this->assertSame($array, $this->parser->parse($string));
}
2020-02-06 23:34:49 +01:00
public function testMalformedNestedArray(): void
2020-01-16 21:12:26 +01:00
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unexpected end of data');
$string = '{{}';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testEmptyString(): void
2020-01-16 21:12:26 +01:00
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Unexpected end of data');
$string = ' ';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testNoOpeningBracket(): void
2020-01-16 21:12:26 +01:00
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Missing opening bracket');
$string = '"one", "two"}';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testNoClosingBracket(): void
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(ParseException::class);
2020-01-16 21:12:26 +01:00
$this->expectExceptionMessage('Unexpected end of data');
2019-09-27 05:41:47 +02:00
2017-12-18 07:11:48 +01:00
$string = '{"one", "two"';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testExtraClosingBracket(): void
2020-01-16 21:12:26 +01:00
{
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Data left in buffer after parsing');
$string = '{"one", "two"}}';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testTrailingData(): void
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Data left in buffer after parsing');
2017-12-18 07:11:48 +01:00
$string = '{"one", "two"} data}';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testMissingQuote(): void
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Could not find matching quote in quoted value');
2017-12-18 07:11:48 +01:00
$string = '{"one", "two}';
$this->parser->parse($string);
}
2020-02-06 23:34:49 +01:00
public function testInvalidDelimiter(): void
2018-07-01 19:33:12 +02:00
{
2019-09-27 05:41:47 +02:00
$this->expectException(ParseException::class);
$this->expectExceptionMessage('Invalid delimiter');
2017-12-18 07:11:48 +01:00
$string = '{"one"; "two"}';
$this->parser->parse($string);
}
}