Skip to content

Commit c873406

Browse files
committed
written the test for neo4jOGM test using ogm class
1 parent 6371f70 commit c873406

File tree

8 files changed

+310
-6
lines changed

8 files changed

+310
-6
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/OGM.php

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2-
32
namespace Neo4j\QueryAPI;
43

54
use Neo4j\QueryAPI\Objects\Point;
5+
use Neo4j\QueryAPI\Objects\Node;
6+
use Neo4j\QueryAPI\Objects\Relationship;
7+
use Neo4j\QueryAPI\Objects\Path;
68

79
class OGM
810
{
@@ -16,7 +18,15 @@ public function map(array $object): mixed
1618
'Integer' => $object['_value'],
1719
'String' => $object['_value'],
1820
'Boolean' => $object['_value'],
21+
'Null' => $object['_value'],
22+
'Array' => $object['_value'],
23+
'Duration' => $object['_value'],
24+
'OffsetDateTime' => $object['_value'],
1925
'Point' => $this->parseWKT($object['_value']),
26+
'Node' => $this->mapNode($object['_value']),
27+
'Relationship' => $this->mapRelationship($object['_value']),
28+
'Path' => $this->mapPath($object['_value']),
29+
default => throw new \InvalidArgumentException('Unknown type: ' . $object['$type']),
2030
};
2131
}
2232

@@ -32,4 +42,46 @@ private function parseWKT(string $wkt): Point
3242

3343
return new Point((float)$longitude, (float)$latitude, $srid);
3444
}
35-
}
45+
46+
private function mapNode(array $nodeData): Node
47+
{
48+
return new Node(
49+
$nodeData['_labels'], // Node labels
50+
$this->mapProperties($nodeData['_properties']) // Node properties
51+
);
52+
}
53+
54+
private function mapRelationship(array $relationshipData): Relationship
55+
{
56+
return new Relationship(
57+
$relationshipData['_type'], // Relationship type
58+
$this->mapProperties($relationshipData['_properties']) // Relationship properties
59+
);
60+
}
61+
62+
private function mapPath(array $pathData): Path
63+
{
64+
$nodes = [];
65+
$relationships = [];
66+
67+
foreach ($pathData as $item) {
68+
if ($item['$type'] === 'Node') {
69+
$nodes[] = $this->mapNode($item['_value']);
70+
} elseif ($item['$type'] === 'Relationship') {
71+
$relationships[] = $this->mapRelationship($item['_value']);
72+
}
73+
}
74+
75+
return new Path($nodes, $relationships);
76+
}
77+
78+
private function mapProperties(array $properties): array
79+
{
80+
// Map properties to their raw values (no special parsing)
81+
$mappedProperties = [];
82+
foreach ($properties as $key => $value) {
83+
$mappedProperties[$key] = $this->map($value);
84+
}
85+
return $mappedProperties;
86+
}
87+
}

src/Objects/Node.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Objects;
3+
4+
class Node
5+
{
6+
private $labels;
7+
private $properties;
8+
9+
public function __construct(array $labels, array $properties)
10+
{
11+
$this->labels = $labels;
12+
$this->properties = $properties;
13+
}
14+
15+
public function getLabels(): array
16+
{
17+
return $this->labels;
18+
}
19+
20+
public function getProperties(): array
21+
{
22+
return $this->properties;
23+
}
24+
}
25+

src/Objects/Path.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
namespace Neo4j\QueryAPI\Objects;
5+
6+
class Path
7+
{
8+
private $nodes;
9+
private $relationships;
10+
11+
public function __construct(array $nodes, array $relationships)
12+
{
13+
$this->nodes = $nodes;
14+
$this->relationships = $relationships;
15+
}
16+
17+
public function getNodes()
18+
{
19+
return $this->nodes;
20+
}
21+
22+
public function getRelationships()
23+
{
24+
return $this->relationships;
25+
}
26+
}

src/Objects/Person.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Objects;
3+
4+
class Person extends Node
5+
{
6+
public function __construct(array $properties)
7+
{
8+
parent::__construct(['Person'], $properties); // Pass the labels and properties correctly
9+
}
10+
}

src/Objects/Point.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,32 @@ class Point
77
public function __construct(
88
public float $longitude,
99
public float $latitude,
10-
public string $crs
10+
public int $srid
1111
)
1212
{
1313
}
14-
}
14+
15+
16+
public function getLongitude(): float
17+
{
18+
return $this->longitude;
19+
}
20+
21+
22+
public function getLatitude(): float
23+
{
24+
return $this->latitude;
25+
}
26+
27+
28+
public function getSrid(): int
29+
{
30+
return $this->srid;
31+
}
32+
33+
34+
public function __toString(): string
35+
{
36+
return "SRID={$this->srid};POINT ({$this->longitude} {$this->latitude})";
37+
}
38+
}

src/Objects/Relationship.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
4+
namespace Neo4j\QueryAPI\Objects;
5+
6+
class Relationship
7+
{
8+
private $type;
9+
private $properties;
10+
11+
public function __construct($type, array $properties = [])
12+
{
13+
$this->type = $type;
14+
$this->properties = $properties;
15+
}
16+
17+
public function getType()
18+
{
19+
return $this->type;
20+
}
21+
22+
public function getProperties()
23+
{
24+
return $this->properties;
25+
}
26+
}

tests/Integration/Neo4jOGMTest.php

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Neo4j\QueryAPI\Tests\Integration;
44

5+
use Neo4j\QueryAPI\Objects\Person;
6+
use Neo4j\QueryAPI\Objects\Point;
7+
use Neo4j\QueryAPI\Objects\Relationship;
58
use Neo4j\QueryAPI\OGM;
69
use PHPUnit\Framework\TestCase;
710

@@ -22,11 +25,149 @@ public function testInteger(): void
2225
]));
2326
}
2427

28+
public function testString(): void
29+
{
30+
$this->assertEquals('Alice', $this->ogm->map([
31+
'$type' => 'String',
32+
'_value' => 'Alice',
33+
]));
34+
}
35+
36+
public function testBoolean(): void
37+
{
38+
$this->assertEquals(true, $this->ogm->map([
39+
'$type' => 'Boolean',
40+
'_value' => true,
41+
]));
42+
}
43+
44+
public function testNull(): void
45+
{
46+
$this->assertEquals(null, $this->ogm->map([
47+
'$type' => 'Null',
48+
'_value' => null,
49+
]));
50+
}
51+
52+
public function testDate(): void
53+
{
54+
$this->assertEquals('2024-12-11T11:00:00Z', $this->ogm->map([
55+
'$type' => 'OffsetDateTime',
56+
'_value' => '2024-12-11T11:00:00Z',
57+
]));
58+
}
59+
public function testDuration(): void
60+
{
61+
$this->assertEquals('P14DT16H12M', $this->ogm->map([
62+
'$type' => 'Duration',
63+
'_value' => 'P14DT16H12M',
64+
]));
65+
}
66+
67+
2568
public function testPoint(): void
2669
{
27-
$this->assertEquals(30, $this->ogm->map([
70+
$point = $this->ogm->map([
2871
'$type' => 'Point',
2972
'_value' => 'SRID=4326;POINT (1.2 3.4)',
73+
]);
74+
75+
$this->assertInstanceOf(Point::class, $point);
76+
$this->assertEquals(1.2, $point->getLongitude());
77+
$this->assertEquals(3.4, $point->getLatitude());
78+
$this->assertEquals(4326, $point->getSrid());
79+
}
80+
public function testArray(): void
81+
{
82+
$arrayData = ['developer', 'python', 'neo4j'];
83+
84+
$this->assertEquals($arrayData, $this->ogm->map([
85+
'$type' => 'Array',
86+
'_value' => $arrayData,
3087
]));
3188
}
89+
public function testWithNode()
90+
{
91+
// Simulate the result from the Neo4j database query
92+
$data = [
93+
'data' => [
94+
'fields' => ['n'],
95+
'values' => [
96+
[
97+
[
98+
'$type' => 'Node',
99+
'_value' => [
100+
'_labels' => ['Person'],
101+
'_properties' => [
102+
'name' => ['_value' => 'Ayush'],
103+
'age' => ['_value' => 30],
104+
'location' => ['_value' => 'New York'],
105+
]
106+
],
107+
]
108+
]
109+
]
110+
]
111+
];
112+
113+
114+
$nodeData = $data['data']['values'][0][0]['_value'];
115+
$node = new Person($nodeData['_properties']);
116+
117+
$properties = $node->getProperties();
118+
119+
$this->assertEquals('Ayush', $properties['name']['_value']); // Ensure 'name' is a string
120+
$this->assertEquals(30, $properties['age']['_value']); // Ensure 'age' is an integer
121+
$this->assertEquals('New York', $properties['location']['_value']); // Ensure 'location' is a string
122+
}
123+
124+
public function testWithSimpleRelationship()
125+
{
126+
// Simulate the result from the Neo4j database query
127+
$data = [
128+
'data' => [
129+
'fields' => ['a', 'b', 'r'],
130+
'values' => [
131+
[
132+
[
133+
'$type' => 'Node',
134+
'_value' => [
135+
'_labels' => ['Person'],
136+
'_properties' => ['name' => ['_value' => 'A']]
137+
]
138+
],
139+
[
140+
'$type' => 'Node',
141+
'_value' => [
142+
'_labels' => ['Person'],
143+
'_properties' => ['name' => ['_value' => 'B']]
144+
]
145+
],
146+
[
147+
'$type' => 'Relationship',
148+
'_value' => [
149+
'_type' => 'FRIENDS',
150+
'_properties' => []
151+
]
152+
]
153+
]
154+
]
155+
]
156+
];
157+
158+
// Parse the response and create the nodes and relationship
159+
$aData = $data['data']['values'][0][0]['_value'];
160+
$bData = $data['data']['values'][0][1]['_value'];
161+
$relationshipData = $data['data']['values'][0][2]['_value'];
162+
163+
$aNode = new Person($aData['_properties']);
164+
$bNode = new Person($bData['_properties']);
165+
$relationship = new Relationship($relationshipData['_type'], $relationshipData['_properties']);
166+
167+
// Assertions
168+
$this->assertEquals('A', $aNode->getProperties()['name']['_value']);
169+
$this->assertEquals('B', $bNode->getProperties()['name']['_value']);
170+
$this->assertEquals('FRIENDS', $relationship->getType());
171+
}
172+
32173
}

0 commit comments

Comments
 (0)