|
| 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 ArrayIterator; |
| 17 | +use BadMethodCallException; |
| 18 | +use InvalidArgumentException; |
| 19 | +use function json_encode; |
| 20 | +use JsonException; |
| 21 | +use Laudis\Neo4j\Types\AbstractCypherContainer; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +final class CypherTypeTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @throws JsonException |
| 28 | + * |
| 29 | + * @psalm-suppress all |
| 30 | + */ |
| 31 | + public function testEmpty(): void |
| 32 | + { |
| 33 | + $empty = new class() extends AbstractCypherContainer { |
| 34 | + public function getIterator() |
| 35 | + { |
| 36 | + return new ArrayIterator([]); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + self::assertEquals('[]', json_encode($empty, JSON_THROW_ON_ERROR)); |
| 41 | + self::assertFalse(isset($empty[0])); |
| 42 | + self::assertNull($empty[0] ?? null); |
| 43 | + |
| 44 | + $caught = null; |
| 45 | + try { |
| 46 | + $empty[0] = 'abc'; |
| 47 | + } catch (BadMethodCallException $e) { |
| 48 | + $caught = true; |
| 49 | + } |
| 50 | + self::assertTrue($caught, 'Empty is writable'); |
| 51 | + |
| 52 | + $caught = null; |
| 53 | + try { |
| 54 | + unset($empty[0]); |
| 55 | + } catch (BadMethodCallException $e) { |
| 56 | + $caught = true; |
| 57 | + } |
| 58 | + self::assertTrue($caught, 'Empty is writable'); |
| 59 | + |
| 60 | + $caught = null; |
| 61 | + try { |
| 62 | + $empty[0]; |
| 63 | + } catch (InvalidArgumentException $e) { |
| 64 | + $caught = true; |
| 65 | + } |
| 66 | + self::assertTrue($caught, 'Empty has still valid access'); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @throws JsonException |
| 71 | + * |
| 72 | + * @psalm-suppress all |
| 73 | + */ |
| 74 | + public function testFilled(): void |
| 75 | + { |
| 76 | + $filled = new class() extends AbstractCypherContainer { |
| 77 | + public function getIterator() |
| 78 | + { |
| 79 | + yield 'a' => 'b'; |
| 80 | + yield 'c' => 'd'; |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + self::assertEquals('{"a":"b","c":"d"}', json_encode($filled, JSON_THROW_ON_ERROR)); |
| 85 | + |
| 86 | + self::assertFalse(isset($filled[0])); |
| 87 | + self::assertNull($filled[0] ?? null); |
| 88 | + |
| 89 | + self::assertTrue(isset($filled['a'])); |
| 90 | + self::assertTrue(isset($filled['c'])); |
| 91 | + self::assertEquals('b', $filled['a']); |
| 92 | + self::assertEquals('d', $filled['c']); |
| 93 | + |
| 94 | + $caught = null; |
| 95 | + try { |
| 96 | + $filled[0] = 'abc'; |
| 97 | + } catch (BadMethodCallException $e) { |
| 98 | + $caught = true; |
| 99 | + } |
| 100 | + self::assertTrue($caught, 'Filled is writable'); |
| 101 | + $caught = null; |
| 102 | + try { |
| 103 | + unset($filled[0]); |
| 104 | + } catch (BadMethodCallException $e) { |
| 105 | + $caught = true; |
| 106 | + } |
| 107 | + self::assertTrue($caught, 'Filled is writable'); |
| 108 | + |
| 109 | + $caught = null; |
| 110 | + try { |
| 111 | + $filled[0]; |
| 112 | + } catch (InvalidArgumentException $e) { |
| 113 | + $caught = true; |
| 114 | + } |
| 115 | + self::assertTrue($caught, 'Filled has still valid access'); |
| 116 | + } |
| 117 | +} |
0 commit comments