Skip to content

Commit 7992fc3

Browse files
committed
test cases are fixed
1 parent bb8b938 commit 7992fc3

File tree

6 files changed

+806
-302
lines changed

6 files changed

+806
-302
lines changed

src/Neo4jQueryAPI.php

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
use Psr\Http\Client\RequestExceptionInterface;
1414
use RuntimeException;
1515
use stdClass;
16+
use Neo4j\QueryAPI\Objects\Bookmarks;
1617

1718
class Neo4jQueryAPI
1819
{
20+
1921
private Client $client;
2022

2123
public function __construct(Client $client)
@@ -25,6 +27,10 @@ public function __construct(Client $client)
2527

2628
public static function login(string $address, string $username, string $password): self
2729
{
30+
$username = 'neo4j';
31+
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
32+
$connectionUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2';
33+
2834

2935
$client = new Client([
3036
'base_uri' => rtrim($address, '/'),
@@ -43,22 +49,23 @@ public static function login(string $address, string $username, string $password
4349
* @throws Neo4jException
4450
* @throws RequestExceptionInterface
4551
*/
46-
public function run(string $cypher, array $parameters = [], string $database = 'neo4j'): ResultSet
52+
public function run(string $cypher, array $parameters = [], string $database = 'neo4j', Bookmarks $bookmark = null): ResultSet
4753
{
4854
try {
49-
// Prepare the payload for the request
5055
$payload = [
5156
'statement' => $cypher,
5257
'parameters' => empty($parameters) ? new stdClass() : $parameters,
53-
'includeCounters' => true
58+
'includeCounters' => true,
5459
];
5560

56-
// Execute the request to the Neo4j server
61+
if ($bookmark !== null) {
62+
$payload['bookmarks'] = $bookmark->getBookmarks();
63+
}
64+
5765
$response = $this->client->post('/db/' . $database . '/query/v2', [
5866
'json' => $payload,
5967
]);
6068

61-
// Decode the response body
6269
$data = json_decode($response->getBody()->getContents(), true);
6370
$ogm = new OGM();
6471

@@ -73,35 +80,47 @@ public function run(string $cypher, array $parameters = [], string $database = '
7380
return new ResultRow($data);
7481
}, $values);
7582

76-
return new ResultSet($rows, new ResultCounters(
77-
containsUpdates: $data['counters']['containsUpdates'],
78-
nodesCreated: $data['counters']['nodesCreated'],
79-
nodesDeleted: $data['counters']['nodesDeleted'],
80-
propertiesSet: $data['counters']['propertiesSet'],
81-
relationshipsCreated: $data['counters']['relationshipsCreated'],
82-
relationshipsDeleted: $data['counters']['relationshipsDeleted'],
83-
labelsAdded: $data['counters']['labelsAdded'],
84-
labelsRemoved: $data['counters']['labelsRemoved'],
85-
indexesAdded: $data['counters']['indexesAdded'],
86-
indexesRemoved: $data['counters']['indexesRemoved'],
87-
constraintsAdded: $data['counters']['constraintsAdded'],
88-
constraintsRemoved: $data['counters']['constraintsRemoved'],
89-
containsSystemUpdates: $data['counters']['containsSystemUpdates'],
90-
systemUpdates: $data['counters']['systemUpdates']
91-
));
92-
} catch (RequestExceptionInterface $e) {
93-
$response = $e->getResponse();
94-
if ($response !== null) {
95-
$contents = $response->getBody()->getContents();
96-
$errorResponse = json_decode($contents, true);
97-
98-
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
99-
}
83+
$resultCounters = new ResultCounters(
84+
containsUpdates: $data['counters']['containsUpdates'] ?? false,
85+
nodesCreated: $data['counters']['nodesCreated'] ?? 0,
86+
nodesDeleted: $data['counters']['nodesDeleted'] ?? 0,
87+
propertiesSet: $data['counters']['propertiesSet'] ?? 0,
88+
relationshipsCreated: $data['counters']['relationshipsCreated'] ?? 0,
89+
relationshipsDeleted: $data['counters']['relationshipsDeleted'] ?? 0,
90+
labelsAdded: $data['counters']['labelsAdded'] ?? 0,
91+
labelsRemoved: $data['counters']['labelsRemoved'] ?? 0,
92+
indexesAdded: $data['counters']['indexesAdded'] ?? 0,
93+
indexesRemoved: $data['counters']['indexesRemoved'] ?? 0,
94+
constraintsAdded: $data['counters']['constraintsAdded'] ?? 0,
95+
constraintsRemoved: $data['counters']['constraintsRemoved'] ?? 0,
96+
containsSystemUpdates: $data['counters']['containsSystemUpdates'] ?? false,
97+
systemUpdates: $data['counters']['systemUpdates'] ?? 0
98+
);
99+
100+
$resultSet = new ResultSet($rows, $resultCounters, new Bookmarks($data['bookmarks'] ?? []));
101+
102+
103+
return $resultSet;
104+
105+
} catch (RequestException $e) {
106+
{
107+
$response = $e->getResponse();
108+
if ($response !== null) {
109+
$contents = $response->getBody()->getContents();
110+
$errorResponse = json_decode($contents, true);
111+
112+
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
113+
}
100114

101-
throw $e;
115+
throw $e;
116+
}
117+
throw new RuntimeException('Error executing query: ' . $e->getMessage(), 0, $e);
102118
}
103119
}
104120

121+
122+
123+
105124
public function beginTransaction(string $database = 'neo4j'): Transaction
106125
{
107126
$response = $this->client->post("/db/neo4j/query/v2/tx");

src/Objects/Bookmarks.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Objects;
4+
5+
class Bookmarks implements \Countable
6+
{
7+
public function __construct(private array $bookmarks)
8+
{
9+
}
10+
11+
public function addBookmarks(?Bookmarks $newBookmarks): void
12+
{
13+
if ($newBookmarks !== null) {
14+
$this->bookmarks = array_unique(array_merge($this->bookmarks, $newBookmarks->bookmarks));
15+
}
16+
}
17+
18+
19+
public function getBookmarks(): array
20+
{
21+
return $this->bookmarks;
22+
}
23+
24+
public function count(): int
25+
{
26+
return count($this->bookmarks);
27+
}
28+
}

src/Objects/ResultCounters.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
class ResultCounters
66
{
77
public function __construct(
8-
private readonly bool $containsUpdates,
9-
private readonly int $nodesCreated,
10-
private readonly int $nodesDeleted,
11-
private readonly int $propertiesSet,
12-
private readonly int $relationshipsCreated,
13-
private readonly int $relationshipsDeleted,
14-
private readonly int $labelsAdded,
15-
private readonly int $labelsRemoved,
16-
private readonly int $indexesAdded,
17-
private readonly int $indexesRemoved,
18-
private readonly int $constraintsAdded,
19-
private readonly int $constraintsRemoved,
20-
private readonly bool $containsSystemUpdates,
21-
private readonly int $systemUpdates
8+
private readonly bool $containsUpdates = false,
9+
private readonly int $nodesCreated = 0,
10+
private readonly int $nodesDeleted = 0,
11+
private readonly int $propertiesSet = 0,
12+
private readonly int $relationshipsCreated = 0,
13+
private readonly int $relationshipsDeleted = 0,
14+
private readonly int $labelsAdded = 0,
15+
private readonly int $labelsRemoved = 0,
16+
private readonly int $indexesAdded = 0,
17+
private readonly int $indexesRemoved = 0,
18+
private readonly int $constraintsAdded = 0,
19+
private readonly int $constraintsRemoved = 0,
20+
private readonly bool $containsSystemUpdates = false,
21+
private readonly int $systemUpdates = 0
2222
) {
2323
}
2424

@@ -92,23 +92,6 @@ public function getLabelsRemoved(): int
9292
{
9393
return $this->labelsRemoved;
9494
}
95-
public function getBookmarks(): array
96-
{
97-
return $this->bookmarks;
98-
}
99-
100-
public function addBookmark(string $bookmark): void
101-
{
102-
if (!in_array($bookmark, $this->bookmarks)) {
103-
$this->bookmarks[] = $bookmark;
104-
}
105-
}
106-
107-
// public function clearBookmarks(): void
108-
// {
109-
// $this->bookmarks = [];
110-
// }
111-
11295
}
11396

11497

src/Results/ResultSet.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
<?php
2+
23
namespace Neo4j\QueryAPI\Results;
34

45
use ArrayIterator;
56
use Countable;
67
use IteratorAggregate;
78
use Neo4j\QueryAPI\Objects\ResultCounters;
9+
use Neo4j\QueryAPI\Objects\Bookmarks; // Make sure to include the Bookmarks class
810
use Traversable;
911

1012
class ResultSet implements IteratorAggregate, Countable
1113
{
1214
/**
1315
* @param list<ResultRow> $rows
1416
*/
15-
public function __construct(private readonly array $rows,private ResultCounters $counters)
17+
public function __construct(
18+
private readonly array $rows,
19+
private ResultCounters $counters,
20+
private Bookmarks $bookmarks
21+
)
1622
{
1723
}
1824

1925
public function getIterator(): Traversable
2026
{
2127
return new ArrayIterator($this->rows);
2228
}
29+
2330
public function getQueryCounters(): ?ResultCounters
2431
{
2532
return $this->counters;
@@ -30,5 +37,8 @@ public function count(): int
3037
return count($this->rows);
3138
}
3239

33-
40+
public function getBookmarks(): ?Bookmarks
41+
{
42+
return $this->bookmarks;
43+
}
3444
}

src/neo4jQuery.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
require '../vendor/autoload.php';
4+
use GuzzleHttp\Client;
5+
use Neo4j\QueryAPI\Objects\ResultCounters;
6+
7+
$username = 'neo4j';
8+
$password = '9lWmptqBgxBOz8NVcTJjgs3cHPyYmsy63ui6Spmw1d0';
9+
$connectionUrl = 'https://6f72daa1.databases.neo4j.io/db/neo4j/query/v2';
10+
11+
$auth = base64_encode("$username:$password");
12+
13+
$client = new Client();
14+
15+
$resultCounters = new ResultCounters(
16+
containsUpdates: false,
17+
nodesCreated: 0,
18+
nodesDeleted: 0,
19+
propertiesSet: 0,
20+
relationshipsCreated: 0,
21+
relationshipsDeleted: 0,
22+
labelsAdded: 0,
23+
labelsRemoved: 0,
24+
indexesAdded: 0,
25+
indexesRemoved: 0,
26+
constraintsAdded: 0,
27+
constraintsRemoved: 0,
28+
containsSystemUpdates: false,
29+
systemUpdates: 0
30+
);
31+
32+
$response = $client->post($connectionUrl, [
33+
'json' => [
34+
'statement' => 'CREATE (n:Person {name: $name}) RETURN n.name AS name',
35+
'parameters' => [
36+
'name' => 'Alice'
37+
],
38+
'includeCounters' => true,
39+
'bookmarks' => $resultCounters->getBookmarks(),
40+
],
41+
'headers' => [
42+
'Authorization' => 'Basic ' . $auth,
43+
'Content-Type' => 'application/json',
44+
'Accept' => 'application/json',
45+
],
46+
]);
47+
48+
$data = json_decode($response->getBody()->getContents(), true);
49+
50+
foreach ($data['counters'] as $key => $value) {
51+
$resultCounters->setCounter($key, $value);
52+
}
53+
54+
if (isset($data['bookmark'])) {
55+
$resultCounters->addBookmark($data['bookmark']);
56+
}

0 commit comments

Comments
 (0)