Skip to content

Commit c241a5b

Browse files
PratikshaPratiksha
authored andcommitted
winp
1 parent 7cf06a0 commit c241a5b

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

src/Configuration.php

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

33
namespace Neo4j\QueryAPI;
44

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

89
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+
// Validate that baseUri is not empty
30+
if (empty($this->baseUri)) {
31+
throw new InvalidArgumentException("Address (baseUri) must be provided.");
32+
}
1733
}
1834
}
35+

src/Neo4jQueryAPI.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public static function login(string $address, ?AuthenticateInterface $auth = nul
3434
$client = new Client();
3535
$config = $config ?? new Configuration(baseUri: $address);
3636

37+
38+
/* if the user now passes a config object without an address it will break.
39+
40+
$config = new Configuration(database: 'mydb');
41+
42+
QueryApi::login('http://myaddress', Authentication::fromEnvironment(), $config);*/
3743
return new self(
3844
client: $client,
3945
responseParser: new ResponseParser(new OGM()),
@@ -44,6 +50,7 @@ public static function login(string $address, ?AuthenticateInterface $auth = nul
4450
auth: $auth ?? Authentication::fromEnvironment()
4551
),
4652
config: $config
53+
4754
);
4855
}
4956

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace Neo4j\QueryAPI\Tests\Integration;
3+
4+
use InvalidArgumentException;
5+
use PHPUnit\Framework\TestCase;
6+
use Neo4j\QueryAPI\Neo4jQueryAPI;
7+
use Neo4j\QueryAPI\Objects\Authentication;
8+
use Neo4j\QueryAPI\Configuration;
9+
10+
class Neo4jQueryAPITest extends TestCase
11+
{
12+
public function testLoginWithConfigurationWithoutAddress()
13+
{
14+
$this->expectException(InvalidArgumentException::class);
15+
$this->expectExceptionMessage("Address (baseUri) must be provided.");
16+
$config = new Configuration(baseUri: "");
17+
18+
Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
19+
}
20+
21+
public function testLoginWithValidConfiguration()
22+
{
23+
$config = new Configuration(baseUri: 'http://valid.address');
24+
25+
$api = Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
26+
27+
$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
28+
$this->assertEquals('http://valid.address', $config->baseUri);
29+
}
30+
public function testLoginWithEmptyAddress()
31+
{
32+
$this->expectException(InvalidArgumentException::class);
33+
$this->expectExceptionMessage("Address (baseUri) must be provided.");
34+
35+
$config = new Configuration(baseUri: "");
36+
37+
Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);
38+
}
39+
/**
40+
* Test to ensure that an exception is thrown when baseUri is empty.
41+
*/
42+
43+
44+
}

0 commit comments

Comments
 (0)