Skip to content

Commit

Permalink
fix: Pass logger to ClientBuilder (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
exaby73 authored Nov 22, 2024
1 parent 3f36df4 commit 5bf29c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/ClientBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Laudis\Neo4j\Formatter\OGMFormatter;
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
use Laudis\Neo4j\Types\CypherMap;
use Psr\Log\LoggerInterface;

/**
* Immutable factory for creating a client.
Expand Down Expand Up @@ -58,16 +59,19 @@ public function __construct(
/**
* Creates a client builder with default configurations and an OGMFormatter.
*
* @pure
*
* @return ClientBuilder<SummarizedResult<CypherMap<OGMTypes>>>
*/
public static function create(): ClientBuilder
public static function create(?string $logLevel = null, ?LoggerInterface $logger = null): ClientBuilder
{
$configuration = DriverConfiguration::default();
if ($logLevel !== null && $logger !== null) {
$configuration = $configuration->withLogger($logLevel, $logger);
}

return new self(
SessionConfiguration::default(),
TransactionConfiguration::default(),
new DriverSetupManager(SummarizedResultFormatter::create(), DriverConfiguration::default())
new DriverSetupManager(SummarizedResultFormatter::create(), $configuration)
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Laudis\Neo4j\Http\HttpDriver;
use Laudis\Neo4j\Neo4j\Neo4jDriver;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;

/**
* Factory for creating drivers directly.
Expand All @@ -46,7 +45,7 @@ final class DriverFactory
* : DriverInterface<OGMResults>
* )
*/
public static function create(string|UriInterface $uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, ?FormatterInterface $formatter = null, ?string $logLevel = null, ?LoggerInterface $logger = null): DriverInterface
public static function create(string|UriInterface $uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, ?FormatterInterface $formatter = null): DriverInterface
{
if (is_string($uri)) {
$uri = Uri::create($uri);
Expand Down
17 changes: 13 additions & 4 deletions tests/Integration/ClientBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,42 @@

use Laudis\Neo4j\Client;
use Laudis\Neo4j\ClientBuilder;
use Laudis\Neo4j\Common\Neo4jLogger;
use Laudis\Neo4j\Databags\DriverConfiguration;
use Laudis\Neo4j\Databags\SessionConfiguration;
use Laudis\Neo4j\Databags\SslConfiguration;
use Laudis\Neo4j\Databags\TransactionConfiguration;
use Laudis\Neo4j\Enum\SslMode;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;

class ClientBuilderTest extends TestCase
{
public function testGetClient(): void
{
$logger = $this->createMock(LoggerInterface::class);
$sslConfig = SslConfiguration::default()->withVerifyPeer(false)->withMode(SslMode::FROM_URL());
$driverconfig = DriverConfiguration::default()
->withSslConfiguration($sslConfig)
->withMaxPoolSize(4096)
->withAcquireConnectionTimeout(2.5);
->withAcquireConnectionTimeout(2.5)
->withLogger(LogLevel::DEBUG, $logger);
$sessionConfig = SessionConfiguration::default()->withDatabase('neo4j');
$transactionConfig = TransactionConfiguration::default()->withTimeout(120.0);
$client = ClientBuilder::create()
$client = ClientBuilder::create(LogLevel::DEBUG, $logger)
->withDefaultDriverConfiguration($driverconfig)
->withDefaultSessionConfiguration($sessionConfig)
->withDefaultTransactionConfiguration($transactionConfig)
->build();

self::assertInstanceOf(Client::class, $client);
self::assertEquals($driverconfig, $client->getDriverSetups()->getDriverConfiguration());
self::assertEquals($sslConfig, $client->getDriverSetups()->getDriverConfiguration()->getSslConfiguration());

$driverConfigurationFromClient = $client->getDriverSetups()->getDriverConfiguration();
self::assertInstanceOf(Neo4jLogger::class, $driverConfigurationFromClient->getLogger());

self::assertEquals($driverconfig, $driverConfigurationFromClient);
self::assertEquals($sslConfig, $driverConfigurationFromClient->getSslConfiguration());
self::assertEquals($sessionConfig, $client->getDefaultSessionConfiguration());
self::assertEquals($transactionConfig, $client->getDefaultTransactionConfiguration());
}
Expand Down

0 comments on commit 5bf29c7

Please sign in to comment.