Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c873406
written the test for neo4jOGM test using ogm class
p123-stack Dec 11, 2024
9d7bca0
test code for OGM test cases
p123-stack Dec 12, 2024
450102a
test code for OGM test cases
p123-stack Dec 12, 2024
2de50a1
added the test code for float and 3D point datatype
p123-stack Dec 12, 2024
32866cc
OGM test code for all point datatypes
p123-stack Dec 13, 2024
6d0fbd3
OGM test code for all datatypes
p123-stack Dec 13, 2024
939d106
Changed the code for point test cases
p123-stack Dec 13, 2024
c2a583b
OGM test code for points
p123-stack Dec 13, 2024
8ed53ba
test code
p123-stack Dec 16, 2024
ede39eb
test code
p123-stack Dec 16, 2024
683f37c
updated test code for ci
p123-stack Dec 16, 2024
1667917
add result cache to gitignore
p123-stack Dec 16, 2024
5f53d22
Merge branch 'main' into pratiksha
p123-stack Dec 16, 2024
42de66a
enable ci for every pull_request
p123-stack Dec 16, 2024
cc44e63
Merge branch 'pratiksha' of https://github.com/nagels-tech/Neo4j-Clie…
p123-stack Dec 16, 2024
945e9f8
test code with parameters
p123-stack Dec 17, 2024
97b5c6c
added the neo4j exception implementation
p123-stack Dec 18, 2024
7226193
Exception testing code
p123-stack Dec 19, 2024
5a77694
Merge remote-tracking branch 'origin/main' into pratiksha
p123-stack Dec 19, 2024
21ec7b1
updated test
p123-stack Dec 20, 2024
0ca519a
added extra material for pratiksha to understand the query api use ca…
transistive Dec 27, 2024
b471ee2
transaction code
p123-stack Dec 27, 2024
e05524e
Transaction test case code
p123-stack Dec 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.result.cache

Large diffs are not rendered by default.

56 changes: 54 additions & 2 deletions src/OGM.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Neo4j\QueryAPI;

use Neo4j\QueryAPI\Objects\Point;
use Neo4j\QueryAPI\Objects\Node;
use Neo4j\QueryAPI\Objects\Relationship;
use Neo4j\QueryAPI\Objects\Path;

class OGM
{
Expand All @@ -16,7 +18,15 @@ public function map(array $object): mixed
'Integer' => $object['_value'],
'String' => $object['_value'],
'Boolean' => $object['_value'],
'Null' => $object['_value'],
'Array' => $object['_value'],
'Duration' => $object['_value'],
'OffsetDateTime' => $object['_value'],
'Point' => $this->parseWKT($object['_value']),
'Node' => $this->mapNode($object['_value']),
'Relationship' => $this->mapRelationship($object['_value']),
'Path' => $this->mapPath($object['_value']),
default => throw new \InvalidArgumentException('Unknown type: ' . $object['$type']),
};
}

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

return new Point((float)$longitude, (float)$latitude, $srid);
}
}

private function mapNode(array $nodeData): Node
{
return new Node(
$nodeData['_labels'], // Node labels
$this->mapProperties($nodeData['_properties']) // Node properties
);
}

private function mapRelationship(array $relationshipData): Relationship
{
return new Relationship(
$relationshipData['_type'], // Relationship type
$this->mapProperties($relationshipData['_properties']) // Relationship properties
);
}

private function mapPath(array $pathData): Path
{
$nodes = [];
$relationships = [];

foreach ($pathData as $item) {
if ($item['$type'] === 'Node') {
$nodes[] = $this->mapNode($item['_value']);
} elseif ($item['$type'] === 'Relationship') {
$relationships[] = $this->mapRelationship($item['_value']);
}
}

return new Path($nodes, $relationships);
}

private function mapProperties(array $properties): array
{
// Map properties to their raw values (no special parsing)
$mappedProperties = [];
foreach ($properties as $key => $value) {
$mappedProperties[$key] = $this->map($value);
}
return $mappedProperties;
}
}
25 changes: 25 additions & 0 deletions src/Objects/Node.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
namespace Neo4j\QueryAPI\Objects;

class Node
{
private $labels;
private $properties;

public function __construct(array $labels, array $properties)
{
$this->labels = $labels;
$this->properties = $properties;
}

public function getLabels(): array
{
return $this->labels;
}

public function getProperties(): array
{
return $this->properties;
}
}

26 changes: 26 additions & 0 deletions src/Objects/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Neo4j\QueryAPI\Objects;

class Path
{
private $nodes;
private $relationships;

public function __construct(array $nodes, array $relationships)
{
$this->nodes = $nodes;
$this->relationships = $relationships;
}

public function getNodes()
{
return $this->nodes;
}

public function getRelationships()
{
return $this->relationships;
}
}
10 changes: 10 additions & 0 deletions src/Objects/Person.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace Neo4j\QueryAPI\Objects;

class Person extends Node
{
public function __construct(array $properties)
{
parent::__construct(['Person'], $properties); // Pass the labels and properties correctly
}
}
28 changes: 26 additions & 2 deletions src/Objects/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,32 @@ class Point
public function __construct(
public float $longitude,
public float $latitude,
public string $crs
public int $srid
)
{
}
}


public function getLongitude(): float
{
return $this->longitude;
}


public function getLatitude(): float
{
return $this->latitude;
}


public function getSrid(): int
{
return $this->srid;
}


public function __toString(): string
{
return "SRID={$this->srid};POINT ({$this->longitude} {$this->latitude})";
}
}
26 changes: 26 additions & 0 deletions src/Objects/Relationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php


namespace Neo4j\QueryAPI\Objects;

class Relationship
{
private $type;
private $properties;

public function __construct($type, array $properties = [])
{
$this->type = $type;
$this->properties = $properties;
}

public function getType()
{
return $this->type;
}

public function getProperties()
{
return $this->properties;
}
}
143 changes: 142 additions & 1 deletion tests/Integration/Neo4jOGMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Neo4j\QueryAPI\Tests\Integration;

use Neo4j\QueryAPI\Objects\Person;
use Neo4j\QueryAPI\Objects\Point;
use Neo4j\QueryAPI\Objects\Relationship;
use Neo4j\QueryAPI\OGM;
use PHPUnit\Framework\TestCase;

Expand All @@ -22,11 +25,149 @@ public function testInteger(): void
]));
}

public function testString(): void
{
$this->assertEquals('Alice', $this->ogm->map([
'$type' => 'String',
'_value' => 'Alice',
]));
}

public function testBoolean(): void
{
$this->assertEquals(true, $this->ogm->map([
'$type' => 'Boolean',
'_value' => true,
]));
}

public function testNull(): void
{
$this->assertEquals(null, $this->ogm->map([
'$type' => 'Null',
'_value' => null,
]));
}

public function testDate(): void
{
$this->assertEquals('2024-12-11T11:00:00Z', $this->ogm->map([
'$type' => 'OffsetDateTime',
'_value' => '2024-12-11T11:00:00Z',
]));
}
public function testDuration(): void
{
$this->assertEquals('P14DT16H12M', $this->ogm->map([
'$type' => 'Duration',
'_value' => 'P14DT16H12M',
]));
}


public function testPoint(): void
{
$this->assertEquals(30, $this->ogm->map([
$point = $this->ogm->map([
'$type' => 'Point',
'_value' => 'SRID=4326;POINT (1.2 3.4)',
]);

$this->assertInstanceOf(Point::class, $point);
$this->assertEquals(1.2, $point->getLongitude());
$this->assertEquals(3.4, $point->getLatitude());
$this->assertEquals(4326, $point->getSrid());
}
public function testArray(): void
{
$arrayData = ['developer', 'python', 'neo4j'];

$this->assertEquals($arrayData, $this->ogm->map([
'$type' => 'Array',
'_value' => $arrayData,
]));
}
public function testWithNode()
{
// Simulate the result from the Neo4j database query
$data = [
'data' => [
'fields' => ['n'],
'values' => [
[
[
'$type' => 'Node',
'_value' => [
'_labels' => ['Person'],
'_properties' => [
'name' => ['_value' => 'Ayush'],
'age' => ['_value' => 30],
'location' => ['_value' => 'New York'],
]
],
]
]
]
]
];


$nodeData = $data['data']['values'][0][0]['_value'];
$node = new Person($nodeData['_properties']);

$properties = $node->getProperties();

$this->assertEquals('Ayush', $properties['name']['_value']); // Ensure 'name' is a string
$this->assertEquals(30, $properties['age']['_value']); // Ensure 'age' is an integer
$this->assertEquals('New York', $properties['location']['_value']); // Ensure 'location' is a string
}

public function testWithSimpleRelationship()
{
// Simulate the result from the Neo4j database query
$data = [
'data' => [
'fields' => ['a', 'b', 'r'],
'values' => [
[
[
'$type' => 'Node',
'_value' => [
'_labels' => ['Person'],
'_properties' => ['name' => ['_value' => 'A']]
]
],
[
'$type' => 'Node',
'_value' => [
'_labels' => ['Person'],
'_properties' => ['name' => ['_value' => 'B']]
]
],
[
'$type' => 'Relationship',
'_value' => [
'_type' => 'FRIENDS',
'_properties' => []
]
]
]
]
]
];

// Parse the response and create the nodes and relationship
$aData = $data['data']['values'][0][0]['_value'];
$bData = $data['data']['values'][0][1]['_value'];
$relationshipData = $data['data']['values'][0][2]['_value'];

$aNode = new Person($aData['_properties']);
$bNode = new Person($bData['_properties']);
$relationship = new Relationship($relationshipData['_type'], $relationshipData['_properties']);

// Assertions
$this->assertEquals('A', $aNode->getProperties()['name']['_value']);
$this->assertEquals('B', $bNode->getProperties()['name']['_value']);
$this->assertEquals('FRIENDS', $relationship->getType());
}

}