Skip to content

Commit 1e35cbb

Browse files
committed
Query Profile code
1 parent d76b13e commit 1e35cbb

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
vendor
33
phpunit.xml
44
test
5-
.phpunit.result.cache
5+
.phpunit.result.cache

src/Profile.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI;
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
7+
use GuzzleHttp\Client;
8+
9+
class Profile
10+
{
11+
private $neo4jUrl;
12+
private $username;
13+
private $password;
14+
private $client;
15+
16+
public function __construct($url, $username, $password)
17+
{
18+
$this->neo4jUrl = $url;
19+
$this->username = $username;
20+
$this->password = $password;
21+
$this->client = new Client();
22+
}
23+
24+
public function executeQuery($query)
25+
{
26+
$response = $this->client->post($this->neo4jUrl, [
27+
'auth' => [$this->username, $this->password],
28+
'json' => [
29+
'statement' => $query
30+
]
31+
]);
32+
33+
return json_decode($response->getBody(), true);
34+
}
35+
36+
public function formatResponse($data): array
37+
{
38+
$output = [
39+
"data" => [
40+
"fields" => [],
41+
"values" => []
42+
],
43+
"profiledQueryPlan" => [],
44+
"bookmarks" => $data['bookmarks'] ?? []
45+
];
46+
47+
if (isset($data['result']['columns']) && isset($data['result']['rows'])) {
48+
$output["data"]["fields"] = $data['result']['columns'];
49+
foreach ($data['result']['rows'] as $row) {
50+
$output["data"]["values"][] = $row;
51+
}
52+
}
53+
54+
if (isset($data['profiledQueryPlan'])) {
55+
$output["profiledQueryPlan"] = $data['profiledQueryPlan'];
56+
}
57+
58+
return $output;
59+
}
60+
}
61+

src/Query_Profile_run.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
use Neo4j\QueryAPI\Profile;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$neo4jUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2';
8+
$username = 'neo4j';
9+
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
10+
11+
$client = new Profile($neo4jUrl, $username, $password);
12+
13+
$query = "PROFILE MATCH (n:Person) RETURN n";
14+
$data = $client->executeQuery($query);
15+
$formattedResponse = $client->formatResponse($data);
16+
17+
echo json_encode($formattedResponse, JSON_PRETTY_PRINT);
18+

tests/Integration/ProfileTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Integration;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\HandlerStack;
8+
use GuzzleHttp\Psr7\Response;
9+
use Neo4j\QueryAPI\Profile;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class ProfileTest extends TestCase
13+
{
14+
public function testExecuteQuery(): void
15+
{
16+
$mockResponseData = [
17+
'result' => [
18+
'columns' => ['name'],
19+
'rows' => [['John Doe']],
20+
],
21+
'bookmarks' => ['bookmark1'],
22+
];
23+
24+
25+
$mock = new MockHandler([
26+
new Response(200, [], json_encode($mockResponseData))
27+
]);
28+
$handlerStack = HandlerStack::create($mock);
29+
30+
$mockClient = new Client(['handler' => $handlerStack]);
31+
32+
$profile = new Profile('http://mock-neo4j-url', 'user', 'password');
33+
$reflection = new \ReflectionClass(Profile::class);
34+
$clientProperty = $reflection->getProperty('client');
35+
$clientProperty->setValue($profile, $mockClient);
36+
37+
$query = 'MATCH (n:Person) RETURN n.name';
38+
$result = $profile->executeQuery($query);
39+
40+
$this->assertIsArray($result);
41+
$this->assertEquals($mockResponseData, $result);
42+
}
43+
44+
public function testFormatResponse(): void
45+
{
46+
$mockInputData = [
47+
'result' => [
48+
'columns' => ['name'],
49+
'rows' => [['John Doe']],
50+
],
51+
'profiledQueryPlan' => [
52+
'plan' => 'Mock Plan',
53+
],
54+
'bookmarks' => ['bookmark1'],
55+
];
56+
57+
$expectedOutput = [
58+
'data' => [
59+
'fields' => ['name'],
60+
'values' => [['John Doe']],
61+
],
62+
'profiledQueryPlan' => [
63+
'plan' => 'Mock Plan',
64+
],
65+
'bookmarks' => ['bookmark1'],
66+
];
67+
68+
$profile = new Profile('http://mock-neo4j-url', 'user', 'password');
69+
70+
$formattedResponse = $profile->formatResponse($mockInputData);
71+
$this->assertEquals($expectedOutput, $formattedResponse);
72+
}
73+
}

0 commit comments

Comments
 (0)