Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 71d7f36

Browse files
committed
fix issue #143
1 parent 47b9b97 commit 71d7f36

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

src/Client.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ public function stack($tag = null, $connectionAlias = null)
132132
*/
133133
public function runStack(StackInterface $stack)
134134
{
135-
$pipeline = $this->pipeline(null, null, $stack->getTag(), $stack->getConnectionAlias());
135+
$connectionAlias = $stack instanceof Stack && $stack->hasWrites()
136+
? $this->connectionManager->getMasterConnection()->getAlias()
137+
: $stack->getConnectionAlias();
138+
$pipeline = $this->pipeline(null, null, $stack->getTag(), $connectionAlias);
136139

137140
foreach ($stack->statements() as $statement) {
138141
$pipeline->push($statement->text(), $statement->parameters(), $statement->getTag());
@@ -151,7 +154,7 @@ public function runStack(StackInterface $stack)
151154
throw $e;
152155
}
153156

154-
return;
157+
return null;
155158
}
156159

157160
return $results;

src/Stack.php

+25
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ class Stack implements StackInterface
3535
*/
3636
protected $preflights = [];
3737

38+
/**
39+
* @var bool
40+
*/
41+
protected $hasWrites = false;
42+
3843
/**
3944
* @param null $tag
4045
* @param null|string $connectionAlias
@@ -67,6 +72,18 @@ public function push($query, $parameters = null, $tag = null)
6772
$this->statements[] = Statement::create($query, $params, $tag);
6873
}
6974

75+
/**
76+
* @param string $query
77+
* @param null|array $parameters
78+
* @param null|array $tag
79+
*/
80+
public function pushWrite($query, $parameters = null, $tag = null)
81+
{
82+
$params = null !== $parameters ? $parameters : [];
83+
$this->statements[] = Statement::create($query, $params, $tag);
84+
$this->hasWrites = true;
85+
}
86+
7087
/**
7188
* @param $query
7289
* @param array|null $parameters
@@ -125,4 +142,12 @@ public function getConnectionAlias()
125142
{
126143
return $this->connectionAlias;
127144
}
145+
146+
/**
147+
* @return bool
148+
*/
149+
public function hasWrites()
150+
{
151+
return $this->hasWrites;
152+
}
128153
}

tests/Integration/IntegrationTestCase.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class IntegrationTestCase extends \PHPUnit_Framework_TestCase
2121
protected $client;
2222

2323
public function setUp()
24+
{
25+
$connections = array_merge($this->getConnections(), $this->getAdditionalConnections());
26+
27+
$this->client = ClientBuilder::create()
28+
->addConnection('http', $connections['http'])
29+
->addConnection('bolt', $connections['bolt'])
30+
->build();
31+
}
32+
33+
protected function getConnections()
2434
{
2535
$httpUri = 'http://localhost:7474';
2636
if (isset($_ENV['NEO4J_USER'])) {
@@ -44,10 +54,15 @@ public function setUp()
4454
);
4555
}
4656

47-
$this->client = ClientBuilder::create()
48-
->addConnection('http', $httpUri)
49-
->addConnection('bolt', $boltUrl)
50-
->build();
57+
return [
58+
'http' => $httpUri,
59+
'bolt' => $boltUrl
60+
];
61+
}
62+
63+
protected function getAdditionalConnections()
64+
{
65+
return [];
5166
}
5267

5368
/**

tests/Issues/Issue143Test.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace GraphAware\Neo4j\Client\Tests\Issues;
4+
5+
use GraphAware\Neo4j\Client\ClientBuilder;
6+
use GraphAware\Neo4j\Client\Tests\Integration\IntegrationTestCase;
7+
8+
class Issue143Test extends IntegrationTestCase
9+
{
10+
public function setUp()
11+
{
12+
$connections = array_merge($this->getConnections(), $this->getAdditionalConnections());
13+
14+
$this->client = ClientBuilder::create()
15+
->addConnection('a', $connections['non-exist'])
16+
->addConnection('http', $connections['http'])
17+
->addConnection('bolt', $connections['bolt'])
18+
->setMaster('bolt')
19+
->build();
20+
}
21+
22+
protected function getAdditionalConnections()
23+
{
24+
return ['non-exist', 'bolt://error:7687'];
25+
}
26+
27+
28+
public function testStackUsesMasterForWritesWhenOneisSet()
29+
{
30+
for ($x = 0; $x < 1000; $x++) {
31+
$stack = $this->client->stack();
32+
$stack->pushWrite('CREATE (n)');
33+
$this->client->runStack($stack);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)