Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ test
#PHP-CS-FIXER
.php-cs-fixer.php
.php-cs-fixer.cache


1 change: 0 additions & 1 deletion .php-cs-fixer.cache

This file was deleted.

13 changes: 13 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@

namespace Neo4j\QueryAPI;

use InvalidArgumentException;
use Neo4j\QueryAPI\Objects\Bookmarks;
use Neo4j\QueryAPI\Enums\AccessMode;

class Configuration
{
/**
* Constructor for Configuration class.
*
* @param string $baseUri The base URI for the Neo4j instance.
* @param string $database The name of the database to connect to.
* @param bool $includeCounters Whether to include counters in the response.
* @param Bookmarks $bookmarks Bookmarks for tracking queries.
* @param AccessMode $accessMode The access mode for the connection (read/write).
*
* @throws InvalidArgumentException if $baseUri is empty.
*/
public function __construct(
public readonly string $baseUri,
public readonly string $database = 'neo4j',
public readonly bool $includeCounters = true,
public readonly Bookmarks $bookmarks = new Bookmarks([]),
public readonly AccessMode $accessMode = AccessMode::WRITE,
) {

}
}
36 changes: 24 additions & 12 deletions src/Neo4jQueryAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,56 @@
use Neo4j\QueryAPI\Exception\Neo4jException;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Results\ResultSet;
use Neo4j\QueryAPI\Configuration;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Client\RequestExceptionInterface;
use Psr\Http\Message\ResponseInterface;

class Neo4jQueryAPI
{
private Configuration $config;

public function __construct(
private ClientInterface $client,
private ResponseParser $responseParser,
private Neo4jRequestFactory $requestFactory
private Neo4jRequestFactory $requestFactory,
?Configuration $config = null
) {
$this->config = $config ?? new Configuration(baseUri: 'http://myaddress'); // Default configuration if not provided
}

/**
* @api
*/
public static function login(string $address, AuthenticateInterface $auth = null): self
public static function login(string $address = null, ?AuthenticateInterface $auth = null, ?Configuration $config = null): self
{
$client = new Client();
$config = $config ?? new Configuration(baseUri: $address);


/* if the user now passes a config object without an address it will break.

$config = new Configuration(database: 'mydb');

QueryApi::login('http://myaddress', Authentication::fromEnvironment(), $config);*/
return new self(
client: $client,
responseParser: new ResponseParser(
ogm: new OGM()
),
responseParser: new ResponseParser(new OGM()),
requestFactory: new Neo4jRequestFactory(
psr17Factory: new Psr17Factory(),
streamFactory: new Psr17Factory(),
configuration: new Configuration(
baseUri: $address
),
configuration: $config,
auth: $auth ?? Authentication::fromEnvironment()
)
),
config: $config
);
}


public function getConfig(): Configuration
{
return $this->config;
}

/**
* Executes a Cypher query.
Expand All @@ -61,7 +73,6 @@ public function run(string $cypher, array $parameters = []): ResultSet

return $this->responseParser->parseRunQueryResponse($response);
}

/**
* Starts a transaction.
*/
Expand All @@ -88,6 +99,7 @@ public function beginTransaction(): Transaction
);
}


/**
* Handles request exceptions by parsing error details and throwing a Neo4jException.
*
Expand All @@ -97,7 +109,7 @@ private function handleRequestException(RequestExceptionInterface $e): void
{
$response = $e->getResponse();
if ($response instanceof ResponseInterface) {
$errorResponse = json_decode((string)$response->getBody(), true);
$errorResponse = json_decode((string) $response->getBody(), true);
throw Neo4jException::fromNeo4jResponse($errorResponse, $e);
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Integration/Neo4jQueryAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Neo4j\QueryAPI\Tests\Integration;

use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Neo4j\QueryAPI\Neo4jQueryAPI;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Configuration;

/** @psalm-suppress UnusedClass */
class Neo4jQueryAPITest extends TestCase
{
public function testLoginWithValidConfiguration()
{
$config = new Configuration(baseUri: 'http://valid.address');

$api = Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);

$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
$this->assertEquals('http://valid.address', $api->getConfig()->baseUri);
}

public function testLoginWithNullConfiguration()
{
$config = null;

$api = Neo4jQueryAPI::login('http://myaddress', Authentication::fromEnvironment(), $config);

$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
$this->assertEquals('http://myaddress', $api->getConfig()->baseUri);
}

public function testConfigOnly()
{
$config = new Configuration(baseUri: 'http://valid.address');

$api = Neo4jQueryAPI::login(auth: Authentication::fromEnvironment(), config: $config);

$this->assertInstanceOf(Neo4jQueryAPI::class, $api);
$this->assertEquals('http://valid.address', $api->getConfig()->baseUri);
}
}