9
9
10
10
class Neo4jQueryAPIIntegrationTest extends TestCase
11
11
{
12
+ private static ?Neo4jQueryAPI $ api = null ;
13
+
12
14
/**
13
15
* @throws GuzzleException
14
16
*/
15
17
public static function setUpBeforeClass (): void
16
18
{
17
- $ api = Neo4jQueryAPI::login (
19
+ self :: $ api = Neo4jQueryAPI::login (
18
20
getenv ('NEO4J_ADDRESS ' ),
19
21
getenv ('NEO4J_USERNAME ' ),
20
22
getenv ('NEO4J_PASSWORD ' )
21
23
);
22
24
23
25
// Clear the database
24
- self ::clearDatabase ($ api );
26
+ self ::clearDatabase (self :: $ api );
25
27
26
28
// Create necessary constraints
27
- self ::createConstraints ($ api );
29
+ self ::createConstraints (self :: $ api );
28
30
29
31
// Insert test data
30
- self ::populateFixtures ($ api , ['bob1 ' , 'alicy ' ]);
32
+ self ::populateFixtures (self :: $ api , ['bob1 ' , 'alicy ' ]);
31
33
32
34
// Validate fixtures
33
- self ::validateFixtures ($ api );
35
+ self ::validateFixtures (self :: $ api );
34
36
}
35
37
36
38
/**
@@ -81,8 +83,10 @@ public function testRunSuccessWithParameters(
81
83
array $ expectedResults
82
84
): void
83
85
{
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 );
86
90
87
91
// Remove bookmarks if present
88
92
unset($ results ['bookmarks ' ]);
@@ -91,6 +95,63 @@ public function testRunSuccessWithParameters(
91
95
$ this ->assertEquals ($ expectedResults , $ results );
92
96
}
93
97
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
+
94
155
public static function queryProvider (): array
95
156
{
96
157
return [
@@ -103,24 +164,10 @@ public static function queryProvider(): array
103
164
['names ' => ['bob1 ' , 'alicy ' ]],
104
165
[
105
166
'data ' => [
106
- ['row ' => ['n.name ' => 'bob1 ' ]],
107
- ['row ' => ['n.name ' => 'alicy ' ]],
167
+ ['fields ' => ['n.name ' ], 'values ' => [['bob1 ' ], ['alicy ' ]]],
108
168
],
109
169
],
110
170
],
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
-
124
171
// Test with a single name
125
172
'testWithSingleName ' => [
126
173
'https://bb79fe35.databases.neo4j.io ' ,
@@ -130,59 +177,51 @@ public static function queryProvider(): array
130
177
['name ' => 'bob1 ' ],
131
178
[
132
179
'data ' => [
133
- ['row ' => ['n.name ' => 'bob1 ' ]],
180
+ ['fields ' => ['n.name ' ], ' values ' => [[ 'bob1 ' ] ]],
134
181
],
135
182
],
136
183
],
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 ' ,
141
187
'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 ' ],
145
191
[
146
- 'data ' => [],
192
+ 'data ' => [
193
+ ['fields ' => ['r ' ], 'values ' => [[
194
+ '_type ' => 'FRIENDS_WITH ' ,
195
+ '_properties ' => [],
196
+ ]]],
197
+ ],
147
198
],
148
199
],
149
-
150
- // Test with an empty name list
151
- 'testWithEmptyNameList ' => [
200
+ // Test with no matching names
201
+ 'testWithNoMatchingNames ' => [
152
202
'https://bb79fe35.databases.neo4j.io ' ,
153
203
'neo4j ' ,
154
204
'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0 ' ,
155
205
'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 ' ]],
169
207
[
170
208
'data ' => [],
171
209
],
172
210
],
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 ' ,
178
215
'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 ' ],
182
219
[
183
- 'data ' => [],
220
+ 'data ' => [
221
+ ['fields ' => ['n.name ' ], 'values ' => [['Alice ' ]]],
222
+ ],
184
223
],
185
- ],
224
+ ]
186
225
];
187
226
}
188
227
}
0 commit comments