Skip to content

Commit 5408df8

Browse files
exaby73transistive
authored andcommitted
feat: Fix tests
1 parent 6fcbb06 commit 5408df8

File tree

4 files changed

+72
-70
lines changed

4 files changed

+72
-70
lines changed

src/Bolt/BoltConnection.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public function setTimeout(float $timeout): void
151151

152152
public function consumeResults(): void
153153
{
154+
if ($this->protocol()->serverState !== ServerState::STREAMING || $this->protocol()->serverState !== ServerState::TX_STREAMING) {
155+
$this->subscribedResults = [];
156+
return;
157+
}
158+
154159
foreach ($this->subscribedResults as $result) {
155160
$result = $result->get();
156161
if ($result) {
@@ -185,10 +190,6 @@ public function begin(?string $database, ?float $timeout, BookmarkHolder $holder
185190
{
186191
$this->consumeResults();
187192

188-
if ($this->protocol()->serverState !== ServerState::READY) {
189-
throw new Neo4jException([Neo4jError::fromMessageAndCode('Neo.ClientError.Request.Invalid', 'Message \'BEGIN\' cannot be handled by a session which isn\'t in the READY state.')]);
190-
}
191-
192193
$extra = $this->buildRunExtra($database, $timeout, $holder, AccessMode::WRITE());
193194
$response = $this->protocol()
194195
->begin($extra)
@@ -203,10 +204,6 @@ public function begin(?string $database, ?float $timeout, BookmarkHolder $holder
203204
*/
204205
public function discard(?int $qid): void
205206
{
206-
if (!in_array($this->protocol()->serverState, [ServerState::STREAMING, ServerState::TX_STREAMING], true)) {
207-
throw new Neo4jException([Neo4jError::fromMessageAndCode('Neo.ClientError.Request.Invalid', 'Message \'DISCARD\' cannot be handled by a session which isn\'t in the STREAMING|TX_STREAMING state.')]);
208-
}
209-
210207
$extra = $this->buildResultExtra(null, $qid);
211208
$response = $this->protocol()
212209
->discard($extra)
@@ -223,10 +220,6 @@ public function discard(?int $qid): void
223220
*/
224221
public function run(string $text, array $parameters, ?string $database, ?float $timeout, BookmarkHolder $holder, ?AccessMode $mode): array
225222
{
226-
if (!in_array($this->protocol()->serverState, [ServerState::READY, ServerState::TX_READY, ServerState::TX_STREAMING], true)) {
227-
throw new Neo4jException([Neo4jError::fromMessageAndCode('Neo.ClientError.Request.Invalid', 'Message \'RUN\' cannot be handled by a session which isn\'t in the READY|TX_READY|TX_STREAMING state.')]);
228-
}
229-
230223
$extra = $this->buildRunExtra($database, $timeout, $holder, $mode);
231224
$response = $this->protocol()
232225
->run($text, $parameters, $extra)
@@ -260,10 +253,6 @@ public function rollback(): void
260253
{
261254
$this->consumeResults();
262255

263-
if ($this->protocol()->serverState !== ServerState::TX_READY) {
264-
throw new Neo4jException([Neo4jError::fromMessageAndCode('Neo.ClientError.Request.Invalid', 'Message \'ROLLBACK\' cannot be handled by a session which isn\'t in the TX_READY state.')]);
265-
}
266-
267256
$response = $this->protocol()
268257
->rollback()
269258
->getResponse();
@@ -284,10 +273,6 @@ public function protocol(): V4_4|V5|V5_1|V5_2|V5_3|V5_4
284273
*/
285274
public function pull(?int $qid, ?int $fetchSize): array
286275
{
287-
if (!in_array($this->protocol()->serverState, [ServerState::STREAMING, ServerState::TX_STREAMING], true)) {
288-
throw new Neo4jException([Neo4jError::fromMessageAndCode('Neo.ClientError.Request.Invalid', 'Message \'PULL\' cannot be handled by a session which isn\'t in the STREAMING|TX_STREAMING state.')]);
289-
}
290-
291276
$extra = $this->buildResultExtra($fetchSize, $qid);
292277

293278
$tbr = [];

src/Bolt/BoltResult.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Laudis\Neo4j\Bolt;
1515

16+
use Bolt\enum\ServerState;
1617
use function array_splice;
1718
use function count;
1819

@@ -148,6 +149,10 @@ public function __destruct()
148149

149150
public function discard(): void
150151
{
152+
$serverState = $this->connection->protocol()->serverState;
153+
if ($serverState !== ServerState::STREAMING || $serverState !== ServerState::TX_STREAMING) {
154+
return;
155+
}
151156
$this->connection->discard($this->qid === -1 ? null : $this->qid);
152157
}
153158
}

src/Bolt/BoltUnmanagedTransaction.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ public function commit(iterable $statements = []): CypherList
9595

9696
public function rollback(): void
9797
{
98+
if ($this->isFinished()) {
99+
switch ($this->state) {
100+
case TransactionState::TERMINATED:
101+
throw new ClientException("Can't rollback, transaction has been terminated");
102+
case TransactionState::COMMITTED:
103+
throw new ClientException("Can't rollback, transaction has already been committed");
104+
case TransactionState::ROLLED_BACK:
105+
throw new ClientException("Can't rollback, transaction has already been rolled back");
106+
default:
107+
}
108+
}
109+
98110
$this->connection->rollback();
99111
$this->state = TransactionState::ROLLED_BACK;
100112
}
@@ -180,7 +192,7 @@ private function handleMessageException(Neo4jException $e): never
180192

181193
public function isRolledBack(): bool
182194
{
183-
return $this->state == TransactionState::ROLLED_BACK;
195+
return $this->state === TransactionState::ROLLED_BACK || $this->state === TransactionState::TERMINATED;
184196
}
185197

186198
public function isCommitted(): bool

tests/Integration/TransactionIntegrationTest.php

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Laudis\Neo4j\Tests\Integration;
1515

1616
use Laudis\Neo4j\Databags\Statement;
17+
use Laudis\Neo4j\Exception\ClientException;
1718
use Laudis\Neo4j\Exception\Neo4jException;
1819
use Laudis\Neo4j\Tests\EnvironmentAwareIntegrationTest;
1920
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
@@ -213,61 +214,60 @@ public function testCommitValidFilledWithInvalidStatement(): void
213214
}
214215

215216
// TODO commit on READY state cause stuck neo4j connection on older version and disconnect at newer
216-
// public function testCommitInvalid(): void
217-
// {
218-
// $tsx = $this->getSession()->beginTransaction();
219-
// $tsx->commit();
220-
//
221-
// self::assertTrue($tsx->isFinished());
222-
// self::assertFalse($tsx->isRolledBack());
223-
// self::assertTrue($tsx->isCommitted());
224-
//
225-
// $exception = false;
226-
// try {
227-
// $tsx->commit();
228-
// } catch (Throwable) {
229-
// $exception = true;
230-
// }
231-
// self::assertTrue($exception);
232-
//
233-
// self::assertTrue($tsx->isFinished());
234-
// self::assertTrue($tsx->isRolledBack());
235-
// self::assertFalse($tsx->isCommitted());
236-
// }
217+
public function testCommitInvalid(): void
218+
{
219+
$tsx = $this->getSession()->beginTransaction();
220+
$tsx->commit();
221+
222+
self::assertTrue($tsx->isFinished());
223+
self::assertFalse($tsx->isRolledBack());
224+
self::assertTrue($tsx->isCommitted());
225+
226+
$exception = null;
227+
try {
228+
$tsx->commit();
229+
} catch (ClientException $e) {
230+
$exception = $e;
231+
}
232+
self::assertTrue($exception instanceof ClientException);
233+
234+
self::assertTrue($tsx->isFinished());
235+
self::assertFalse($tsx->isRolledBack());
236+
self::assertTrue($tsx->isCommitted());
237+
}
237238

238239
public function testRollbackValid(): void
239240
{
240-
$this->markTestSkipped('Skipped due to ConnectionTimeoutException');
241-
// $tsx = $this->getSession()->beginTransaction();
242-
// $tsx->rollback();
243-
//
244-
// self::assertTrue($tsx->isFinished());
245-
// self::assertTrue($tsx->isRolledBack());
246-
// self::assertFalse($tsx->isCommitted());
241+
$tsx = $this->getSession()->beginTransaction();
242+
$tsx->rollback();
243+
244+
self::assertTrue($tsx->isFinished());
245+
self::assertTrue($tsx->isRolledBack());
246+
self::assertFalse($tsx->isCommitted());
247247
}
248248

249249
// TODO rollback on READY state cause stuck neo4j connection on older version and disconnect at newer
250-
// public function testRollbackInvalid(): void
251-
// {
252-
// $tsx = $this->getSession()->beginTransaction();
253-
// $tsx->rollback();
254-
//
255-
// self::assertTrue($tsx->isFinished());
256-
// self::assertTrue($tsx->isRolledBack());
257-
// self::assertFalse($tsx->isCommitted());
258-
//
259-
// $exception = false;
260-
// try {
261-
// $tsx->rollback();
262-
// } catch (Throwable) {
263-
// $exception = true;
264-
// }
265-
// self::assertTrue($exception);
266-
//
267-
// self::assertTrue($tsx->isFinished());
268-
// self::assertTrue($tsx->isRolledBack());
269-
// self::assertFalse($tsx->isCommitted());
270-
// }
250+
public function testRollbackInvalid(): void
251+
{
252+
$tsx = $this->getSession()->beginTransaction();
253+
$tsx->rollback();
254+
255+
self::assertTrue($tsx->isFinished());
256+
self::assertTrue($tsx->isRolledBack());
257+
self::assertFalse($tsx->isCommitted());
258+
259+
$exception = null;
260+
try {
261+
$tsx->rollback();
262+
} catch (ClientException $e) {
263+
$exception = $e;
264+
}
265+
self::assertTrue($exception instanceof ClientException);
266+
267+
self::assertTrue($tsx->isFinished());
268+
self::assertTrue($tsx->isRolledBack());
269+
self::assertFalse($tsx->isCommitted());
270+
}
271271

272272
// /**
273273
// * TODO - rework this test

0 commit comments

Comments
 (0)