Skip to content

Commit 5306c17

Browse files
committed
test: Add test for Client
1 parent 5676a7a commit 5306c17

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

src/Bolt/BoltDriver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use Exception;
1717

18-
use Psr\Log\LogLevel;
1918
use function is_string;
2019

2120
use Laudis\Neo4j\Authentication\Authenticate;
@@ -29,7 +28,7 @@
2928
use Laudis\Neo4j\Databags\SessionConfiguration;
3029
use Laudis\Neo4j\Formatter\OGMFormatter;
3130
use Psr\Http\Message\UriInterface;
32-
use Throwable;
31+
use Psr\Log\LogLevel;
3332

3433
/**
3534
* Drives a singular bolt connections.
@@ -105,7 +104,8 @@ public function verifyConnectivity(?SessionConfiguration $config = null): bool
105104
try {
106105
GeneratorHelper::getReturnFromGenerator($this->pool->acquire($config));
107106
} catch (ConnectException $e) {
108-
$this->pool->getLogger()->log(LogLevel::WARNING, 'Could not connect to server on URI ' . $this->parsedUrl->__toString(), ['error' => $e]);
107+
$this->pool->getLogger()->log(LogLevel::WARNING, 'Could not connect to server on URI '.$this->parsedUrl->__toString(), ['error' => $e]);
108+
109109
return false;
110110
}
111111

src/Common/DriverSetupManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313

1414
namespace Laudis\Neo4j\Common;
1515

16-
use Bolt\error\ConnectException;
17-
use Psr\Log\LogLevel;
1816
use function array_key_exists;
1917
use function array_key_first;
2018
use function array_reduce;
2119

20+
use Bolt\error\ConnectException;
2221
use Countable;
2322
use InvalidArgumentException;
2423
use Laudis\Neo4j\Authentication\Authenticate;
@@ -31,6 +30,7 @@
3130

3231
use const PHP_INT_MIN;
3332

33+
use Psr\Log\LogLevel;
3434
use RuntimeException;
3535
use SplPriorityQueue;
3636

@@ -152,6 +152,7 @@ public function verifyConnectivity(SessionConfiguration $config, ?string $alias
152152
sprintf('Could not connect to server using alias (%s)', $alias ?? '<default>'),
153153
['exception' => $e]
154154
);
155+
155156
return false;
156157
}
157158

src/Neo4j/Neo4jDriver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Bolt\error\ConnectException;
1717
use Exception;
1818

19-
use Psr\Log\LogLevel;
2019
use function is_string;
2120

2221
use Laudis\Neo4j\Authentication\Authenticate;
@@ -33,6 +32,7 @@
3332
use Laudis\Neo4j\Databags\SessionConfiguration;
3433
use Laudis\Neo4j\Formatter\OGMFormatter;
3534
use Psr\Http\Message\UriInterface;
35+
use Psr\Log\LogLevel;
3636

3737
/**
3838
* Driver for auto client-side routing.
@@ -107,7 +107,7 @@ public function verifyConnectivity(?SessionConfiguration $config = null): bool
107107
try {
108108
GeneratorHelper::getReturnFromGenerator($this->pool->acquire($config));
109109
} catch (ConnectException $e) {
110-
$this->pool->getLogger()->log(LogLevel::WARNING, 'Could not connect to server on URI ' . $this->parsedUrl->__toString(), ['error' => $e]);
110+
$this->pool->getLogger()->log(LogLevel::WARNING, 'Could not connect to server on URI '.$this->parsedUrl->__toString(), ['error' => $e]);
111111

112112
return false;
113113
}

tests/Integration/ClientIntegrationTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
use Laudis\Neo4j\Bolt\BoltDriver;
2020
use Laudis\Neo4j\Bolt\ConnectionPool;
2121
use Laudis\Neo4j\ClientBuilder;
22+
use Laudis\Neo4j\Common\DriverSetupManager;
2223
use Laudis\Neo4j\Common\Uri;
2324
use Laudis\Neo4j\Contracts\DriverInterface;
2425
use Laudis\Neo4j\Contracts\TransactionInterface;
2526
use Laudis\Neo4j\Databags\DriverConfiguration;
27+
use Laudis\Neo4j\Databags\DriverSetup;
2628
use Laudis\Neo4j\Databags\SessionConfiguration;
2729
use Laudis\Neo4j\Databags\Statement;
30+
use Laudis\Neo4j\Databags\TransactionConfiguration;
2831
use Laudis\Neo4j\Exception\Neo4jException;
32+
use Laudis\Neo4j\Formatter\SummarizedResultFormatter;
2933
use Laudis\Neo4j\Tests\EnvironmentAwareIntegrationTest;
3034
use Psr\Log\LoggerInterface;
3135
use Psr\Log\LogLevel;
@@ -61,6 +65,45 @@ public function testDriverAuthFailureVerifyConnectivity(): void
6165
$driver->verifyConnectivity();
6266
}
6367

68+
public function testClientAuthFailureVerifyConnectivity(): void
69+
{
70+
$connection = $_ENV['CONNECTION'] ?? false;
71+
if (str_starts_with($connection, 'http')) {
72+
$this->markTestSkipped('HTTP does not support auth failure connectivity passing');
73+
}
74+
75+
if (!is_string($connection)) {
76+
$connection = 'bolt://localhost';
77+
}
78+
79+
$uri = Uri::create($connection);
80+
$uri = $uri->withUserInfo('neo4j', 'absolutelyonehundredpercentawrongpassword');
81+
82+
/** @noinspection PhpUnhandledExceptionInspection */
83+
$conf = DriverConfiguration::default()->withLogger(LogLevel::DEBUG, $this->createMock(LoggerInterface::class));
84+
$logger = $conf->getLogger();
85+
if ($logger === null) {
86+
throw new RuntimeException('Logger not set');
87+
}
88+
89+
$client = (new ClientBuilder(
90+
SessionConfiguration::create(),
91+
TransactionConfiguration::create(),
92+
(new DriverSetupManager(
93+
SummarizedResultFormatter::create(),
94+
$conf,
95+
))->withSetup(
96+
new DriverSetup($uri, Authenticate::fromUrl($uri, $logger))
97+
)
98+
))->build();
99+
100+
$driver = $client->getDriver(null);
101+
102+
$this->expectException(Neo4jException::class);
103+
$this->expectExceptionMessage('Neo4j errors detected. First one with code "Neo.ClientError.Security.Unauthorized" and message "The client is unauthorized due to authentication failure."');
104+
$driver->verifyConnectivity();
105+
}
106+
64107
public function testDifferentAuth(): void
65108
{
66109
$auth = Authenticate::fromUrl($this->getUri());

0 commit comments

Comments
 (0)