Skip to content

Commit 323b47b

Browse files
committed
Fixes bug when sending statements when opening transaction in http
fixes #49
1 parent d8465a0 commit 323b47b

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/HttpDriver/RequestFactory.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@ public function __construct(RequestFactoryInterface $factory, StreamFactoryInter
3535
$this->formatter = $formatter;
3636
}
3737

38-
public function openTransaction(RequestData $data): RequestInterface
38+
/**
39+
* @param iterable<Statement> $statements
40+
*
41+
* @throws JsonException
42+
*/
43+
public function openTransaction(RequestData $data, iterable $statements): RequestInterface
3944
{
40-
return $this->createRequest($data, 'POST');
45+
$body = $this->formatter->prepareBody($statements, $data);
46+
47+
$request = $this->createRequest($data, 'POST');
48+
$request->getBody()->write($body);
49+
50+
return $request;
4151
}
4252

4353
public function createRequest(RequestData $data, string $method, string $body = ''): RequestInterface

src/Network/Http/HttpSession.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function run(iterable $statements): Vector
6666
*/
6767
public function openTransaction(iterable $statements = null): TransactionInterface
6868
{
69-
$request = $this->factory->openTransaction($this->data);
69+
$request = $this->factory->openTransaction($this->data, $statements ?? []);
7070
$response = $this->client->sendRequest($request);
7171
/** @var array{commit: string} $data */
7272
$data = $this->interpretResponse($response);

tests/Integration/HttpConsistencyTest.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Laudis\Neo4j\ClientBuilder;
99
use Laudis\Neo4j\Contracts\ClientInterface;
10+
use Laudis\Neo4j\Databags\Statement;
11+
use Laudis\Neo4j\Network\Bolt\BoltInjections;
1012
use PHPUnit\Framework\TestCase;
1113

1214
final class HttpConsistencyTest extends TestCase
@@ -17,19 +19,52 @@ protected function setUp(): void
1719
{
1820
parent::setUp();
1921
$this->client = ClientBuilder::create()
20-
->addHttpConnection('default', 'http://neo4j:test@neo4j')
21-
->setDefaultConnection('default')
22+
->addHttpConnection('http', 'http://neo4j:test@neo4j')
23+
->addBoltConnection('bolt', 'bolt://neo4j:test@neo4j')
24+
->addBoltConnection('neo4j', 'bolt://neo4j:test@neo4j', BoltInjections::create()->withAutoRouting(true))
2225
->build();
26+
27+
$this->client->run('MATCH (x) DETACH DELETE x');
2328
}
2429

25-
public function testConsistency(): void
30+
/**
31+
* @dataProvider aliases
32+
*/
33+
public function testConsistency(string $alias): void
2634
{
27-
$res = $this->client->run('MERGE (n:zzz {name: "bbbb"}) RETURN n');
35+
$res = $this->client->run('MERGE (n:zzz {name: "bbbb"}) RETURN n', [], $alias);
2836
self::assertEquals(1, $res->count());
2937
self::assertEquals(['name' => 'bbbb'], $res->first()->get('n'));
3038

31-
$res = $this->client->run('MATCH (n:zzz {name: $name}) RETURN n', ['name' => 'bbbb']);
39+
$res = $this->client->run('MATCH (n:zzz {name: $name}) RETURN n', ['name' => 'bbbb'], $alias);
3240
self::assertEquals(1, $res->count());
3341
self::assertEquals(['name' => 'bbbb'], $res->first()->get('n'));
3442
}
43+
44+
/**
45+
* @dataProvider aliases
46+
*/
47+
public function testConsistencyTransaction(string $alias): void
48+
{
49+
$tsx = $this->client->openTransaction([
50+
Statement::create('CREATE (n:aaa) SET n.name="aaa" return n')
51+
], $alias);
52+
53+
$tsx->commit([Statement::create('CREATE (n:bbb) SET n.name="bbb" return n')]);
54+
55+
$results = $this->client->run('MATCH (n) RETURN n', ['name' => 'bbbb'], $alias);
56+
57+
self::assertEquals(2, $results->count());
58+
self::assertEquals(['name' => 'aaa'], $results->first()->get('n'));
59+
self::assertEquals(['name' => 'bbb'], $results->last()->get('n'));
60+
}
61+
62+
public function aliases(): array
63+
{
64+
return [
65+
['http'],
66+
['bolt'],
67+
['neo4j']
68+
];
69+
}
3570
}

0 commit comments

Comments
 (0)