Skip to content

Commit 1ab6a4e

Browse files
added the integration tests for OGM data objects
1 parent 119d724 commit 1ab6a4e

File tree

15 files changed

+446
-43
lines changed

15 files changed

+446
-43
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ coverage
1919
xml-coverage
2020
composer.lock
2121

22-
.env
22+
.env
23+
.phpunit.result.cache

phpUnit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php" colors="true" verbose="true">
3+
<php>
4+
<!-- Database Configuration -->
5+
<env name="NEO4J_ADDRESS" value="https://6f72daa1.databases.neo4j.io" />
6+
<env name="NEO4J_USERNAME" value="neo4j" />
7+
<env name="NEO4J_PASSWORD" value="9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0" />
8+
</php>
9+
<testsuites>
10+
<testsuite name="Application Test Suite">
11+
<directory>./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
</phpunit>

src/OGM.php

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
namespace Neo4j\QueryAPI;
44

5+
use DateTimeZone;
56
use Neo4j\QueryAPI\Objects\Point;
67
use Neo4j\QueryAPI\Objects\Node;
78
use Neo4j\QueryAPI\Objects\Relationship;
89
use Neo4j\QueryAPI\Objects\Path;
910
use InvalidArgumentException;
11+
use Neo4j\QueryAPI\Objects\Temporal\Date;
12+
use Neo4j\QueryAPI\Objects\Temporal\DateTime;
13+
use Neo4j\QueryAPI\Objects\Temporal\DateTimeZoneId;
14+
use Neo4j\QueryAPI\Objects\Temporal\Duration;
15+
use Neo4j\QueryAPI\Objects\Temporal\LocalDateTime;
16+
use Neo4j\QueryAPI\Objects\Temporal\LocalTime;
17+
use Neo4j\QueryAPI\Objects\Temporal\Time;
1018

1119
final class OGM
1220
{
@@ -21,14 +29,24 @@ public function map(array $data): mixed
2129
}
2230

2331
return match ($data['$type']) {
24-
'Integer', 'Float', 'String', 'Boolean', 'Duration', 'OffsetDateTime' => $data['_value'],
32+
'Integer', 'Float', 'Boolean' => $data['_value'],
2533
'Array', 'List' => is_array($data['_value']) ? array_map([$this, 'map'], $data['_value']) : [],
2634
'Null' => null,
2735
'Node' => $this->mapNode($data['_value']),
2836
'Map' => is_array($data['_value']) ? $this->mapProperties($data['_value']) : [],
2937
'Point' => $this->parsePoint($data['_value']),
3038
'Relationship' => $this->mapRelationship($data['_value']),
3139
'Path' => $this->mapPath($data['_value']),
40+
'Date' => $this->mapDate($data['_value']),
41+
'OffsetDateTime' => $this->mapDateTime($data['_value']),
42+
'Time' => $this->mapTime($data['_value']),
43+
'LocalTime' => $this->mapLocalTime($data['_value']),
44+
'LocalDateTime'=> $this->mapLocalDateTime($data['_value']),
45+
'Duration'=>$this->mapDuration($data['_value']),
46+
47+
'String' => $this->isValidTimeZone($data['_value'])
48+
? new DateTimeZoneId($data['_value']) // Convert timezone strings to `DateTimeZoneId`
49+
: $data['_value'],
3250
default => throw new InvalidArgumentException('Unknown type: ' . json_encode($data, JSON_THROW_ON_ERROR)),
3351
};
3452
}
@@ -37,10 +55,10 @@ public function map(array $data): mixed
3755
private function parsePoint(string $value): Point
3856
{
3957
if (preg_match('/SRID=(\d+);POINT(?: Z)? \(([-\d.]+) ([-\d.]+)(?: ([-\d.]+))?\)/', $value, $matches)) {
40-
$srid = (int) $matches[1];
41-
$x = (float) $matches[2];
42-
$y = (float) $matches[3];
43-
$z = isset($matches[4]) ? (float) $matches[4] : null;
58+
$srid = (int)$matches[1];
59+
$x = (float)$matches[2];
60+
$y = (float)$matches[3];
61+
$z = isset($matches[4]) ? (float)$matches[4] : null;
4462

4563
return new Point($x, $y, $z, $srid);
4664
}
@@ -129,5 +147,46 @@ private function mapProperties(array $properties): array
129147
return $mappedProperties;
130148
}
131149

150+
private function mapDate(string $value)
151+
{
152+
$totalDaysSinceEpoch = (new \DateTime($value))->diff(new \DateTime('@0'))->days;
153+
154+
return new Date($totalDaysSinceEpoch);
155+
}
156+
157+
private function mapDateTime(string $value)
158+
{
159+
return new DateTime($value);
160+
}
161+
162+
private function mapDateTimeZoneId(string $value)
163+
{
164+
return new DateTimeZoneId($value);
165+
}
166+
167+
private function isValidTimeZone(string $value): bool
168+
{
169+
return in_array($value, timezone_identifiers_list(), true);
170+
}
132171

172+
private function mapTime(mixed $_value)
173+
{
174+
return new Time($_value);
175+
176+
}
177+
178+
private function mapLocalTime(mixed $_value)
179+
{
180+
return new LocalTime($_value);
181+
}
182+
183+
private function mapLocalDateTime(mixed $_value)
184+
{
185+
return new LocalDateTime($_value);
186+
}
187+
188+
private function mapDuration(mixed $_value)
189+
{
190+
return new Duration($_value);
191+
}
133192
}

src/Objects/Temporal/Date.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects\Temporal;
4+
5+
final class Date
6+
{
7+
public int $days;
8+
9+
public function __construct(int $days)
10+
{
11+
$this->days = $days;
12+
}
13+
14+
/**
15+
* Returns the number of days since the Unix epoch.
16+
*/
17+
public function getDays(): int
18+
{
19+
return $this->days;
20+
}
21+
22+
/**
23+
* Converts the stored date into a DateTimeImmutable.
24+
*/
25+
public function toDateTimeImmutable(): \DateTimeImmutable
26+
{
27+
$dt = new \DateTimeImmutable('@0');
28+
return $dt->modify(sprintf('+%d days', $this->days));
29+
}
30+
31+
public function __toString(): string
32+
{
33+
return sprintf("Date(%d days since epoch)", $this->days);
34+
}
35+
}

src/Objects/Temporal/DateTime.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects\Temporal;
4+
5+
final class DateTime
6+
{
7+
private \DateTimeImmutable $dateTime;
8+
9+
public function __construct(string $dateTime)
10+
{
11+
$this->dateTime = new \DateTimeImmutable($dateTime);
12+
}
13+
14+
public function getDateTime(): \DateTimeImmutable
15+
{
16+
return $this->dateTime;
17+
}
18+
19+
public function __toString(): string
20+
{
21+
return $this->dateTime->format('c');
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects\Temporal;
4+
5+
final class DateTimeZoneId
6+
{
7+
private string $zoneId;
8+
9+
public function __construct(string $zoneId)
10+
{
11+
$this->zoneId = $zoneId;
12+
}
13+
14+
public function getZoneId(): string
15+
{
16+
return $this->zoneId;
17+
}
18+
19+
public function __toString(): string
20+
{
21+
return $this->zoneId;
22+
}
23+
}

src/Objects/Temporal/Duration.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects\Temporal;
4+
5+
final class Duration
6+
{
7+
private string $duration;
8+
9+
public function __construct(string $duration)
10+
{
11+
$this->duration = $duration;
12+
}
13+
14+
public function getDuration(): string
15+
{
16+
return $this->duration;
17+
}
18+
19+
public function __toString(): string
20+
{
21+
return $this->duration;
22+
}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects\Temporal;
4+
5+
final class LocalDateTime
6+
{
7+
private \DateTimeImmutable $localDateTime;
8+
9+
public function __construct(string $localDateTime)
10+
{
11+
$this->localDateTime = new \DateTimeImmutable($localDateTime);
12+
}
13+
14+
public function getLocalDateTime(): \DateTimeImmutable
15+
{
16+
return $this->localDateTime;
17+
}
18+
19+
public function __toString(): string
20+
{
21+
// Adjust the format as necessary (without timezone info)
22+
return $this->localDateTime->format('Y-m-d\TH:i:s');
23+
}
24+
}

src/Objects/Temporal/LocalTime.php

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

src/Objects/Temporal/Time.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects\Temporal;
4+
5+
final class Time
6+
{
7+
private string $time; // You might later parse this into components if needed.
8+
9+
public function __construct(string $time)
10+
{
11+
$this->time = $time;
12+
}
13+
14+
public function getTime(): string
15+
{
16+
return $this->time;
17+
}
18+
19+
public function __toString(): string
20+
{
21+
return $this->time;
22+
}
23+
}

0 commit comments

Comments
 (0)