From b4970518e451c4bdb858f71216d441f90d90d97a Mon Sep 17 00:00:00 2001 From: ghlen Date: Tue, 19 Jan 2021 10:43:33 +0100 Subject: [PATCH] added more parameterhelper and cypherformatter tests --- phpunit.coverage.xml.dist | 5 +- src/ParameterHelper.php | 6 +- tests/Unit/BoltCypherFormatterTest.php | 76 ++++++++++++++ tests/Unit/ParameterHelperTest.php | 137 +++++++++++++++++++++++++ 4 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 tests/Unit/BoltCypherFormatterTest.php create mode 100644 tests/Unit/ParameterHelperTest.php diff --git a/phpunit.coverage.xml.dist b/phpunit.coverage.xml.dist index df048fd4..a2d8d6ac 100644 --- a/phpunit.coverage.xml.dist +++ b/phpunit.coverage.xml.dist @@ -2,8 +2,11 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" > + + + - + ./tests diff --git a/src/ParameterHelper.php b/src/ParameterHelper.php index d9cc1421..fe732b24 100644 --- a/src/ParameterHelper.php +++ b/src/ParameterHelper.php @@ -21,7 +21,7 @@ final class ParameterHelper { - public static function asList(iterable $iterable): Sequence + public static function asList(iterable $iterable): Vector { return new Vector($iterable); } @@ -58,7 +58,7 @@ public static function asParameter($value) * * @return Map */ - public static function formatParameters(iterable $parameters): iterable + public static function formatParameters(iterable $parameters): Map { /** @var Map $tbr */ $tbr = new Map(); @@ -83,7 +83,7 @@ private static function iterableToArray(iterable $value): array foreach ($value as $key => $val) { if (is_int($key) || is_string($key)) { /** @psalm-suppress MixedAssignment */ - $tbr[$key] = $val; + $tbr[$key] = self::asParameter($val); } else { $msg = 'Iterable parameters must have an integer or string as key values, '.gettype($key).' received.'; throw new InvalidArgumentException($msg); diff --git a/tests/Unit/BoltCypherFormatterTest.php b/tests/Unit/BoltCypherFormatterTest.php new file mode 100644 index 00000000..4f90d3df --- /dev/null +++ b/tests/Unit/BoltCypherFormatterTest.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Laudis\Neo4j\Tests\Unit; + +use Bolt\structures\Node; +use Bolt\structures\Path; +use Bolt\structures\UnboundRelationship; +use Laudis\Neo4j\Formatter\BoltCypherFormatter; +use PHPUnit\Framework\TestCase; +use stdClass; +use UnexpectedValueException; + +final class BoltCypherFormatterTest extends TestCase +{ + private BoltCypherFormatter $formatter; + + protected function setUp(): void + { + parent::setUp(); + $this->formatter = new BoltCypherFormatter(); + } + + public function testFormatPath(): void + { + $path = new Path( + [new Node(1, ['a'], ['a' => 1]), new Node(2, ['b'], ['a' => 2]), new Node(3, ['c'], ['a' => 3])], + [new UnboundRelationship(4, 'a', ['d']), new UnboundRelationship(5, 'a', ['e'])], + [1, 2, 3, 4, 5] + ); + + $results = [ + [ + $path, + ], + [ + ], + ]; + $result = $this->formatter->formatResult(['fields' => ['a']], $results); + + self::assertEquals(1, $result->count()); + self::assertEquals(1, $result->first()->count()); + self::assertEquals([ + ['a' => 1], + [0 => 'd'], + ['a' => 2], + [0 => 'e'], + ['a' => 3], + ], $result->first()->get('a', [])); + } + + public function testInvalidObject(): void + { + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Cannot handle objects without a properties method. Class given: '.stdClass::class); + $this->formatter->formatResult(['fields' => ['a']], [[new stdClass()], []]); + } + + public function testResource(): void + { + $resource = fopen('php://temp', 'b'); + $this->expectException(UnexpectedValueException::class); + $this->expectExceptionMessage('Did not expect to receive value of type: resource'); + $this->formatter->formatResult(['fields' => ['a']], [[$resource], []]); + } +} diff --git a/tests/Unit/ParameterHelperTest.php b/tests/Unit/ParameterHelperTest.php new file mode 100644 index 00000000..13c96b87 --- /dev/null +++ b/tests/Unit/ParameterHelperTest.php @@ -0,0 +1,137 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Laudis\Neo4j\Tests\Unit; + +use Ds\Map; +use Ds\Vector; +use InvalidArgumentException; +use Iterator; +use Laudis\Neo4j\ParameterHelper; +use PHPUnit\Framework\TestCase; +use stdClass; + +final class ParameterHelperTest extends TestCase +{ + /** @var iterable */ + private static iterable $invalidIterable; + + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + self::$invalidIterable = new class() implements Iterator { + private bool $initial = true; + + public function current(): int + { + return 1; + } + + public function next(): void + { + $this->initial = false; + } + + public function key(): stdClass + { + return new stdClass(); + } + + public function valid(): bool + { + return $this->initial; + } + + public function rewind(): void + { + $this->initial = true; + } + }; + } + + public function testAsList(): void + { + self::assertEquals([1, 2, 3], ParameterHelper::asList([2 => 1, 'a' => 2, 'd' => 3])->toArray()); + } + + public function testAsMap(): void + { + self::assertEquals([2 => 1, 'a' => 2, 'd' => 3], ParameterHelper::asMap([2 => 1, 'a' => 2, 'd' => 3])->toArray()); + } + + public function testFormatParameterString(): void + { + self::assertEquals(['a' => 'b', 'c' => 'd'], ParameterHelper::formatParameters([ + 'a' => 'b', + 'c' => 'd', + ])->toArray()); + } + + public function testFormatParameterInteger(): void + { + self::assertEquals([2 => 'b', 3 => 'd'], ParameterHelper::formatParameters([ + 2 => 'b', + 3 => 'd', + ])->toArray()); + } + + public function testFormatParameterVector(): void + { + self::assertEquals(['b', 'd'], ParameterHelper::formatParameters([ + 'b', + 'd', + ])->toArray()); + } + + public function testFormatParameterIterable(): void + { + self::assertEquals([[1, 2]], ParameterHelper::formatParameters([ + [1, 2], + ])->toArray()); + } + + public function testFormatParameterInvalidIterable(): void + { + $this->expectException(InvalidArgumentException::class); + ParameterHelper::formatParameters(self::$invalidIterable); + } + + public function testFormatParameterInvalidIterable2(): void + { + $this->expectException(InvalidArgumentException::class); + ParameterHelper::formatParameters([ + 'a' => [ + self::$invalidIterable, + ], + ]); + } + + public function testAsParmeterEmptyVector(): void + { + $result = ParameterHelper::asParameter(new Vector()); + self::assertIsArray($result); + self::assertCount(0, $result); + } + + public function testAsParmeterEmptyMap(): void + { + $result = ParameterHelper::asParameter(new Map()); + self::assertInstanceOf(stdClass::class, $result); + } + + public function testAsParmeterEmptyArray(): void + { + $result = ParameterHelper::asParameter([]); + self::assertInstanceOf(stdClass::class, $result); + } +}