Skip to content

Commit e98e611

Browse files
committed
stricter checks when opening a connection
1 parent 987d390 commit e98e611

File tree

2 files changed

+119
-10
lines changed

2 files changed

+119
-10
lines changed

src/ClientBuilder.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ public static function create(): ClientBuilder
4848
*/
4949
public function addBoltConnection(string $alias, string $url, BoltInjections $provider = null): ClientBuilder
5050
{
51-
$parse = parse_url($url);
52-
if (!isset($parse['host'], $parse['user'], $parse['pass'])) {
53-
throw new InvalidArgumentException('The provided url must have a parsed host, user and pass value');
54-
}
51+
$parse = $this->assertCorrectUrl($url);
5552
$this->connectionPool->put($alias, new BoltDriver($parse, $provider ?? new BoltInjections()));
5653

5754
return $this;
@@ -62,10 +59,7 @@ public function addBoltConnection(string $alias, string $url, BoltInjections $pr
6259
*/
6360
public function addHttpConnection(string $alias, string $url, HttpInjections $injections = null): ClientBuilder
6461
{
65-
$parse = parse_url($url);
66-
if (!isset($parse['host'], $parse['user'], $parse['pass'])) {
67-
throw new InvalidArgumentException('The provided url must have a parsed host, user and pass value');
68-
}
62+
$parse = $this->assertCorrectUrl($url);
6963
$injections = $injections ?? new HttpInjections();
7064
$factory = $injections->requestFactory();
7165
$requestFactory = new RequestFactory($factory, $injections->streamFactory(), new HttpCypherFormatter());
@@ -100,4 +94,17 @@ public function build(): ClientInterface
10094

10195
return new Client($this->connectionPool, $this->default);
10296
}
97+
98+
/**
99+
* @return array{host:string, user:string, pass:string, scheme:string}
100+
*/
101+
private function assertCorrectUrl(string $url): array
102+
{
103+
$parse = parse_url($url);
104+
if (!isset($parse['host'], $parse['user'], $parse['pass'], $parse['scheme'])) {
105+
throw new InvalidArgumentException('The provided url must have a parsed host, user, pass and scheme value');
106+
}
107+
108+
return $parse;
109+
}
103110
}

tests/Unit/ClientBuilderTest.php

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
namespace Laudis\Neo4j\Tests\Unit;
1515

1616
use BadMethodCallException;
17+
use Buzz\Exception\NetworkException;
1718
use InvalidArgumentException;
1819
use Laudis\Neo4j\ClientBuilder;
20+
use Laudis\Neo4j\Exception\Neo4jException;
21+
use Laudis\Neo4j\Network\Bolt\BoltInjections;
22+
use Laudis\Neo4j\Network\Http\HttpInjections;
1923
use PHPUnit\Framework\TestCase;
2024

2125
final class ClientBuilderTest extends TestCase
@@ -41,7 +45,7 @@ public function testBadDefault(): void
4145
public function testBadHttpUrl(): void
4246
{
4347
$this->expectException(InvalidArgumentException::class);
44-
$this->expectExceptionMessage('The provided url must have a parsed host, user and pass value');
48+
$this->expectExceptionMessage('The provided url must have a parsed host, user, pass and scheme value');
4549

4650
ClientBuilder::create()
4751
->addHttpConnection('temp', 'neoj:test');
@@ -50,9 +54,107 @@ public function testBadHttpUrl(): void
5054
public function testBadBoltUrl(): void
5155
{
5256
$this->expectException(InvalidArgumentException::class);
53-
$this->expectExceptionMessage('The provided url must have a parsed host, user and pass value');
57+
$this->expectExceptionMessage('The provided url must have a parsed host, user, pass and scheme value');
5458

5559
ClientBuilder::create()
5660
->addBoltConnection('temp', 'neoj:test');
5761
}
62+
63+
public function testBoltSetupNoScheme(): void
64+
{
65+
$this->expectException(InvalidArgumentException::class);
66+
$client = ClientBuilder::create()->addBoltConnection('bolt', 'neo4j:test@neo4j-42:7687')->build();
67+
$client->openTransaction();
68+
}
69+
70+
public function testBoltSetupWithScheme(): void
71+
{
72+
$client = ClientBuilder::create()->addBoltConnection('bolt', 'bolt://neo4j:test@neo4j-42:7687')->build();
73+
$client->openTransaction();
74+
self::assertTrue(true);
75+
}
76+
77+
public function testBoltSetupWithoutPort(): void
78+
{
79+
$client = ClientBuilder::create()->addBoltConnection('bolt', 'bolt://neo4j:test@neo4j-42')->build();
80+
$client->openTransaction();
81+
self::assertTrue(true);
82+
}
83+
84+
public function testBoltSetupWithoutUserAndPass(): void
85+
{
86+
$this->expectException(InvalidArgumentException::class);
87+
$client = ClientBuilder::create()->addBoltConnection('bolt', 'bolt://@neo4j-42')->build();
88+
$client->openTransaction();
89+
}
90+
91+
public function testBoltEmpty(): void
92+
{
93+
$this->expectException(InvalidArgumentException::class);
94+
$client = ClientBuilder::create()->addBoltConnection('bolt', '')->build();
95+
$client->openTransaction();
96+
}
97+
98+
public function testBoltSetupWrongScheme(): void
99+
{
100+
$client = ClientBuilder::create()->addBoltConnection('bolt', 'neo4j://neo4j:test@neo4j-42:7687')->build();
101+
$client->openTransaction();
102+
self::assertTrue(true);
103+
}
104+
105+
public function testHttpSetupNoScheme(): void
106+
{
107+
$this->expectException(InvalidArgumentException::class);
108+
$client = ClientBuilder::create()->addHttpConnection('http', 'test:neo4j@neo4j-42:7474')->build();
109+
$client->openTransaction();
110+
}
111+
112+
public function testHttpSetupWithScheme(): void
113+
{
114+
$client = ClientBuilder::create()->addHttpConnection('http', 'http://neo4j:test@neo4j-42:7474')->build();
115+
$client->openTransaction();
116+
self::assertTrue(true);
117+
}
118+
119+
public function testHttpSetupWrongScheme(): void
120+
{
121+
$client = ClientBuilder::create()->addHttpConnection('http', 'neo4j://neo4j:test@neo4j-42:7474')->build();
122+
$this->expectException(NetworkException::class);
123+
$client->openTransaction();
124+
}
125+
126+
public function testHttpSetupWithoutPort(): void
127+
{
128+
$client = ClientBuilder::create()->addHttpConnection('http', 'http://neo4j:test@neo4j-42')->build();
129+
$client->openTransaction();
130+
self::assertTrue(true);
131+
}
132+
133+
public function testHttpSetupWithoutUserAndPass(): void
134+
{
135+
$this->expectException(InvalidArgumentException::class);
136+
$client = ClientBuilder::create()->addHttpConnection('http', 'http://@neo4j-42')->build();
137+
$client->openTransaction();
138+
}
139+
140+
public function testHttpEmpty(): void
141+
{
142+
$this->expectException(InvalidArgumentException::class);
143+
$client = ClientBuilder::create()->addHttpConnection('http', '')->build();
144+
$client->openTransaction();
145+
}
146+
147+
public function testHttpWithDatabase(): void
148+
{
149+
$client = ClientBuilder::create()->addHttpConnection('http', 'http://neo4j:test@neo4j-42', HttpInjections::create()->withDatabase('abc'))->build();
150+
$this->expectException(Neo4jException::class);
151+
$client->openTransaction();
152+
}
153+
154+
public function testBoltWithDatabase(): void
155+
{
156+
$client = ClientBuilder::create()->addBoltConnection('bolt', 'bolt://neo4j:test@neo4j-42', BoltInjections::create()->withDatabase('abc'))->build();
157+
$this->expectException(Neo4jException::class);
158+
$client->openTransaction();
159+
}
58160
}

0 commit comments

Comments
 (0)