Skip to content

Commit 0691aa8

Browse files
committed
Merge remote-tracking branch 'origin/main' into integration-test-cleanup-1976649542
2 parents 9bfc0fa + 61c9476 commit 0691aa8

File tree

4 files changed

+115
-14
lines changed

4 files changed

+115
-14
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Nagels IT
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/Configuration.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,30 @@
22

33
namespace Neo4j\QueryAPI;
44

5+
use InvalidArgumentException;
56
use Neo4j\QueryAPI\Objects\Bookmarks;
67
use Neo4j\QueryAPI\Enums\AccessMode;
78

89
final class Configuration
910
{
11+
/**
12+
* Constructor for Configuration class.
13+
*
14+
* @param string $baseUri The base URI for the Neo4j instance.
15+
* @param string $database The name of the database to connect to.
16+
* @param bool $includeCounters Whether to include counters in the response.
17+
* @param Bookmarks $bookmarks Bookmarks for tracking queries.
18+
* @param AccessMode $accessMode The access mode for the connection (read/write).
19+
*
20+
* @throws InvalidArgumentException if $baseUri is empty.
21+
*/
1022
public function __construct(
1123
public readonly string $baseUri,
1224
public readonly string $database = 'neo4j',
1325
public readonly bool $includeCounters = true,
1426
public readonly Bookmarks $bookmarks = new Bookmarks([]),
1527
public readonly AccessMode $accessMode = AccessMode::WRITE,
1628
) {
29+
1730
}
1831
}

src/Neo4jQueryAPI.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Http\Discovery\Psr17FactoryDiscovery;
66
use Http\Discovery\Psr18ClientDiscovery;
7+
use http\Exception\RuntimeException;
8+
use InvalidArgumentException;
79
use Neo4j\QueryAPI\Exception\Neo4jException;
810
use Psr\Http\Client\ClientInterface;
911
use Neo4j\QueryAPI\Authentication\AuthenticateInterface;
@@ -15,35 +17,60 @@
1517
final class Neo4jQueryAPI
1618
{
1719
public function __construct(
18-
private ClientInterface $client,
19-
private ResponseParser $responseParser,
20-
private Neo4jRequestFactory $requestFactory
20+
private ClientInterface $client,
21+
private ResponseParser $responseParser,
22+
private Neo4jRequestFactory $requestFactory,
23+
private ?Configuration $config
2124
) {
25+
2226
}
2327

2428
/**
2529
* @api
2630
*/
27-
public static function login(string $address, AuthenticateInterface $auth = null): self
31+
public static function login(string $address = null, ?AuthenticateInterface $auth = null, ?Configuration $config = null): self
2832
{
33+
$config = $config ?? new Configuration(baseUri: $address ?? '');
34+
if (
35+
trim($config->baseUri) !== '' &&
36+
$address !== null &&
37+
trim($address) !== '' &&
38+
$config->baseUri !== $address
39+
) {
40+
throw new InvalidArgumentException(sprintf('Address (%s) as argument is different from address in configuration (%s)', $config->baseUri, $address));
41+
}
42+
2943
$client = Psr18ClientDiscovery::find();
3044

3145
return new self(
3246
client: $client,
33-
responseParser: new ResponseParser(
34-
ogm: new OGM()
35-
),
47+
responseParser: new ResponseParser(new OGM()),
3648
requestFactory: new Neo4jRequestFactory(
3749
psr17Factory: Psr17FactoryDiscovery::findRequestFactory(),
3850
streamFactory: Psr17FactoryDiscovery::findStreamFactory(),
39-
configuration: new Configuration(
40-
baseUri: $address
41-
),
51+
configuration: $config,
4252
auth: $auth ?? Authentication::fromEnvironment()
43-
)
53+
),
54+
config: $config
4455
);
4556
}
4657

58+
/**
59+
* @api
60+
*/
61+
public function create(Configuration $configuration, AuthenticateInterface $auth = null): self
62+
{
63+
return self::login(auth: $auth, config: $configuration);
64+
}
65+
66+
public function getConfig(): Configuration
67+
{
68+
return $this->config;
69+
}
70+
71+
/**
72+
* Executes a Cypher query.
73+
*/
4774
public function run(string $cypher, array $parameters = []): ResultSet
4875
{
4976
$request = $this->requestFactory->buildRunQueryRequest($cypher, $parameters);
@@ -53,13 +80,13 @@ public function run(string $cypher, array $parameters = []): ResultSet
5380
} catch (RequestExceptionInterface $e) {
5481
$this->handleRequestException($e);
5582
}
83+
5684
return $this->responseParser->parseRunQueryResponse($response);
5785
}
5886

5987
public function beginTransaction(): Transaction
6088
{
6189
$request = $this->requestFactory->buildBeginTransactionRequest();
62-
$response = $this->client->sendRequest($request);
6390

6491
try {
6592
$response = $this->client->sendRequest($request);
@@ -81,8 +108,6 @@ public function beginTransaction(): Transaction
81108
);
82109
}
83110

84-
85-
86111
/**
87112
* Handles request exceptions by parsing error details and throwing a Neo4jException.
88113
*
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Neo4j\QueryAPI\Tests\Integration;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
use Neo4j\QueryAPI\Neo4jQueryAPI;
8+
use Neo4j\QueryAPI\Objects\Authentication;
9+
use Neo4j\QueryAPI\Configuration;
10+
11+
final class Neo4jQueryAPITest extends TestCase
12+
{
13+
public function testLoginWithValidConfiguration(): void
14+
{
15+
$config = new Configuration(baseUri: 'http://valid.address');
16+
17+
$this->expectException(InvalidArgumentException::class);
18+
$this->expectExceptionMessage('Address (http://valid.address) as argument is different from address in configuration (http://myaddress)');
19+
20+
Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
21+
}
22+
23+
public function testLoginWithNullConfiguration(): void
24+
{
25+
$config = null;
26+
27+
$api = Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
28+
29+
$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
30+
$this->assertEquals('http://myaddress', $api->getConfig()->baseUri);
31+
}
32+
33+
public function testConfigOnly(): void
34+
{
35+
$config = new Configuration(baseUri: 'http://valid.address');
36+
37+
$api = Neo4jQueryAPI::login(auth: Authentication::fromEnvironment(), config: $config);
38+
39+
$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
40+
$this->assertEquals('http://valid.address', $api->getConfig()->baseUri);
41+
}
42+
}

0 commit comments

Comments
 (0)