Skip to content

Commit 8642a4a

Browse files
authored
Merge pull request #17 from laudis-technologies/testing
Closes #16
2 parents 51d270e + e98e611 commit 8642a4a

File tree

4 files changed

+125
-14
lines changed

4 files changed

+125
-14
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ composer require nyholm/psr7 nyholm/psr7-server kriswallsmith/buzz
2828
```php
2929
$client = Laudis\Neo4j\ClientBuilder::create()
3030
->addHttpConnection('backup', 'http://neo4j:password@localhost')
31-
->addBoltConnection('default', 'neo4j:password@localhost')
31+
->addBoltConnection('default', 'bolt://neo4j:password@localhost')
3232
->setDefaultConnection('default')
3333
->build();
3434
```
@@ -180,8 +180,10 @@ This library does not use any custom result classes but uses php-ds instead. The
180180
Flexibility is maintained where possible by making all parameters iterables if they are a container of sorts. This means you can pass parameters as an array, \Ds\Map or any other object which implements the \Iterator or \IteratorAggregate. These examples are all valid:
181181

182182
```php
183+
use Ds\Map;
184+
183185
// Vanilla flavour
184-
use Ds\Map;$client->run('MATCH (x {slug: $slug})', ['slug' => 'a']);
186+
$client->run('MATCH (x {slug: $slug})', ['slug' => 'a']);
185187
// php-ds implementation
186188
$client->run('MATCH (x {slug: $slug})', new Map(['slug' => 'a']));
187189
// laravel style
@@ -192,7 +194,6 @@ $client->run('MATCH (x {slug: $slug})', collect(['slug' => 'a']));
192194

193195
| **Version** | **Tested** |
194196
|-------------|-------------|
195-
| 2.3 | Yes |
196197
| 3.0 + | Yes |
197198
| 4.0 + | Yes |
198199

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
}

src/Network/Bolt/BoltSession.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public function openTransaction(iterable $statements = null): TransactionInterfa
9292
$sock = new StreamSocket($this->parsedUrl['host'], $this->parsedUrl['port'] ?? self::DEFAULT_TCP_PORT);
9393
$bolt = new Bolt($sock);
9494
$bolt->init($userAgent, $this->parsedUrl['user'], $this->parsedUrl['pass']);
95-
if (!$bolt->begin()) {
95+
$extra = ['db' => $this->injections->database()];
96+
if (!$bolt->begin($extra)) {
9697
throw new Neo4jException(new Vector([new Neo4jError('', 'Cannot open new transaction')]));
9798
}
9899
} catch (Exception $e) {

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)