|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Laudis Neo4j package. |
| 7 | + * |
| 8 | + * (c) Laudis technologies <http://laudis.tech> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Laudis\Neo4j\Tests\Unit; |
| 15 | + |
| 16 | +use Ds\Map; |
| 17 | +use Ds\Vector; |
| 18 | +use InvalidArgumentException; |
| 19 | +use Iterator; |
| 20 | +use Laudis\Neo4j\ParameterHelper; |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use stdClass; |
| 23 | + |
| 24 | +final class ParameterHelperTest extends TestCase |
| 25 | +{ |
| 26 | + /** @var iterable<iterable|scalar|null> */ |
| 27 | + private static iterable $invalidIterable; |
| 28 | + |
| 29 | + public static function setUpBeforeClass(): void |
| 30 | + { |
| 31 | + parent::setUpBeforeClass(); |
| 32 | + self::$invalidIterable = new class() implements Iterator { |
| 33 | + private bool $initial = true; |
| 34 | + |
| 35 | + public function current(): int |
| 36 | + { |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + public function next(): void |
| 41 | + { |
| 42 | + $this->initial = false; |
| 43 | + } |
| 44 | + |
| 45 | + public function key(): stdClass |
| 46 | + { |
| 47 | + return new stdClass(); |
| 48 | + } |
| 49 | + |
| 50 | + public function valid(): bool |
| 51 | + { |
| 52 | + return $this->initial; |
| 53 | + } |
| 54 | + |
| 55 | + public function rewind(): void |
| 56 | + { |
| 57 | + $this->initial = true; |
| 58 | + } |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + public function testAsList(): void |
| 63 | + { |
| 64 | + self::assertEquals([1, 2, 3], ParameterHelper::asList([2 => 1, 'a' => 2, 'd' => 3])->toArray()); |
| 65 | + } |
| 66 | + |
| 67 | + public function testAsMap(): void |
| 68 | + { |
| 69 | + self::assertEquals([2 => 1, 'a' => 2, 'd' => 3], ParameterHelper::asMap([2 => 1, 'a' => 2, 'd' => 3])->toArray()); |
| 70 | + } |
| 71 | + |
| 72 | + public function testFormatParameterString(): void |
| 73 | + { |
| 74 | + self::assertEquals(['a' => 'b', 'c' => 'd'], ParameterHelper::formatParameters([ |
| 75 | + 'a' => 'b', |
| 76 | + 'c' => 'd', |
| 77 | + ])->toArray()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testFormatParameterInteger(): void |
| 81 | + { |
| 82 | + self::assertEquals([2 => 'b', 3 => 'd'], ParameterHelper::formatParameters([ |
| 83 | + 2 => 'b', |
| 84 | + 3 => 'd', |
| 85 | + ])->toArray()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testFormatParameterVector(): void |
| 89 | + { |
| 90 | + self::assertEquals(['b', 'd'], ParameterHelper::formatParameters([ |
| 91 | + 'b', |
| 92 | + 'd', |
| 93 | + ])->toArray()); |
| 94 | + } |
| 95 | + |
| 96 | + public function testFormatParameterIterable(): void |
| 97 | + { |
| 98 | + self::assertEquals([[1, 2]], ParameterHelper::formatParameters([ |
| 99 | + [1, 2], |
| 100 | + ])->toArray()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testFormatParameterInvalidIterable(): void |
| 104 | + { |
| 105 | + $this->expectException(InvalidArgumentException::class); |
| 106 | + ParameterHelper::formatParameters(self::$invalidIterable); |
| 107 | + } |
| 108 | + |
| 109 | + public function testFormatParameterInvalidIterable2(): void |
| 110 | + { |
| 111 | + $this->expectException(InvalidArgumentException::class); |
| 112 | + ParameterHelper::formatParameters([ |
| 113 | + 'a' => [ |
| 114 | + self::$invalidIterable, |
| 115 | + ], |
| 116 | + ]); |
| 117 | + } |
| 118 | + |
| 119 | + public function testAsParmeterEmptyVector(): void |
| 120 | + { |
| 121 | + $result = ParameterHelper::asParameter(new Vector()); |
| 122 | + self::assertIsArray($result); |
| 123 | + self::assertCount(0, $result); |
| 124 | + } |
| 125 | + |
| 126 | + public function testAsParmeterEmptyMap(): void |
| 127 | + { |
| 128 | + $result = ParameterHelper::asParameter(new Map()); |
| 129 | + self::assertInstanceOf(stdClass::class, $result); |
| 130 | + } |
| 131 | + |
| 132 | + public function testAsParmeterEmptyArray(): void |
| 133 | + { |
| 134 | + $result = ParameterHelper::asParameter([]); |
| 135 | + self::assertInstanceOf(stdClass::class, $result); |
| 136 | + } |
| 137 | +} |
0 commit comments