Skip to content

Commit 21ec7b1

Browse files
committed
updated test
1 parent 5a77694 commit 21ec7b1

File tree

9 files changed

+237
-448
lines changed

9 files changed

+237
-448
lines changed

src/Neo4jQueryAPI.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Client;
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\Exception\RequestException;
9+
use Neo4j\QueryAPI\Results\ResultRow;
910
use Neo4j\QueryAPI\Results\ResultSet;
1011
use Neo4j\QueryAPI\Exception\Neo4jException;
1112
use Psr\Http\Client\RequestExceptionInterface;
@@ -59,7 +60,18 @@ public function run(string $cypher, array $parameters, string $database = 'neo4j
5960
$data = json_decode($response->getBody()->getContents(), true);
6061
$ogm = new OGM();
6162

62-
return new ResultSet($data['data']['fields'], $data['data']['values'], $ogm);
63+
$keys = $data['data']['fields'];
64+
$values = $data['data']['values'];
65+
$rows = array_map(function ($resultRow) use ($ogm, $keys) {
66+
$data = [];
67+
foreach ($keys as $index => $key) {
68+
$fieldData = $resultRow[$index] ?? null;
69+
$data[$key] = $ogm->map($fieldData);
70+
}
71+
return new ResultRow($data);
72+
}, $values);
73+
74+
return new ResultSet($rows);
6375
} catch (RequestExceptionInterface $e) {
6476
$response = $e->getResponse();
6577
if ($response !== null) {

src/OGM.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@ public function map(array $object): mixed
1717
{
1818
return match ($object['$type']) {
1919
'Integer' => $object['_value'],
20-
'float' => $object['_value'],
20+
'Float' => $object['_value'],
2121
'String' => $object['_value'],
2222
'Boolean' => $object['_value'],
2323
'Null' => $object['_value'],
24-
'Array' => $object['_value'],
24+
'Array' => $object['_value'], // Handle generic arrays
25+
'List' => array_map([$this, 'map'], $object['_value']), // Recursively map lists
2526
'Duration' => $object['_value'],
2627
'OffsetDateTime' => $object['_value'],
28+
'Node' => $this->mapNode($object['_value']),
2729
'Map' => $this->mapProperties($object['_value']),
2830
'Point' => $this->parseWKT($object['_value']),
29-
'Node' => $this->mapNode($object['_value']),
3031
'Relationship' => $this->mapRelationship($object['_value']),
3132
'Path' => $this->mapPath($object['_value']),
3233
default => throw new \InvalidArgumentException('Unknown type: ' . $object['$type']),
3334
};
3435
}
36+
3537
public static function parseWKT(string $wkt): Point
3638
{
3739
$sridPart = substr($wkt, 0, strpos($wkt, ';'));
@@ -62,11 +64,12 @@ public static function parseWKT(string $wkt): Point
6264
private function mapNode(array $nodeData): Node
6365
{
6466
return new Node(
65-
$nodeData['_labels'],
66-
$this->mapProperties($nodeData['_properties'])
67+
$nodeData['_labels'], // Labels of the node
68+
$this->mapProperties($nodeData['_properties']) // Mapped properties
6769
);
6870
}
6971

72+
7073
private function mapRelationship(array $relationshipData): Relationship
7174
{
7275
return new Relationship(

src/Objects/Node.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Neo4j\QueryAPI\Objects;
43

54
/**
@@ -48,4 +47,17 @@ public function getProperties(): array
4847
{
4948
return $this->properties;
5049
}
50+
51+
/**
52+
* Convert the Node object to an array representation.
53+
*
54+
* @return array Node data as an array.
55+
*/
56+
public function toArray(): array
57+
{
58+
return [
59+
'_labels' => $this->labels,
60+
'_properties' => $this->properties,
61+
];
62+
}
5163
}

src/Results/ResultRow.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ResultRow implements ArrayAccess
3535
{
3636
public function __construct(private array $data)
3737
{
38+
3839
}
3940

4041

@@ -53,11 +54,11 @@ public function offsetGet($offset): mixed
5354

5455
public function offsetSet($offset, $value): void
5556
{
56-
throw new BadMethodCallException("You cant set the value of column {$offset}.");
57+
throw new BadMethodCallException("You can't set the value of column {$offset}.");
5758
}
5859
public function offsetUnset($offset): void
5960
{
60-
throw new BadMethodCallException("You cant Unset {$offset}.");
61+
throw new BadMethodCallException("You can't Unset {$offset}.");
6162

6263
}
6364

@@ -67,7 +68,10 @@ public function get(string $row): mixed
6768
return $this->offsetGet($row);
6869
}
6970

70-
71+
public function toArray(): array
72+
{
73+
return $this->data;
74+
}
7175
}
7276

7377

src/Results/ResultSet.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
//
32
namespace Neo4j\QueryAPI\Results;
43

54
use InvalidArgumentException;
@@ -9,18 +8,11 @@
98

109
class ResultSet implements IteratorAggregate
1110
{
12-
private array $rows;
13-
14-
public function __construct(private array $keys, private array $resultRows, private OGM $ogm)
11+
/**
12+
* @param list<ResultRow> $rows
13+
*/
14+
public function __construct(private readonly array $rows)
1515
{
16-
$this->rows = array_map(function ($resultRow) {
17-
$data = [];
18-
foreach ($this->keys as $index => $key) {
19-
$fieldData = $resultRow[$index] ?? null;
20-
$data[$key] = $this->ogm->map($fieldData);
21-
}
22-
return new ResultRow($data);
23-
}, $this->resultRows);
2416
}
2517

2618
public function getIterator(): Traversable

tests/Integration/Neo4jOGMTest.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,7 @@ public static function integerDataProvider(): array
3232
];
3333
}
3434

35-
public static function floatDataProvider(): array
36-
{
37-
return [
38-
'Test with height 1.75' => [
39-
'CREATE (n:Person {height: $height}) RETURN n.height',
40-
['height' => 1.75],
41-
1.75, // Expecting a float value directly, not wrapped in an array
42-
],
4335

44-
];
45-
}
4636

4737
public static function nullDataProvider()
4838
{
@@ -92,7 +82,7 @@ public function testInteger(): void
9282
public function testFloat(): void
9383
{
9484
$this->assertEquals(1.75, $this->ogm->map([
95-
'$type' => 'float',
85+
'$type' => 'Float',
9686
'_value' => 1.75,
9787
]));
9888
}
@@ -397,16 +387,8 @@ public function testWithPath()
397387
$this->assertEquals($expectedResult, $actual);
398388
}
399389

400-
#[DataProvider('floatDataProvider')]
401-
public function testWithFloat(string $query, array $parameters, float $expectedResult): void
402-
{
403-
$actual = $this->ogm->map([
404-
'$type' => 'float',
405-
'_value' => $parameters['height'],
406-
]);
407390

408-
$this->assertEquals($expectedResult, $actual);
409-
}
391+
410392

411393

412394
#[DataProvider('nullDataProvider')]

0 commit comments

Comments
 (0)