Skip to content

Commit 15f57c0

Browse files
committed
fixes #20
1 parent 8642a4a commit 15f57c0

File tree

4 files changed

+103
-46
lines changed

4 files changed

+103
-46
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"php-ds/php-ds": "1.*",
3030
"psr/http-client": "^1.0",
3131
"php-http/message-factory": "^1.0",
32-
"stefanak-michal/bolt": "^2.0"
32+
"stefanak-michal/bolt": "^2.1"
3333
},
3434
"suggest": {
3535
"ext-sockets": "Needed to implement bolt protocol",

composer.lock

Lines changed: 35 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Formatter/BoltCypherFormatter.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Laudis\Neo4j\Formatter;
1515

16+
use Bolt\structures\Path;
1617
use Ds\Map;
1718
use Ds\Vector;
1819
use UnexpectedValueException;
@@ -53,13 +54,37 @@ private function formatRow(array $meta, array $result): Map
5354
return $map;
5455
}
5556

57+
private function mapPath(Path $path): array
58+
{
59+
$rels = $path->rels();
60+
$nodes = $path->nodes();
61+
$tbr = [];
62+
/**
63+
* @var mixed $node
64+
*/
65+
foreach ($nodes as $i => $node) {
66+
/** @var mixed */
67+
$tbr[] = $node;
68+
if (isset($rels[$i])) {
69+
/** @var mixed */
70+
$tbr[] = $rels[$i];
71+
}
72+
}
73+
74+
return $tbr;
75+
}
76+
5677
/**
5778
* @param mixed $value
5879
*
5980
* @return scalar|array|null
6081
*/
6182
private function mapValue($value)
6283
{
84+
if ($value instanceof Path) {
85+
$value = $this->mapPath($value);
86+
}
87+
6388
if (is_object($value)) {
6489
return $this->objectToProperty($value);
6590
}
@@ -77,8 +102,13 @@ private function mapValue($value)
77102

78103
private function objectToProperty(object $object): array
79104
{
105+
if ($object instanceof Path) {
106+
return $this->mapPath($object);
107+
}
108+
80109
if (!method_exists($object, 'properties')) {
81-
throw new UnexpectedValueException('Cannot handle objects without a properties method. Class given: '.get_class($object));
110+
$message = 'Cannot handle objects without a properties method. Class given: '.get_class($object);
111+
throw new UnexpectedValueException($message);
82112
}
83113

84114
/** @var array $properties */

tests/Integration/ComplexQueryTests.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function setUp(): void
4040
public function testListParameterHelper(string $alias): void
4141
{
4242
$result = $this->client->run(<<<'CYPHER'
43-
MATCH (x) WHERE x.slug in $listOrMap RETURN x
43+
MATCH (x) WHERE x.slug IN $listOrMap RETURN x
4444
CYPHER, ['listOrMap' => ParameterHelper::asList([])], $alias);
4545
self::assertEquals(0, $result->count());
4646
}
@@ -79,7 +79,7 @@ public function testMapParameterHelper(string $alias): void
7979
$this->client->run(<<<'CYPHER'
8080
MERGE (x:Node {slug: 'a'})
8181
WITH x
82-
MATCH (x) WHERE x.slug in $listOrMap RETURN x
82+
MATCH (x) WHERE x.slug IN $listOrMap RETURN x
8383
CYPHER, ['listOrMap' => ParameterHelper::asMap(['a' => 'b'])], $alias);
8484
}
8585

@@ -92,7 +92,7 @@ public function testArrayParameterHelper(string $alias): void
9292
$this->client->run(<<<'CYPHER'
9393
MERGE (x:Node {slug: 'a'})
9494
WITH x
95-
MATCH (x) WHERE x.slug in $listOrMap RETURN x
95+
MATCH (x) WHERE x.slug IN $listOrMap RETURN x
9696
CYPHER, ['listOrMap' => []], $alias);
9797
}
9898

@@ -105,7 +105,7 @@ public function testInvalidParameter(string $alias): void
105105
$this->client->run(<<<'CYPHER'
106106
MERGE (x:Node {slug: 'a'})
107107
WITH x
108-
MATCH (x) WHERE x.slug in $listOrMap RETURN x
108+
MATCH (x) WHERE x.slug IN $listOrMap RETURN x
109109
CYPHER, ['listOrMap' => self::generate()], $alias);
110110
}
111111

@@ -125,7 +125,7 @@ public function testInvalidParameters(string $alias): void
125125
$this->client->run(<<<'CYPHER'
126126
MERGE (x:Node {slug: 'a'})
127127
WITH x
128-
MATCH (x) WHERE x.slug in $listOrMap RETURN x
128+
MATCH (x) WHERE x.slug IN $listOrMap RETURN x
129129
CYPHER, self::generate(), $alias);
130130
}
131131

@@ -170,7 +170,7 @@ public function testPath(string $alias): void
170170
public function testNullListAndMap(string $alias): void
171171
{
172172
$results = $this->client->run(<<<'CYPHER'
173-
RETURN null as x, [1, 2, 3] as y, {x: 'x', y: 'y', z: 'z'} as z
173+
RETURN null AS x, [1, 2, 3] AS y, {x: 'x', y: 'y', z: 'z'} AS z
174174
CYPHER
175175
, ['x' => 'x', 'xy' => 'xy', 'y' => 'y', 'yz' => 'yz', 'z' => 'z'], $alias);
176176

@@ -203,6 +203,36 @@ public function testListAndMapInput(string $alias): void
203203
self::assertEquals(['list' => [1, 2, 3]], $result->get('y'));
204204
}
205205

206+
/**
207+
* @dataProvider transactionProvider
208+
*/
209+
public function testPathRetunType(string $alias): void
210+
{
211+
$this->client->run(<<<'CYPHER'
212+
MERGE (:Node {x: 'x'}) - [:Rel] -> (x:Node {x: 'y'})
213+
WITH x
214+
MERGE (x) - [:Rel] -> (:Node {x: 'z'})
215+
CYPHER
216+
, [], $alias);
217+
218+
$results = $this->client->run(<<<'CYPHER'
219+
MATCH (a:Node {x: 'x'}), (b:Node {x: 'z'}), p = shortestPath((a)-[*]-(b))
220+
RETURN p
221+
CYPHER
222+
, [], $alias);
223+
224+
self::assertEquals(1, $results->count());
225+
$result = $results->first();
226+
self::assertEquals(1, $result->count());
227+
self::assertEquals([
228+
['x' => 'x'],
229+
[],
230+
['x' => 'y'],
231+
[],
232+
['x' => 'z'],
233+
], $result->get('p'));
234+
}
235+
206236
/**
207237
* @return array<int, array<int, string>>
208238
*/

0 commit comments

Comments
 (0)