Skip to content

Commit 7fc986e

Browse files
committed
accessmode parameter is added, tests are created
1 parent 6a423d9 commit 7fc986e

File tree

3 files changed

+114
-8
lines changed

3 files changed

+114
-8
lines changed

src/Neo4jQueryAPI.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Client;
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\Exception\RequestException;
9+
use InvalidArgumentException;
910
use Neo4j\QueryAPI\Objects\ChildQueryPlan;
1011
use Neo4j\QueryAPI\Objects\QueryArguments;
1112
use Neo4j\QueryAPI\Objects\ResultCounters;
@@ -52,23 +53,40 @@ public static function login(string $address, string $username, string $password
5253
* @throws RequestExceptionInterface
5354
* @api
5455
*/
55-
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null): ResultSet
56+
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null, ?string $impersonatedUser = null, string $accessMode = 'WRITE' ): ResultSet
5657
{
58+
$validAccessModes = ['READ', 'WRITE', 'ROUTE'];
59+
if (!in_array(strtoupper($accessMode), $validAccessModes, true)) {
60+
throw new InvalidArgumentException("Invalid access mode: $accessMode. Allowed values are 'READ', 'WRITE', or 'ROUTE'.");
61+
}
5762
try {
5863
$payload = [
5964
'statement' => $cypher,
6065
'parameters' => empty($parameters) ? new stdClass() : $parameters,
6166
'includeCounters' => true,
67+
'routing' => strtoupper($accessMode)
6268
];
69+
error_log("Request Payload: " . json_encode($payload));
6370

6471
if ($bookmark !== null) {
6572
$payload['bookmarks'] = $bookmark->getBookmarks();
6673
}
74+
if ($impersonatedUser !== null) {
75+
$payload['impersonatedUser'] = $impersonatedUser;
76+
}
77+
78+
//
79+
6780

6881
$response = $this->client->post('/db/' . $database . '/query/v2', [
6982
'json' => $payload,
83+
7084
]);
7185

86+
// if ($response->getStatusCode() !== 200) {
87+
// throw new Neo4jException("Failed to run query: " . $response->getReasonPhrase());
88+
// }
89+
7290
$data = json_decode($response->getBody()->getContents(), true);
7391
$ogm = new OGM();
7492

@@ -111,16 +129,21 @@ public function run(string $cypher, array $parameters = [], string $database = '
111129
$profile
112130
);
113131
} catch (RequestExceptionInterface $e) {
132+
error_log("Request Exception: " . $e->getMessage());
133+
114134
$response = $e->getResponse();
115135
if ($response !== null) {
116136
$contents = $response->getBody()->getContents();
117-
$errorResponse = json_decode($contents, true);
137+
error_log("Error Response: " . $contents);
118138

119-
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
120-
}
139+
$errorResponse = json_decode($contents, true);
140+
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
121141
}
122-
throw new RuntimeException('Error executing query: ' . $e->getMessage(), 0, $e);
142+
143+
throw $e;
123144
}
145+
}
146+
124147

125148
/**
126149
* @api

src/Results/ResultSet.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class ResultSet implements IteratorAggregate, Countable
2222
* @param list<ResultRow> $rows
2323
*/
2424
public function __construct(
25-
private readonly array $rows,
26-
private ResultCounters $counters,
27-
private Bookmarks $bookmarks,
25+
private readonly array $rows,
26+
private ResultCounters $counters,
27+
private Bookmarks $bookmarks,
2828
private ?ProfiledQueryPlan $profiledQueryPlan = null
2929
)
3030
{
@@ -47,6 +47,7 @@ public function getQueryCounters(): ?ResultCounters
4747
{
4848
return $this->counters;
4949
}
50+
5051
/**
5152
* @api
5253
*/
@@ -71,4 +72,24 @@ public function getBookmarks(): ?Bookmarks
7172
{
7273
return $this->bookmarks;
7374
}
75+
76+
/**
77+
* @api
78+
*/
79+
80+
public function testAccessMode()
81+
{
82+
$resultSet = new ResultSet([], null, 'WRITE');
83+
$this->assertEquals('WRITE', $resultSet->getAccessMode());
84+
}
85+
86+
87+
// public function getImpersonatedUser(): ?ImpersonatedUser
88+
// {
89+
//
90+
// }
91+
92+
93+
94+
7495
}

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPUnit\Framework\Attributes\DataProvider;
1414
use PHPUnit\Framework\TestCase;
1515
use Neo4j\QueryAPI\Transaction;
16+
use Psr\Http\Message\ResponseInterface;
1617

1718
class Neo4jQueryAPIIntegrationTest extends TestCase
1819
{
@@ -193,6 +194,67 @@ public function testChildQueryPlanExistence(): void
193194
$this->assertInstanceOf(ProfiledQueryPlan::class, $child);
194195
}
195196
}
197+
public function testImpersonatedUserSuccess(): void
198+
{
199+
200+
$result = $this->api->run(
201+
"PROFILE MATCH (n:Person {name: 'Alice'}) RETURN n.name",
202+
[],
203+
'neo4j',
204+
null,
205+
'HAPPYBDAY'
206+
);
207+
208+
209+
$impersonatedUser = $result->getImpersonatedUser();
210+
$this->assertNotNull($impersonatedUser, "Impersonated user should not be null.");
211+
212+
//A user being impersonated (ImpersonatedUser) might have other users who are being impersonated through it, forming a chain or hierarchy.
213+
214+
}
215+
216+
217+
public function testImpersonatedUserFailure(): void
218+
{
219+
$this->expectException(Neo4jException::class);
220+
221+
222+
$this->api->run(
223+
"PROFILE MATCH (n:Person {name: 'Alice'}) RETURN n.name",
224+
[],
225+
'neo4j',
226+
null,
227+
'invalidUser'
228+
);
229+
}
230+
231+
//
232+
233+
public function testRunWithDefaultAccessMode(): void
234+
{
235+
$result = $this->api->run("MATCH (n) RETURN COUNT(n)");
236+
237+
$this->assertInstanceOf(ResultSet::class, $result);
238+
// Default mode is WRITE;
239+
}
240+
241+
public function testRunWithAccessMode(): void
242+
{
243+
$result = $this->api->run(
244+
"MATCH (n) RETURN COUNT(n)",
245+
[],
246+
'neo4j',
247+
null,
248+
'READ'
249+
);
250+
251+
}
252+
253+
254+
255+
256+
257+
196258

197259
public function testTransactionCommit(): void
198260
{

0 commit comments

Comments
 (0)