2018-01-25 06:45:04 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Postgres\Test;
|
|
|
|
|
2019-09-27 05:41:47 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2018-01-25 06:45:04 +01:00
|
|
|
use function Amp\Postgres\encode;
|
|
|
|
|
2018-07-01 19:33:12 +02:00
|
|
|
class EncodeTest extends TestCase
|
|
|
|
{
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testSingleDimensionalStringArray(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = ["one", "two", "three"];
|
|
|
|
$string = '{"one","two","three"}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testMultiDimensionalStringArray(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = ["one", "two", ["three", "four"], "five"];
|
|
|
|
$string = '{"one","two",{"three","four"},"five"}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testQuotedStrings(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = ["one", "two", ["three", "four"], "five"];
|
|
|
|
$string = '{"one","two",{"three","four"},"five"}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testEscapedQuoteDelimiter(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = ['va"lue1', 'value"2'];
|
|
|
|
$string = '{"va\\"lue1","value\\"2"}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testNullValue(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = ["one", null, "three"];
|
|
|
|
$string = '{"one",NULL,"three"}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testSingleDimensionalIntegerArray(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = [1, 2, 3];
|
|
|
|
$string = '{' . \implode(',', $array) . '}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testIntegerArrayWithNull(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = [1, 2, null, 3];
|
|
|
|
$string = '{1,2,NULL,3}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testMultidimensionalIntegerArray(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = [1, 2, [3, 4], [5], 6, 7, [[8, 9], 10]];
|
|
|
|
$string = '{1,2,{3,4},{5},6,7,{{8,9},10}}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testEscapedBackslashesInQuotedValue(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-25 06:45:04 +01:00
|
|
|
$array = ["test\\ing", "esca\\ped\\"];
|
|
|
|
$string = '{"test\\\\ing","esca\\\\ped\\\\"}';
|
|
|
|
|
|
|
|
$this->assertSame($string, encode($array));
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function testObjectWithoutToStringMethod(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2019-09-27 05:41:47 +02:00
|
|
|
$this->expectException(\Error::class);
|
|
|
|
$this->expectExceptionMessage('Object without a __toString() method in array');
|
|
|
|
|
2018-01-25 06:45:04 +01:00
|
|
|
encode([new \stdClass]);
|
|
|
|
}
|
|
|
|
}
|