Skip to content

Commit 04af22c

Browse files
committed
fixed routing bug when using neo4j v3
1 parent 6534255 commit 04af22c

12 files changed

+17
-149
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"php-http/message": "^1.0",
3333
"php-ds/php-ds": "1.*",
3434
"php-http/message-factory": "^1.0",
35-
"stefanak-michal/bolt": "^2.2",
35+
"stefanak-michal/bolt": "^2.3",
3636
"symfony/polyfill-php80": "^1.2"
3737
},
3838
"suggest": {

src/Bolt/Session.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,7 @@ public function beginTransaction(?iterable $statements = null, ?TransactionConfi
116116
$bolt = new Bolt($this->pool->acquire($this->uri, $this->config->getAccessMode()));
117117
$this->auth->authenticateBolt($bolt, $this->uri, $this->userAgent);
118118

119-
$protocolVersion = $bolt->getProtocolVersion();
120-
if ($protocolVersion >= 4.0) {
121-
$begin = $bolt->begin(['db' => $this->config->getDatabase()]);
122-
} else {
123-
$bolt->setProtocolVersions((int) $protocolVersion);
124-
$begin = $bolt->begin();
125-
}
119+
$begin = $bolt->begin(['db' => $this->config->getDatabase()]);
126120

127121
if (!$begin) {
128122
throw new Neo4jException(new Vector([new Neo4jError('', 'Cannot open new transaction')]));

src/Neo4j/Neo4jConnectionPool.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Laudis\Neo4j\Neo4j;
1515

16+
use function array_filter;
1617
use Bolt\connection\StreamSocket;
1718
use Exception;
1819
use Laudis\Neo4j\Bolt\BoltDriver;
@@ -36,15 +37,13 @@ final class Neo4jConnectionPool implements ConnectionPoolInterface
3637
private ?RoutingTable $table = null;
3738
/** @var ConnectionPoolInterface<StreamSocket> */
3839
private ConnectionPoolInterface $pool;
39-
private string $version;
4040

4141
/**
4242
* @param ConnectionPoolInterface<StreamSocket> $pool
4343
*/
44-
public function __construct(ConnectionPoolInterface $pool, string $version)
44+
public function __construct(ConnectionPoolInterface $pool)
4545
{
4646
$this->pool = $pool;
47-
$this->version = $version;
4847
}
4948

5049
/**
@@ -82,18 +81,27 @@ private function routingTable(DriverInterface $driver): RoutingTable
8281
{
8382
if ($this->table === null || $this->table->getTtl() < time()) {
8483
$session = $driver->createSession();
85-
if (str_starts_with($this->version, '3')) {
84+
$row = $session->run(
85+
'CALL dbms.components() yield versions UNWIND versions as version RETURN version'
86+
)->first();
87+
/** @var string */
88+
$version = $row->get('version');
89+
90+
if (str_starts_with($version, '3')) {
8691
$response = $session->run('CALL dbms.cluster.overview()');
8792

8893
/** @var iterable<array{addresses: list<string>, role:string}> $values */
8994
$values = [];
9095
foreach ($response as $server) {
96+
/** @var list<string> $addresses */
97+
$addresses = $server->get('addresses');
98+
$addresses = array_filter($addresses, static fn (string $x) => str_starts_with($x, 'bolt://'));
9199
/**
92100
* @psalm-suppress InvalidArrayAssignment
93101
*
94102
* @var array{addresses: list<string>, role:string}
95103
*/
96-
$values[] = ['addresses' => $server->get('addresses'), 'role' => $server->get('role')];
104+
$values[] = ['addresses' => $addresses, 'role' => $server->get('role')];
97105
}
98106

99107
$this->table = new RoutingTable($values, time() + 3600);

src/Neo4j/Neo4jDriver.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use function is_string;
1919
use Laudis\Neo4j\Authentication\Authenticate;
2020
use Laudis\Neo4j\Bolt\BoltConnectionPool;
21-
use Laudis\Neo4j\Bolt\BoltDriver;
2221
use Laudis\Neo4j\Bolt\Session;
2322
use Laudis\Neo4j\Common\Uri;
2423
use Laudis\Neo4j\Contracts\AuthenticateInterface;
@@ -82,17 +81,10 @@ public static function createWithFormatter($uri, FormatterInterface $formatter,
8281
$uri = Uri::create($uri);
8382
}
8483

85-
$session = BoltDriver::create($uri)->createSession();
86-
$row = $session->run(
87-
'CALL dbms.components() yield versions UNWIND versions as version RETURN version'
88-
)->first();
89-
$version = $row->get('version');
90-
91-
/** @psalm-suppress all */
9284
return new self(
9385
$uri,
9486
$authenticate ?? Authenticate::fromUrl(),
95-
new Neo4jConnectionPool(new BoltConnectionPool(), $version),
87+
new Neo4jConnectionPool(new BoltConnectionPool()),
9688
$configuration ?? DriverConfiguration::default(),
9789
$formatter
9890
);

src/Neo4j/RoutingTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
namespace Laudis\Neo4j\Neo4j;
1515

1616
use Ds\Set;
17-
use Laudis\Neo4j\Enum\RoutingRoles;
1817
use function in_array;
18+
use Laudis\Neo4j\Enum\RoutingRoles;
1919

2020
final class RoutingTable
2121
{

tests/Helpers/TestHelper.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

tests/Integration/ClientIntegrationTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use Laudis\Neo4j\Contracts\TransactionInterface;
2323
use Laudis\Neo4j\Databags\Statement;
2424
use Laudis\Neo4j\Exception\Neo4jException;
25-
use Laudis\Neo4j\Tests\Helpers\TestHelper;
2625
use PHPUnit\Framework\TestCase;
2726

2827
final class ClientIntegrationTest extends TestCase
@@ -72,7 +71,6 @@ public function testEqualEffect(): void
7271

7372
public function testAvailabilityFullImplementation(): void
7473
{
75-
TestHelper::skipIfUnsupportedVersion('cluster', __CLASS__);
7674
$results = $this->client->getDriver('cluster')
7775
->createSession()
7876
->beginTransaction()
@@ -109,8 +107,6 @@ public function testTransactionFunction(): void
109107
*/
110108
public function testValidRun(string $alias): void
111109
{
112-
TestHelper::skipIfUnsupportedVersion($alias, __CLASS__);
113-
114110
$response = $this->client->run(<<<'CYPHER'
115111
MERGE (x:TestNode {test: $test})
116112
WITH x
@@ -148,8 +144,6 @@ public function testInvalidRun(string $alias): void
148144
*/
149145
public function testValidStatement(string $alias): void
150146
{
151-
TestHelper::skipIfUnsupportedVersion($alias, __CLASS__);
152-
153147
$response = $this->client->runStatement(
154148
Statement::create(<<<'CYPHER'
155149
MERGE (x:TestNode {test: $test})
@@ -191,8 +185,6 @@ public function testInvalidStatement(string $alias): void
191185
*/
192186
public function testStatements(string $alias): void
193187
{
194-
TestHelper::skipIfUnsupportedVersion($alias, __CLASS__);
195-
196188
$params = ['test' => 'a', 'otherTest' => 'b'];
197189
$response = $this->client->runStatements([
198190
Statement::create(<<<'CYPHER'
@@ -248,8 +240,6 @@ public function testInvalidStatements(string $alias): void
248240
*/
249241
public function testMultipleTransactions(string $alias): void
250242
{
251-
TestHelper::skipIfUnsupportedVersion($alias, __CLASS__);
252-
253243
$x = $this->client->beginTransaction(null, $alias);
254244
$y = $this->client->beginTransaction(null, $alias);
255245
self::assertNotSame($x, $y);

tests/Integration/ClusterIntegrationTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Laudis\Neo4j\Bolt\BoltConfiguration;
1919
use Laudis\Neo4j\ClientBuilder;
2020
use Laudis\Neo4j\Contracts\ClientInterface;
21-
use Laudis\Neo4j\Tests\Helpers\TestHelper;
2221
use PHPUnit\Framework\TestCase;
2322

2423
final class ClusterIntegrationTest extends TestCase
@@ -40,8 +39,6 @@ protected function setUp(): void
4039
*/
4140
public function testAcceptance(string $connection): void
4241
{
43-
TestHelper::skipIfUnsupportedVersion($connection, __CLASS__);
44-
4542
self::assertEquals(1, $this->client->run('RETURN 1 as x', [], $connection)->first()->get('x'));
4643
}
4744

@@ -50,8 +47,6 @@ public function testAcceptance(string $connection): void
5047
*/
5148
public function testWrite(string $connection): void
5249
{
53-
TestHelper::skipIfUnsupportedVersion($connection, __CLASS__);
54-
5550
self::assertEquals([], $this->client->run('CREATE (x:X) RETURN x', [], $connection)->first()->get('x'));
5651
}
5752

tests/Integration/ConsistencyTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Laudis\Neo4j\ClientBuilder;
1919
use Laudis\Neo4j\Contracts\ClientInterface;
2020
use Laudis\Neo4j\Databags\Statement;
21-
use Laudis\Neo4j\Tests\Helpers\TestHelper;
2221
use PHPUnit\Framework\TestCase;
2322

2423
final class ConsistencyTest extends TestCase
@@ -41,8 +40,6 @@ protected function setUp(): void
4140
*/
4241
public function testConsistency(string $alias): void
4342
{
44-
TestHelper::skipIfUnsupportedVersion($alias, __CLASS__);
45-
4643
$this->client->run('MATCH (x) DETACH DELETE x', [], $alias);
4744
$res = $this->client->run('MERGE (n:zzz {name: "bbbb"}) RETURN n', [], $alias);
4845
self::assertEquals(1, $res->count());
@@ -58,8 +55,6 @@ public function testConsistency(string $alias): void
5855
*/
5956
public function testConsistencyTransaction(string $alias): void
6057
{
61-
TestHelper::skipIfUnsupportedVersion($alias, __CLASS__);
62-
6358
$this->client->run('MATCH (x) DETACH DELETE x', [], $alias);
6459
$tsx = $this->client->beginTransaction([
6560
Statement::create('CREATE (n:aaa) SET n.name="aaa" return n'),

0 commit comments

Comments
 (0)