Skip to content

Commit

Permalink
added more parameterhelper and cypherformatter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
transistive committed Jan 19, 2021
1 parent fece01b commit b497051
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 4 deletions.
5 changes: 4 additions & 1 deletion phpunit.coverage.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
>
<php>
<ini name="memory_limit" value="512M" />
</php>
<testsuites>
<testsuite name="Neo4j php test suite">
<testsuite name="Neo4j php test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
Expand Down
6 changes: 3 additions & 3 deletions src/ParameterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -58,7 +58,7 @@ public static function asParameter($value)
*
* @return Map<array-key, iterable|scalar|stdClass|null>
*/
public static function formatParameters(iterable $parameters): iterable
public static function formatParameters(iterable $parameters): Map
{
/** @var Map<array-key, iterable|scalar|stdClass|null> $tbr */
$tbr = new Map();
Expand All @@ -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);
Expand Down
76 changes: 76 additions & 0 deletions tests/Unit/BoltCypherFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Laudis Neo4j package.
*
* (c) Laudis technologies <http://laudis.tech>
*
* 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], []]);
}
}
137 changes: 137 additions & 0 deletions tests/Unit/ParameterHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Laudis Neo4j package.
*
* (c) Laudis technologies <http://laudis.tech>
*
* 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<iterable|scalar|null> */
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);
}
}

0 comments on commit b497051

Please sign in to comment.