Skip to content

Commit 57fdd32

Browse files
committed
test code for different datatypes
1 parent 7c8a5a6 commit 57fdd32

File tree

5 files changed

+383
-60
lines changed

5 files changed

+383
-60
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":1,"defects":{"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPropertyExistenceCheck":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNumericFilters":8,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNestedRelationships":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithMultipleConditions":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPartialNameMatch":7},"times":{"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#0":0.393,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithExactNames":0.368,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNoMatchingNames":1.426,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithSingleName":0.376,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNonExistentLabel":0.438,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPropertyExistenceCheck":0.417,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNumericFilters":0.378,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithSortingResults":0.371,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNestedRelationships":0.415,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithMultipleConditions":0.396,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithInvalidQuery":0.378,"Neo4j\\QueryAPI\\Tests\\Unit\\Neo4jQueryAPIUnitTest::testCorrectClientSetup":0.011,"Neo4j\\QueryAPI\\Tests\\Unit\\Neo4jQueryAPIUnitTest::testRunSuccess":0.005,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithEmptyNameList":0.363,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPartialNameMatch":0.359,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNoData":0.399}}
1+
{"version":1,"defects":{"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPropertyExistenceCheck":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNumericFilters":8,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNestedRelationships":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithMultipleConditions":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPartialNameMatch":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithExactNames":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithSingleName":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testNodeType":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testRelationshipType":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNoMatchingNames":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithExactNames":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testRelationshipType":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithNoMatchingNames":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithNull":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithArray":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithNumber":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithString":7,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithBoolean":7},"times":{"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#0":0.393,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithExactNames":0.11,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNoMatchingNames":0.081,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithSingleName":0.094,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNonExistentLabel":0.091,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPropertyExistenceCheck":0.417,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNumericFilters":0.378,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithSortingResults":0.371,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNestedRelationships":0.415,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithMultipleConditions":0.396,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithInvalidQuery":0.09,"Neo4j\\QueryAPI\\Tests\\Unit\\Neo4jQueryAPIUnitTest::testCorrectClientSetup":0.011,"Neo4j\\QueryAPI\\Tests\\Unit\\Neo4jQueryAPIUnitTest::testRunSuccess":0.005,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithEmptyNameList":0.094,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithPartialNameMatch":0.359,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithNoData":0.092,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testNodeType":0.096,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testRelationshipType":0.101,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryAPIIntegrationTest::testRunSuccessWithParameters#testWithString":0.086,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithExactNames":1.527,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithSingleName":0.112,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testRelationshipType":0.137,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithNoMatchingNames":0.102,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithString":0.091,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithNumber":0.087,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithBoolean":0.091,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithNull":0.098,"Neo4j\\QueryAPI\\Tests\\Integration\\Neo4jQueryintegrationtest::testRunSuccessWithParameters#testWithArray":0.09}}

src/Neo4jQueryAPI.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public static function login(string $address, string $username, string $password
2525
'timeout' => 10.0,
2626
'headers' => [
2727
'Authorization' => 'Basic ' . base64_encode("$username:$password"),
28-
'Content-Type' => 'application/json',
28+
'Content-Type' => 'application/vnd.neo4j.query',
29+
'Accept'=>'application/vnd.neo4j.query',
2930
],
3031
]);
3132

@@ -57,12 +58,36 @@ private function normalizeResults(array $results): array
5758
$values = $results['data']['values'];
5859

5960
$normalizedData = array_map(function ($row) use ($fields) {
60-
return ['row' => array_combine($fields, $row)];
61+
return [
62+
'row' => array_map(function ($value) {
63+
// Check if the value is an object that represents a Neo4j value and extract its value
64+
if (is_array($value) && isset($value['_value'])) {
65+
return $value['_value']; // Extract the actual value if it's a Neo4j object
66+
}
67+
return $value; // Return as is if it's already a primitive value
68+
}, array_combine($fields, $row)),
69+
];
6170
}, $values);
6271

6372
return ['data' => $normalizedData];
6473
}
6574

6675
return $results; // Return unchanged if no transformation is needed
6776
}
77+
78+
/* private function normalizeResults(array $results): array
79+
{
80+
if (isset($results['data']['fields']) && isset($results['data']['values'])) {
81+
$fields = $results['data']['fields'];
82+
$values = $results['data']['values'];
83+
84+
$normalizedData = array_map(function ($row) use ($fields) {
85+
return ['row' => array_combine($fields, $row)];
86+
}, $values);
87+
88+
return ['data' => $normalizedData];
89+
}
90+
91+
return $results; // Return unchanged if no transformation is needed
92+
}*/
6893
}

src/run_query.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Neo4j\QueryAPI\Neo4jQueryAPI;
4+
use GuzzleHttp\Exception\RequestException;
5+
6+
require __DIR__ . '/../vendor/autoload.php';
7+
8+
try {
9+
// Login to the Neo4j instance
10+
$api = Neo4jQueryAPI::login(
11+
'https://bb79fe35.databases.neo4j.io', // Replace with your Neo4j instance URL
12+
'neo4j', // Replace with your Neo4j username
13+
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0' // Replace with your Neo4j password
14+
15+
);
16+
17+
// Define a Cypher query
18+
$query = 'MATCH (n:Person) RETURN n LIMIT 10';
19+
20+
// Fetch results in plain JSON format
21+
$plainResults = $api->run($query, [], 'neo4j', false);
22+
echo "Plain JSON Results:\n";
23+
echo "<pre>";
24+
print_r($plainResults);
25+
echo "</pre>";
26+
27+
// Fetch results in Neo4j-extended JSON format
28+
$extendedResults = $api->run($query, [], 'neo4j', true);
29+
echo "Extended JSON Results:\n";
30+
echo "<pre>";
31+
print_r($extendedResults);
32+
echo "</pre>";
33+
34+
} catch (RequestException $e) {
35+
echo "Request Error: " . $e->getMessage();
36+
} catch (RuntimeException $e) {
37+
echo "Runtime Error: " . $e->getMessage();
38+
} catch (Exception $e) {
39+
echo "General Error: " . $e->getMessage();
40+
}
41+

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 96 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@
99

1010
class Neo4jQueryAPIIntegrationTest extends TestCase
1111
{
12+
private static ?Neo4jQueryAPI $api = null;
13+
1214
/**
1315
* @throws GuzzleException
1416
*/
1517
public static function setUpBeforeClass(): void
1618
{
17-
$api = Neo4jQueryAPI::login(
19+
self::$api = Neo4jQueryAPI::login(
1820
getenv('NEO4J_ADDRESS'),
1921
getenv('NEO4J_USERNAME'),
2022
getenv('NEO4J_PASSWORD')
2123
);
2224

2325
// Clear the database
24-
self::clearDatabase($api);
26+
self::clearDatabase(self::$api);
2527

2628
// Create necessary constraints
27-
self::createConstraints($api);
29+
self::createConstraints(self::$api);
2830

2931
// Insert test data
30-
self::populateFixtures($api, ['bob1', 'alicy']);
32+
self::populateFixtures(self::$api, ['bob1', 'alicy']);
3133

3234
// Validate fixtures
33-
self::validateFixtures($api);
35+
self::validateFixtures(self::$api);
3436
}
3537

3638
/**
@@ -81,8 +83,10 @@ public function testRunSuccessWithParameters(
8183
array $expectedResults
8284
): void
8385
{
84-
$api = Neo4jQueryAPI::login($address, $username, $password);
85-
$results = $api->run($query, $parameters);
86+
$results = $this->executeQuery($query, $parameters);
87+
88+
// Normalize the results before assertion
89+
$results = $this->normalizeResults($results);
8690

8791
// Remove bookmarks if present
8892
unset($results['bookmarks']);
@@ -91,6 +95,63 @@ public function testRunSuccessWithParameters(
9195
$this->assertEquals($expectedResults, $results);
9296
}
9397

98+
/**
99+
* Executes the query using the Neo4j API.
100+
*
101+
* @throws GuzzleException
102+
*/
103+
private function executeQuery(string $query, array $parameters): array
104+
{
105+
// Check if the API connection is initialized
106+
if (self::$api === null) {
107+
throw new \Exception('API connection is not initialized.');
108+
}
109+
110+
// Execute the query
111+
$response = self::$api->run($query, $parameters);
112+
113+
// Check if the response contains any error
114+
if (isset($response['errors']) && !empty($response['errors'])) {
115+
throw new \Exception('Error executing query: ' . json_encode($response['errors']));
116+
}
117+
118+
return $response;
119+
}
120+
121+
/**
122+
* Normalize the Neo4j results to match the expected format.
123+
*/
124+
private function normalizeResults(array $results): array
125+
{
126+
// Check if the results contain 'fields' and 'values'
127+
if (isset($results['data']) && is_array($results['data'])) {
128+
// Normalize data into 'fields' and 'values' format
129+
$fields = [];
130+
$values = [];
131+
132+
foreach ($results['data'] as $row) {
133+
if (isset($row['row']) && is_array($row['row'])) {
134+
// Ensure each row's field-value pairs are added to fields and values
135+
foreach ($row['row'] as $key => $value) {
136+
$fields[] = $key; // Add field name (e.g., 'n.name')
137+
$values[] = [$value]; // Add the corresponding value in an array for each row
138+
}
139+
}
140+
}
141+
142+
return [
143+
'data' => [
144+
[
145+
'fields' => $fields,
146+
'values' => $values,
147+
],
148+
],
149+
];
150+
}
151+
152+
return $results; // Return unchanged if no transformation is needed
153+
}
154+
94155
public static function queryProvider(): array
95156
{
96157
return [
@@ -103,24 +164,10 @@ public static function queryProvider(): array
103164
['names' => ['bob1', 'alicy']],
104165
[
105166
'data' => [
106-
['row' => ['n.name' => 'bob1']],
107-
['row' => ['n.name' => 'alicy']],
167+
['fields' => ['n.name'], 'values' => [['bob1'], ['alicy']]],
108168
],
109169
],
110170
],
111-
112-
// Test with no matching names
113-
'testWithNoMatchingNames' => [
114-
'https://bb79fe35.databases.neo4j.io',
115-
'neo4j',
116-
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0',
117-
'MATCH (n:Person) WHERE n.name IN $names RETURN n.name',
118-
['names' => ['charlie', 'david']],
119-
[
120-
'data' => [],
121-
],
122-
],
123-
124171
// Test with a single name
125172
'testWithSingleName' => [
126173
'https://bb79fe35.databases.neo4j.io',
@@ -130,59 +177,51 @@ public static function queryProvider(): array
130177
['name' => 'bob1'],
131178
[
132179
'data' => [
133-
['row' => ['n.name' => 'bob1']],
180+
['fields' => ['n.name'], 'values' => [['bob1']]],
134181
],
135182
],
136183
],
137-
138-
// Test for non-existent label
139-
'testWithNonExistentLabel' => [
140-
'https://bb79fe35.databases.neo4j.io',
184+
// Test for relationship data type
185+
'testRelationshipType' => [
186+
'https://your-neo4j-instance',
141187
'neo4j',
142-
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0',
143-
'MATCH (n:NonExistentLabel) RETURN n',
144-
[],
188+
'your-password',
189+
'MATCH (a:Person {name: $name1}), (b:Person {name: $name2}) CREATE (a)-[r:FRIENDS_WITH]->(b) RETURN r',
190+
['name1' => 'Alice', 'name2' => 'Bob'],
145191
[
146-
'data' => [],
192+
'data' => [
193+
['fields' => ['r'], 'values' => [[
194+
'_type' => 'FRIENDS_WITH',
195+
'_properties' => [],
196+
]]],
197+
],
147198
],
148199
],
149-
150-
// Test with an empty name list
151-
'testWithEmptyNameList' => [
200+
// Test with no matching names
201+
'testWithNoMatchingNames' => [
152202
'https://bb79fe35.databases.neo4j.io',
153203
'neo4j',
154204
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0',
155205
'MATCH (n:Person) WHERE n.name IN $names RETURN n.name',
156-
['names' => []],
157-
[
158-
'data' => [],
159-
],
160-
],
161-
162-
// Test for no data
163-
'testWithNoData' => [
164-
'https://bb79fe35.databases.neo4j.io',
165-
'neo4j',
166-
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0',
167-
'MATCH (n:Person) WHERE n.age > 100 RETURN n.name',
168-
[],
206+
['names' => ['charlie', 'david']],
169207
[
170208
'data' => [],
171209
],
172210
],
173-
174-
175-
// Test with an invalid query that should return an error
176-
'testWithInvalidQuery' => [
177-
'https://bb79fe35.databases.neo4j.io',
211+
// Additional test cases as needed...
212+
// Test with string data type
213+
'testWithString' => [
214+
'https://your-neo4j-instance',
178215
'neo4j',
179-
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0',
180-
'MATCH (n:Person) WHERE n.nonexistentProperty = $value RETURN n.name',
181-
['value' => 'someValue'],
216+
'your-password',
217+
'CREATE (n:Person {name: $name}) RETURN n.name',
218+
['name' => 'Alice'],
182219
[
183-
'data' => [],
220+
'data' => [
221+
['fields' => ['n.name'], 'values' => [['Alice']]],
222+
],
184223
],
185-
],
224+
]
186225
];
187226
}
188227
}

0 commit comments

Comments
 (0)