Skip to content

Commit 945e9f8

Browse files
committed
test code with parameters
1 parent cc44e63 commit 945e9f8

File tree

3 files changed

+132
-32
lines changed

3 files changed

+132
-32
lines changed

src/OGM.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function map(array $object): mixed
2020
'Array' => $object['_value'],
2121
'Duration' => $object['_value'],
2222
'OffsetDateTime' => $object['_value'],
23-
'Map' => $object['_value'],
23+
'Map' => $this->mapProperties($object['_value']),
2424
'Point' => $this->parseWKT($object['_value']),
2525
'Node' => $this->mapNode($object['_value']),
2626
'Relationship' => $this->mapRelationship($object['_value']),
@@ -52,29 +52,7 @@ public static function parseWKT(string $wkt): Point
5252
return new Point((float)$x, (float)$y, $z !== null ? (float)$z : null, $srid);
5353
}
5454

55-
/*public static function parseWKT(string $wkt): Point
56-
[$x, $y] = $coordinates;
57-
$z = null;
58-
{
59-
$sridPart = substr($wkt, 0, strpos($wkt, ';'));
60-
$srid = (int)str_replace('SRID=', '', $sridPart);
61-
62-
$pointPart = substr($wkt, strpos($wkt, 'POINT') + 6);
63-
if (strpos($pointPart, 'Z') !== false) {
64-
$pointPart = str_replace('Z', '', $pointPart);
65-
}
66-
$pointPart = trim($pointPart, ' ()');
67-
$coordinates = explode(' ', $pointPart);
68-
69-
if (count($coordinates) === 2) {
70-
} elseif (count($coordinates) === 3) {
71-
[$x, $y, $z] = $coordinates;
72-
} else {
73-
throw new \InvalidArgumentException("Invalid WKT format: unable to parse coordinates.");
74-
}
7555

76-
return new Point((float)$x, (float)$y, $z !== null ? (float)$z : null, $srid);
77-
}*/
7856

7957

8058
private function mapNode(array $nodeData): Node

tests/Integration/Neo4jOGMTest.php

Lines changed: 128 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,65 @@
88
use Neo4j\QueryAPI\Objects\Point;
99
use Neo4j\QueryAPI\Objects\Relationship;
1010
use Neo4j\QueryAPI\OGM;
11+
use PHPUnit\Framework\Attributes\DataProvider;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class Neo4jOGMTest extends TestCase
1415
{
1516
private OGM $ogm;
1617

18+
public static function integerDataProvider(): array
19+
{
20+
return [
21+
'Test with age 30' => [
22+
'CREATE (n:Person {age: $age}) RETURN n.age',
23+
['age' => 30],
24+
30, // Expected result should be just the integer, not an array
25+
],
26+
'Test with age 40' => [
27+
'CREATE (n:Person {age: $age}) RETURN n.age',
28+
['age' => 40],
29+
40, // Expected result should be just the integer
30+
],
31+
32+
];
33+
}
34+
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+
],
43+
44+
];
45+
}
46+
47+
public static function nullDataProvider()
48+
{
49+
return
50+
[
51+
52+
'testWithNull' => [
53+
'CREATE (n:Person {middleName: $middleName}) RETURN n.middleName',
54+
['middleName' => null],
55+
null,
56+
],
57+
];
58+
}
59+
60+
public static function booleanDataProvider():array
61+
{
62+
return [
63+
['query1', ['_value' => true], true],
64+
['query2', ['_value' => false], false],
65+
['query3', ['_value' => null], null], // Optional if you want to test null as well.
66+
];
67+
}
68+
69+
1770
public function setUp(): void
1871
{
1972
$this->ogm = new OGM();
@@ -136,20 +189,43 @@ public function testWithCartesian3DPoint(): void
136189

137190
public function testArray(): void
138191
{
139-
$arrayData = ['bob1', 'alicy'];
140-
141-
$this->assertEquals($arrayData, $this->ogm->map([
192+
$input = [
142193
'$type' => 'Array',
143-
'_value' => $arrayData,
144-
]));
194+
'_value' => [
195+
[
196+
[
197+
'$type' => 'String',
198+
'_value' => 'bob1',
199+
],
200+
[
201+
'$type' => 'String',
202+
'_value' => 'alicy',
203+
],
204+
],
205+
],
206+
];
207+
208+
$expectedOutput = [
209+
0 => [
210+
[
211+
'$type' => 'String',
212+
'_value' => 'bob1',
213+
],
214+
[
215+
'$type' => 'String',
216+
'_value' => 'alicy',
217+
],
218+
],
219+
];
220+
221+
$this->assertEquals($expectedOutput, $this->ogm->map($input));
145222
}
146223

224+
225+
147226
public function testMap(): void
148227
{
149-
$mapData = ['hello' =>
150-
[ '$type' => 'String',
151-
'_value' => 'hello',]
152-
];
228+
$mapData = ['hello' => 'hello'];
153229
$this->assertEquals(
154230
$mapData,
155231
$this->ogm->map([
@@ -302,5 +378,48 @@ public function testWithPath()
302378
$this->assertEquals('B', $path->getNodes()[1]->getProperties()['name']['_value']);
303379
}
304380

381+
#[DataProvider('integerDataProvider')] public function testWithInteger(string $query, array $parameters, int $expectedResult): void
382+
{
383+
$actual = $this->ogm->map([
384+
'$type' => 'Integer',
385+
'_value' => $parameters['age'],
386+
]);
387+
388+
$this->assertEquals($expectedResult, $actual);
389+
}
390+
391+
#[DataProvider('floatDataProvider')]
392+
public function testWithFloat(string $query, array $parameters, float $expectedResult): void
393+
{
394+
$actual = $this->ogm->map([
395+
'$type' => 'float',
396+
'_value' => $parameters['height'],
397+
]);
398+
399+
$this->assertEquals($expectedResult, $actual);
400+
}
401+
402+
403+
#[DataProvider('nullDataProvider')]
404+
public function testWithNull(string $query, array $parameters, ?string $expectedResult): void
405+
{
406+
$actual = $this->ogm->map([
407+
'$type' => 'Null',
408+
'_value' => null,
409+
]);
410+
$this->assertEquals($expectedResult, $actual);
411+
}
412+
413+
#[DataProvider('booleanDataProvider')]
414+
public function testWithBoolean(string $query, array $parameters, ?bool $expectedResult): void
415+
{
416+
$actual = $this->ogm->map([
417+
'$type' => 'Boolean',
418+
'_value' => $parameters['_value'],
419+
]);
420+
$this->assertEquals($expectedResult, $actual);
421+
}
422+
423+
305424

306425
}

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ private function createSubset(array $expected, array $actual): array
9696
return $subset;
9797
}
9898

99+
100+
101+
99102
public static function queryProvider(): array
100103
{
101104

0 commit comments

Comments
 (0)