Skip to content

Commit 3c18f1d

Browse files
committed
fixed static analysis tests
1 parent e6f1bc2 commit 3c18f1d

9 files changed

+158
-173
lines changed

src/Bolt/BoltConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*
3636
* @psalm-import-type BoltMeta from FormatterInterface
3737
*/
38-
final class BoltConnection implements ConnectionInterface
38+
class BoltConnection implements ConnectionInterface
3939
{
4040
private V3 $boltProtocol;
4141
/** @psalm-readonly */

src/Bolt/SslConfigurationFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class SslConfigurationFactory
2424
{
2525
/**
26-
* @return array{0: 's'|'ssc'|'s', 1: array}
26+
* @return array{0: 's'|'ssc'|'', 1: array{verify_peer?: bool, peer_name?: string, SNI_enabled?: bool, allow_self_signed?: bool}}
2727
*/
2828
public function create(UriInterface $uri, SslConfiguration $config): array
2929
{
@@ -46,6 +46,9 @@ public function create(UriInterface $uri, SslConfiguration $config): array
4646
return [$sslConfig, []];
4747
}
4848

49+
/**
50+
* @return array{verify_peer?: bool, peer_name?: string, SNI_enabled?: bool, allow_self_signed?: bool}
51+
*/
4952
private function enableSsl(string $host, string $sslConfig, SslConfiguration $config): array
5053
{
5154
$options = [

src/BoltFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Small wrapper around the bolt library to easily guarantee only bolt version 3 and up will be created and authenticated.
3434
*/
35-
final class BoltFactory
35+
class BoltFactory
3636
{
3737
private BasicConnectionFactoryInterface $connectionFactory;
3838
private ProtocolFactory $protocolFactory;

tests/Integration/BoltResultIntegrationTest.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/*
46
* This file is part of the Neo4j PHP Client and Driver package.
57
*
@@ -11,21 +13,20 @@
1113

1214
namespace Laudis\Neo4j\Tests\Integration;
1315

14-
use Bolt\Bolt;
15-
use Bolt\connection\StreamSocket;
1616
use Dotenv\Dotenv;
1717
use function explode;
1818
use function is_string;
1919
use Laudis\Neo4j\Authentication\Authenticate;
20-
use Laudis\Neo4j\Bolt\BoltConnection;
2120
use Laudis\Neo4j\Bolt\BoltResult;
22-
use Laudis\Neo4j\Bolt\SslConfigurator;
21+
use Laudis\Neo4j\Bolt\ProtocolFactory;
22+
use Laudis\Neo4j\Bolt\SslConfigurationFactory;
23+
use Laudis\Neo4j\Bolt\SystemWideConnectionFactory;
2324
use Laudis\Neo4j\BoltFactory;
24-
use Laudis\Neo4j\Common\ConnectionConfiguration;
2525
use Laudis\Neo4j\Common\Uri;
26-
use Laudis\Neo4j\Databags\DriverConfiguration;
27-
use Laudis\Neo4j\Enum\AccessMode;
28-
use Laudis\Neo4j\Enum\ConnectionProtocol;
26+
use Laudis\Neo4j\Databags\ConnectionRequestData;
27+
use Laudis\Neo4j\Databags\SessionConfiguration;
28+
use Laudis\Neo4j\Databags\SslConfiguration;
29+
use Laudis\Neo4j\Enum\SslMode;
2930
use PHPUnit\Framework\TestCase;
3031

3132
final class BoltResultIntegrationTest extends TestCase
@@ -59,26 +60,18 @@ public function buildConnections(): array
5960
public function testIterationLong(string $connection): void
6061
{
6162
$uri = Uri::create($connection);
62-
$socket = new StreamSocket($uri->getHost(), $uri->getPort() ?? 7687);
63-
$options = (new SslConfigurator())->configure($uri, DriverConfiguration::default());
64-
if ($options !== null) {
65-
$socket->setSslContextOptions($options);
66-
}
67-
6863
$i = 0;
69-
$factory = new BoltFactory(new Bolt($socket), Authenticate::fromUrl($uri), '', $socket);
70-
$config = new ConnectionConfiguration(
71-
'',
72-
$uri,
73-
'',
74-
ConnectionProtocol::determineBoltVersion($factory->build()[0]),
75-
AccessMode::READ(),
76-
DriverConfiguration::default(),
77-
null
64+
$factory = new BoltFactory(
65+
SystemWideConnectionFactory::getInstance(),
66+
new ProtocolFactory(),
67+
new SslConfigurationFactory()
7868
);
79-
$connection = new BoltConnection($factory, null, $config);
80-
$connection->open();
81-
$connection->getImplementation()->run('UNWIND range(1, 100000) AS i RETURN i');
69+
$connection = $factory->createConnection(
70+
new ConnectionRequestData($uri, Authenticate::fromUrl($uri), 'a/b', new SslConfiguration(SslMode::FROM_URL(), false)),
71+
SessionConfiguration::default()
72+
);
73+
74+
$connection->getImplementation()[0]->run('UNWIND range(1, 100000) AS i RETURN i');
8275
$result = new BoltResult($connection, 1000, -1);
8376
foreach ($result as $i => $x) {
8477
self::assertEquals($i + 1, $x[0] ?? 0);

tests/Integration/EdgeCasesTest.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@
1414
namespace Laudis\Neo4j\Tests\Integration;
1515

1616
use Dotenv\Dotenv;
17-
use Laudis\Neo4j\ClientBuilder;
17+
use Laudis\Neo4j\Basic\Driver;
18+
use Laudis\Neo4j\Basic\Session;
1819
use Laudis\Neo4j\Common\Uri;
19-
use Laudis\Neo4j\Contracts\ClientInterface;
2020
use Laudis\Neo4j\Databags\Statement;
2121
use Laudis\Neo4j\Tests\Fixtures\MoviesFixture;
2222
use Laudis\Neo4j\Types\Node;
2323
use PHPUnit\Framework\TestCase;
2424

2525
final class EdgeCasesTest extends TestCase
2626
{
27-
private ?ClientInterface $client = null;
27+
private ?Session $session = null;
2828

2929
protected function setUp(): void
3030
{
3131
parent::setUp();
3232
if (($uri = $this->getBoltUri()) !== null) {
33-
$this->client = ClientBuilder::create()
34-
->withDriver('neo4j', $uri)
35-
->build();
33+
$this->session = Driver::create($uri)->createSession();
3634
}
3735
}
3836

@@ -60,15 +58,15 @@ private function getBoltUri(): ?string
6058

6159
public function testRunALotOfStatements(): void
6260
{
63-
if ($this->client === null) {
61+
if ($this->session === null) {
6462
self::markTestSkipped('No neo4j uri provided');
6563
}
6664

67-
$this->client->run('MATCH (n) DETACH DELETE n');
68-
$this->client->run(MoviesFixture::CQL);
65+
$this->session->run('MATCH (n) DETACH DELETE n');
66+
$this->session->run(MoviesFixture::CQL);
6967

70-
$persons = $this->client->run('MATCH (p:Person) RETURN p');
71-
$movies = $this->client->run('MATCH (m:Movie) RETURN m');
68+
$persons = $this->session->run('MATCH (p:Person) RETURN p');
69+
$movies = $this->session->run('MATCH (m:Movie) RETURN m');
7270

7371
$personIds = [];
7472
foreach ($persons->toArray() as $record) {
@@ -98,27 +96,28 @@ public function testRunALotOfStatements(): void
9896
}
9997
}
10098

101-
$this->client->runStatements($statements);
99+
$this->session->runStatements($statements);
102100
self::assertCount(4978, $statements);
103101
}
104102

105103
public function testGettingKeysFromArraylist(): void
106104
{
107-
if ($this->client === null) {
105+
if ($this->session === null) {
108106
self::markTestSkipped('No neo4j uri provided');
109107
}
110108

111-
$this->client->run('MATCH (n) DETACH DELETE n');
112-
$this->client->run(MoviesFixture::CQL);
109+
$this->session->run('MATCH (n) DETACH DELETE n');
110+
$this->session->run(MoviesFixture::CQL);
113111

114-
$result = $this->client->run('MATCH (n:Person)-[r:ACTED_IN]->(m)
112+
$result = $this->session->run('MATCH (n:Person)-[r:ACTED_IN]->(m)
115113
RETURN n, {roles: r.roles, movie: m} AS actInfo LIMIT 1');
116114

117115
$resultKeys = [];
118116
foreach ($result->toArray() as $record) {
119117
$keys = $record->keys();
120118
foreach ($keys as $key) {
121119
// Calling count inside foreach breaks it
120+
/** @psalm-suppress UnusedFunctionCall */
122121
count($keys);
123122
$resultKeys[] = $key;
124123
}

tests/Integration/TransactionIntegrationTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313

1414
namespace Laudis\Neo4j\Tests\Integration;
1515

16-
use Laudis\Neo4j\Bolt\BoltConnectionPool;
1716
use Laudis\Neo4j\Bolt\BoltDriver;
17+
use Laudis\Neo4j\Bolt\Connection;
18+
use Laudis\Neo4j\Bolt\ConnectionPool;
1819
use Laudis\Neo4j\Contracts\FormatterInterface;
1920
use Laudis\Neo4j\Databags\Statement;
2021
use Laudis\Neo4j\Exception\Neo4jException;
@@ -24,7 +25,7 @@
2425
use Throwable;
2526

2627
/**
27-
* @psalm-import-type BasicResults from \Laudis\Neo4j\Formatter\BasicFormatter
28+
* @psalm-import-type BasicResults from BasicFormatter
2829
*
2930
* @extends EnvironmentAwareIntegrationTest<BasicResults>
3031
*/
@@ -377,7 +378,7 @@ public function testCorrectConnectionReuse(string $alias): void
377378
self::markTestSkipped('Can only white box test bolt driver');
378379
}
379380

380-
$poolReflection = new ReflectionClass(BoltConnectionPool::class);
381+
$poolReflection = new ReflectionClass(Connection::class);
381382
$poolReflection->setStaticPropertyValue('connectionCache', []);
382383

383384
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
@@ -388,7 +389,7 @@ public function testCorrectConnectionReuse(string $alias): void
388389
$b = $this->getClient()->beginTransaction([], $alias);
389390
$this->getClient()->run('MATCH (x) RETURN x', [], $alias);
390391

391-
$poolReflection = new ReflectionClass(BoltConnectionPool::class);
392+
$poolReflection = new ReflectionClass(ConnectionPool::class);
392393
/** @var array $cache */
393394
$cache = $poolReflection->getStaticPropertyValue('connectionCache');
394395

tests/Unit/BoltConnectionPoolTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
use Generator;
1717
use Laudis\Neo4j\Authentication\Authenticate;
18+
use Laudis\Neo4j\Bolt\BoltConnection;
1819
use Laudis\Neo4j\Bolt\ConnectionPool;
20+
use Laudis\Neo4j\BoltFactory;
1921
use Laudis\Neo4j\Common\GeneratorHelper;
2022
use Laudis\Neo4j\Common\Uri;
21-
use Laudis\Neo4j\Contracts\ConnectionFactoryInterface;
2223
use Laudis\Neo4j\Contracts\ConnectionInterface;
2324
use Laudis\Neo4j\Contracts\SemaphoreInterface;
2425
use Laudis\Neo4j\Databags\ConnectionRequestData;
@@ -34,8 +35,8 @@ class BoltConnectionPoolTest extends TestCase
3435
private ConnectionPool $pool;
3536
/** @var SemaphoreInterface&MockObject */
3637
private SemaphoreInterface $semaphore;
37-
/** @var ConnectionFactoryInterface&MockObject */
38-
private ConnectionFactoryInterface $factory;
38+
/** @var BoltFactory&MockObject */
39+
private BoltFactory $factory;
3940

4041
protected function setUp(): void
4142
{
@@ -142,9 +143,9 @@ private function setupPool(Generator $semaphoreGenerator): void
142143
$this->semaphore->method('wait')
143144
->willReturn($semaphoreGenerator);
144145

145-
$this->factory = $this->createMock(ConnectionFactoryInterface::class);
146+
$this->factory = $this->createMock(BoltFactory::class);
146147
$this->factory->method('createConnection')
147-
->willReturn($this->createMock(ConnectionInterface::class));
148+
->willReturn($this->createMock(BoltConnection::class));
148149

149150
$this->pool = new ConnectionPool(
150151
$this->semaphore, $this->factory, new ConnectionRequestData(
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Neo4j PHP Client and Driver package.
7+
*
8+
* (c) Nagels <https://nagels.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Tests\Unit;
15+
16+
use Laudis\Neo4j\Bolt\SslConfigurationFactory;
17+
use Laudis\Neo4j\Common\Uri;
18+
use Laudis\Neo4j\Databags\SslConfiguration;
19+
use Laudis\Neo4j\Enum\SslMode;
20+
use PHPUnit\Framework\TestCase;
21+
22+
/**
23+
* @psalm-suppress PossiblyUndefinedArrayOffset
24+
*/
25+
final class SslConfigurationFactoryTest extends TestCase
26+
{
27+
private SslConfigurationFactory $factory;
28+
29+
protected function setUp(): void
30+
{
31+
$this->factory = new SslConfigurationFactory();
32+
}
33+
34+
public function testDisablePeerVerification(): void
35+
{
36+
$config = SslConfiguration::default()
37+
->withVerifyPeer(false)
38+
->withMode(SslMode::ENABLE());
39+
40+
$uri = Uri::create('');
41+
$array = $this->factory->create($uri, $config);
42+
43+
self::assertArrayHasKey('verify_peer', $array[1]);
44+
self::assertFalse($array[1]['verify_peer']);
45+
self::assertArrayNotHasKey('allow_self_signed', $array[1]);
46+
self::assertEquals('s', $array[0]);
47+
}
48+
49+
public function testEnablePeerVerification(): void
50+
{
51+
$config = SslConfiguration::default()
52+
->withVerifyPeer(true)
53+
->withMode(SslMode::ENABLE());
54+
55+
$uri = Uri::create('');
56+
$array = $this->factory->create($uri, $config);
57+
58+
self::assertNotEmpty($array[1]);
59+
self::assertArrayHasKey('verify_peer', $array[1]);
60+
self::assertArrayNotHasKey('allow_self_signed', $array[1]);
61+
self::assertTrue($array[1]['verify_peer']);
62+
self::assertEquals('s', $array[0]);
63+
}
64+
65+
public function testSelfSigned(): void
66+
{
67+
$config = SslConfiguration::default()
68+
->withVerifyPeer(true)
69+
->withMode(SslMode::ENABLE_WITH_SELF_SIGNED());
70+
71+
$uri = Uri::create('');
72+
$array = $this->factory->create($uri, $config);
73+
74+
self::assertNotEmpty($array[1]);
75+
self::assertArrayHasKey('allow_self_signed', $array[1]);
76+
self::assertTrue($array[1]['allow_self_signed']);
77+
self::assertEquals('ssc', $array[0]);
78+
}
79+
80+
public function testFromUriNull(): void
81+
{
82+
$config = SslConfiguration::default()
83+
->withVerifyPeer(true)
84+
->withMode(SslMode::FROM_URL());
85+
86+
$uri = Uri::create('neo4j://localhost');
87+
$array = $this->factory->create($uri, $config);
88+
89+
self::assertEmpty($array[1]);
90+
self::assertEquals('', $array[0]);
91+
}
92+
93+
public function testFromUri(): void
94+
{
95+
$config = SslConfiguration::default()
96+
->withMode(SslMode::FROM_URL());
97+
98+
$uri = Uri::create('neo4j+s://localhost');
99+
$array = $this->factory->create($uri, $config);
100+
101+
self::assertNotEmpty($array[1]);
102+
self::assertArrayHasKey('verify_peer', $array[1]);
103+
self::assertArrayNotHasKey('allow_self_signed', $array[1]);
104+
self::assertTrue($array[1]['verify_peer']);
105+
self::assertEquals('s', $array[0]);
106+
}
107+
}

0 commit comments

Comments
 (0)