|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Amp\Postgres\Test; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use function Amp\Postgres\Internal\cast; |
| 7 | + |
| 8 | +enum IntegerEnum: int |
| 9 | +{ |
| 10 | + case One = 1; |
| 11 | + case Two = 2; |
| 12 | + case Three = 3; |
| 13 | +} |
| 14 | + |
| 15 | +enum StringEnum: string |
| 16 | +{ |
| 17 | + case One = 'one'; |
| 18 | + case Two = 'two'; |
| 19 | + case Three = 'three'; |
| 20 | +} |
| 21 | + |
| 22 | +enum UnitEnum |
| 23 | +{ |
| 24 | + case Case; |
| 25 | +} |
| 26 | + |
| 27 | +class CastTest extends TestCase |
| 28 | +{ |
| 29 | + public function testSingleDimensionalStringArray(): void |
| 30 | + { |
| 31 | + $array = ["one", "two", "three"]; |
| 32 | + $string = '{"one","two","three"}'; |
| 33 | + |
| 34 | + $this->assertSame($string, cast($array)); |
| 35 | + } |
| 36 | + |
| 37 | + public function testMultiDimensionalStringArray(): void |
| 38 | + { |
| 39 | + $array = ["one", "two", ["three", "four"], "five"]; |
| 40 | + $string = '{"one","two",{"three","four"},"five"}'; |
| 41 | + |
| 42 | + $this->assertSame($string, cast($array)); |
| 43 | + } |
| 44 | + |
| 45 | + public function testQuotedStrings(): void |
| 46 | + { |
| 47 | + $array = ["one", "two", ["three", "four"], "five"]; |
| 48 | + $string = '{"one","two",{"three","four"},"five"}'; |
| 49 | + |
| 50 | + $this->assertSame($string, cast($array)); |
| 51 | + } |
| 52 | + |
| 53 | + public function testEscapedQuoteDelimiter(): void |
| 54 | + { |
| 55 | + $array = ['va"lue1', 'value"2']; |
| 56 | + $string = '{"va\\"lue1","value\\"2"}'; |
| 57 | + |
| 58 | + $this->assertSame($string, cast($array)); |
| 59 | + } |
| 60 | + |
| 61 | + public function testNullValue(): void |
| 62 | + { |
| 63 | + $array = ["one", null, "three"]; |
| 64 | + $string = '{"one",NULL,"three"}'; |
| 65 | + |
| 66 | + $this->assertSame($string, cast($array)); |
| 67 | + } |
| 68 | + |
| 69 | + public function testSingleDimensionalIntegerArray(): void |
| 70 | + { |
| 71 | + $array = [1, 2, 3]; |
| 72 | + $string = '{' . \implode(',', $array) . '}'; |
| 73 | + |
| 74 | + $this->assertSame($string, cast($array)); |
| 75 | + } |
| 76 | + |
| 77 | + public function testIntegerArrayWithNull(): void |
| 78 | + { |
| 79 | + $array = [1, 2, null, 3]; |
| 80 | + $string = '{1,2,NULL,3}'; |
| 81 | + |
| 82 | + $this->assertSame($string, cast($array)); |
| 83 | + } |
| 84 | + |
| 85 | + public function testMultidimensionalIntegerArray(): void |
| 86 | + { |
| 87 | + $array = [1, 2, [3, 4], [5], 6, 7, [[8, 9], 10]]; |
| 88 | + $string = '{1,2,{3,4},{5},6,7,{{8,9},10}}'; |
| 89 | + |
| 90 | + $this->assertSame($string, cast($array)); |
| 91 | + } |
| 92 | + |
| 93 | + public function testEscapedBackslashesInQuotedValue(): void |
| 94 | + { |
| 95 | + $array = ["test\\ing", "esca\\ped\\"]; |
| 96 | + $string = '{"test\\\\ing","esca\\\\ped\\\\"}'; |
| 97 | + |
| 98 | + $this->assertSame($string, cast($array)); |
| 99 | + } |
| 100 | + |
| 101 | + public function testBackedEnum(): void |
| 102 | + { |
| 103 | + $this->assertSame(3, cast(IntegerEnum::Three)); |
| 104 | + $this->assertSame('three', cast(StringEnum::Three)); |
| 105 | + } |
| 106 | + |
| 107 | + public function testBackedEnumInArray(): void |
| 108 | + { |
| 109 | + $array = [ |
| 110 | + [IntegerEnum::One, IntegerEnum::Two, IntegerEnum::Three], |
| 111 | + [StringEnum::One, StringEnum::Two, StringEnum::Three], |
| 112 | + ]; |
| 113 | + $string = '{{1,2,3},{"one","two","three"}}'; |
| 114 | + |
| 115 | + $this->assertSame($string, cast($array)); |
| 116 | + } |
| 117 | + |
| 118 | + public function testUnitEnum(): void |
| 119 | + { |
| 120 | + $this->expectException(\ValueError::class); |
| 121 | + $this->expectExceptionMessage('An object in parameter values must be'); |
| 122 | + |
| 123 | + cast(UnitEnum::Case); |
| 124 | + } |
| 125 | + |
| 126 | + public function testUnitEnumInArray(): void |
| 127 | + { |
| 128 | + $this->expectException(\ValueError::class); |
| 129 | + $this->expectExceptionMessage('An object in parameter arrays must be'); |
| 130 | + |
| 131 | + cast([UnitEnum::Case]); |
| 132 | + } |
| 133 | + |
| 134 | + public function testObjectWithoutToStringMethod(): void |
| 135 | + { |
| 136 | + $this->expectException(\ValueError::class); |
| 137 | + $this->expectExceptionMessage('An object in parameter values must be'); |
| 138 | + |
| 139 | + cast(new \stdClass); |
| 140 | + } |
| 141 | + |
| 142 | + public function testObjectWithoutToStringMethodInArray(): void |
| 143 | + { |
| 144 | + $this->expectException(\ValueError::class); |
| 145 | + $this->expectExceptionMessage('An object in parameter arrays must be'); |
| 146 | + |
| 147 | + cast([new \stdClass]); |
| 148 | + } |
| 149 | +} |
0 commit comments