Skip to content

Commit 1d62ec1

Browse files
committed
wip test_tx_run of testkit
1 parent 344f1a1 commit 1d62ec1

14 files changed

+131
-60
lines changed

src/Common/ResponseHelper.php

Lines changed: 0 additions & 37 deletions
This file was deleted.

testkit-backend/bookmarkHolder

Whitespace-only changes.

testkit-backend/bookmarkHolder,

Whitespace-only changes.

testkit-backend/connection,

Whitespace-only changes.

testkit-backend/database,

Whitespace-only changes.

testkit-backend/src/Handlers/AbstractRunner.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313

1414
namespace Laudis\Neo4j\TestkitBackend\Handlers;
1515

16+
use Exception;
1617
use Laudis\Neo4j\Contracts\SessionInterface;
1718
use Laudis\Neo4j\Contracts\TransactionInterface;
1819
use Laudis\Neo4j\Databags\SummarizedResult;
1920
use Laudis\Neo4j\Databags\TransactionConfiguration;
2021
use Laudis\Neo4j\Exception\Neo4jException;
22+
use Laudis\Neo4j\Exception\TransactionException;
2123
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
2224
use Laudis\Neo4j\TestkitBackend\MainRepository;
2325
use Laudis\Neo4j\TestkitBackend\Requests\SessionRunRequest;
26+
use Laudis\Neo4j\TestkitBackend\Requests\TransactionRunRequest;
2427
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
2528
use Laudis\Neo4j\TestkitBackend\Responses\ResultResponse;
2629
use Laudis\Neo4j\Types\AbstractCypherObject;
@@ -47,7 +50,7 @@ public function __construct(MainRepository $repository, LoggerInterface $logger)
4750
$this->logger = $logger;
4851
}
4952

50-
public function handle($request): ResultResponse
53+
public function handle($request): ResultResponse|DriverErrorResponse
5154
{
5255
$session = $this->getRunner($request);
5356
$id = Uuid::v4();
@@ -77,16 +80,24 @@ public function handle($request): ResultResponse
7780

7881
$this->repository->addRecords($id, $result);
7982

80-
return new ResultResponse($id, $result->isEmpty() ? [] : $result->first()->keys());
83+
return new ResultResponse($id, $result->keys());
8184
} catch (Neo4jException $exception) {
82-
$this->logger->debug($exception->__toString());
83-
$this->repository->addRecords($id, new DriverErrorResponse(
84-
$this->getId($request),
85-
$exception
86-
));
87-
88-
return new ResultResponse($id, []);
89-
} // NOTE: all other exceptions will be caught in the Backend
85+
if ($request instanceof SessionRunRequest) {
86+
return new DriverErrorResponse($request->getSessionId(), $exception);
87+
}
88+
if ($request instanceof TransactionRunRequest) {
89+
return new DriverErrorResponse($request->getTxId(), $exception);
90+
}
91+
92+
throw new Exception('Unhandled neo4j exception for run request of type: '.get_class($request));
93+
} catch (TransactionException $exception) {
94+
if ($request instanceof TransactionRunRequest) {
95+
return new DriverErrorResponse($request->getTxId(), $exception);
96+
}
97+
98+
throw new Exception('Unhandled neo4j exception for run request of type: '.get_class($request));
99+
}
100+
// NOTE: all other exceptions will be caught in the Backend
90101
}
91102

92103
/**

testkit-backend/src/Handlers/SessionBeginTransaction.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
use Laudis\Neo4j\TestkitBackend\Requests\SessionBeginTransactionRequest;
2222
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
2323
use Laudis\Neo4j\TestkitBackend\Responses\TransactionResponse;
24+
use Laudis\Neo4j\Types\AbstractCypherObject;
25+
use Laudis\Neo4j\Types\CypherList;
26+
use Laudis\Neo4j\Types\CypherMap;
2427
use Psr\Log\LoggerInterface;
2528
use Symfony\Component\Uid\Uuid;
2629

@@ -52,7 +55,14 @@ public function handle($request): TestkitResponseInterface
5255
}
5356

5457
if ($request->getTxMeta()) {
55-
$config = $config->withMetaData($request->getTxMeta());
58+
$metaData = $request->getTxMeta();
59+
$actualMeta = [];
60+
if ($metaData !== null) {
61+
foreach ($metaData as $key => $meta) {
62+
$actualMeta[$key] = $this->decodeToValue($meta);
63+
}
64+
}
65+
$config = $config->withMetaData($actualMeta);
5666
}
5767

5868
// TODO - Create beginReadTransaction and beginWriteTransaction
@@ -70,4 +80,45 @@ public function handle($request): TestkitResponseInterface
7080

7181
return new TransactionResponse($id);
7282
}
83+
84+
/**
85+
* @param array{name: string, data: array{value: iterable|scalar|null}} $param
86+
*
87+
* @return scalar|AbstractCypherObject|iterable|null
88+
*/
89+
private function decodeToValue(array $param)
90+
{
91+
$value = $param['data']['value'];
92+
if (is_iterable($value)) {
93+
if ($param['name'] === 'CypherMap') {
94+
/** @psalm-suppress MixedArgumentTypeCoercion */
95+
$map = [];
96+
/**
97+
* @var numeric $k
98+
* @var mixed $v
99+
*/
100+
foreach ($value as $k => $v) {
101+
/** @psalm-suppress MixedArgument */
102+
$map[(string) $k] = $this->decodeToValue($v);
103+
}
104+
105+
return new CypherMap($map);
106+
}
107+
108+
if ($param['name'] === 'CypherList') {
109+
$list = [];
110+
/**
111+
* @var mixed $v
112+
*/
113+
foreach ($value as $v) {
114+
/** @psalm-suppress MixedArgument */
115+
$list[] = $this->decodeToValue($v);
116+
}
117+
118+
return new CypherList($list);
119+
}
120+
}
121+
122+
return $value;
123+
}
73124
}

testkit-backend/src/Handlers/TransactionCommit.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515

1616
use Laudis\Neo4j\Contracts\UnmanagedTransactionInterface;
1717
use Laudis\Neo4j\Exception\Neo4jException;
18+
use Laudis\Neo4j\Exception\TransactionException;
1819
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
1920
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
2021
use Laudis\Neo4j\TestkitBackend\MainRepository;
2122
use Laudis\Neo4j\TestkitBackend\Requests\TransactionCommitRequest;
2223
use Laudis\Neo4j\TestkitBackend\Responses\BackendErrorResponse;
2324
use Laudis\Neo4j\TestkitBackend\Responses\DriverErrorResponse;
2425
use Laudis\Neo4j\TestkitBackend\Responses\TransactionResponse;
26+
use Throwable;
2527

2628
/**
2729
* @implements RequestHandlerInterface<TransactionCommitRequest>
@@ -48,7 +50,7 @@ public function handle($request): TestkitResponseInterface
4850

4951
try {
5052
$tsx->commit();
51-
} catch (Neo4jException $e) {
53+
} catch (Neo4jException|TransactionException $e) {
5254
return new DriverErrorResponse($request->getTxId(), $e);
5355
}
5456

testkit-backend/src/Handlers/TransactionRollback.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Laudis\Neo4j\Contracts\TransactionInterface;
1717
use Laudis\Neo4j\Exception\Neo4jException;
18+
use Laudis\Neo4j\Exception\TransactionException;
1819
use Laudis\Neo4j\TestkitBackend\Contracts\RequestHandlerInterface;
1920
use Laudis\Neo4j\TestkitBackend\Contracts\TestkitResponseInterface;
2021
use Laudis\Neo4j\TestkitBackend\MainRepository;
@@ -48,7 +49,7 @@ public function handle($request): TestkitResponseInterface
4849

4950
try {
5051
$tsx->rollback();
51-
} catch (Neo4jException $e) {
52+
} catch (Neo4jException|TransactionException $e) {
5253
return new DriverErrorResponse($request->getTxId(), $e);
5354
}
5455

0 commit comments

Comments
 (0)