Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 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.

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

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,
) {
if (empty($this->baseUri)) {
throw new InvalidArgumentException("Address (baseUri) must be provided.");
}
}
}
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
42 changes: 42 additions & 0 deletions src/abc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

use Neo4j\QueryAPI\Neo4jQueryAPI;
use Neo4j\QueryAPI\Objects\Authentication;
use Neo4j\QueryAPI\Configuration;

$neo4jUsername = 'neo4j';
$neo4jPassword = 'OXDRMgdWFKMcBRCBrIwXnKkwLgDlmFxipnywT6t_AK0';
$neo4jUrl = 'https://bb79fe35.databases.neo4j.io';

$auth = Authentication::basic($neo4jUsername, $neo4jPassword);

$config = new Configuration(baseUri: $neo4jUrl);

$neo4j = Neo4jQueryAPI::login(
$neo4jUrl,
$auth,
$config
);

$cypher = 'MATCH (n:Movie) RETURN n LIMIT 25';
$resultSet = $neo4j->run($cypher);

foreach ($resultSet as $row) {
$node = $row['n'];

$properties = $node->getProperties();

if (isset($properties['title'])) {
echo "Movie Title: " . $properties['title'] . "\n";
} else {
echo "Title not found.\n";
}

if (isset($properties['released'])) {
echo "Movie Year: " . $properties['released'] . "\n";
} else {
echo "Year not found.\n";
}
}
62 changes: 62 additions & 0 deletions tests/Integration/Neo4jQueryAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?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 testLoginWithConfigurationWithoutAddress()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Address (baseUri) must be provided.");
$config = new Configuration(baseUri: "");

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

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 testLoginWithEmptyAddress()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Address (baseUri) must be provided.");

$config = new Configuration(baseUri: "");

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

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);
}
}