99
1010class 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