Skip to content

Commit 30d31a7

Browse files
PratikshaPratiksha
authored andcommitted
updated the TransactionUnitTest
1 parent f256ec6 commit 30d31a7

File tree

6 files changed

+66
-56
lines changed

6 files changed

+66
-56
lines changed

src/Exception/Neo4jException.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __construct(
3838
*/
3939
public static function fromNeo4jResponse(array $response, ?\Throwable $exception = null): self
4040
{
41+
4142
$errorDetails = $response['errors'][0] ?? ['message' => 'Unknown error', 'code' => 'Neo.UnknownError'];
4243

4344

src/Objects/ResultCounters.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,4 @@ public function __construct(
2525
) {
2626
}
2727

28-
29-
30-
3128
}

src/ResponseParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Neo4j\QueryAPI\Objects\ProfiledQueryPlan;
1515
use Neo4j\QueryAPI\Objects\Point;
1616

17-
final class ResponseParser
17+
class ResponseParser
1818
{
1919
public function __construct(private readonly OGM $ogm)
2020
{

tests/Integration/Neo4jQueryAPIIntegrationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Neo4j\QueryAPI\OGM;
1919
use Neo4j\QueryAPI\Results\ResultRow;
2020
use Neo4j\QueryAPI\Results\ResultSet;
21-
2221
use PHPUnit\Framework\TestCase;
2322
use Neo4j\QueryAPI\Enums\AccessMode;
2423
use Neo4j\QueryAPI\ResponseParser;

tests/Unit/Neo4jQueryAPIUnitTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use RuntimeException;
2222
use Neo4j\QueryAPI\Configuration;
2323

24-
2524
/**
2625
* @api
2726
*/

tests/Unit/TransactionUnitTest.php

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,102 +2,116 @@
22

33
namespace Neo4j\QueryAPI\Tests\Unit;
44

5-
use Neo4j\QueryAPI\Results\ResultSet;
6-
use PHPUnit\Framework\MockObject\MockObject;
7-
use PHPUnit\Framework\TestCase;
85
use Neo4j\QueryAPI\Transaction;
9-
use Neo4j\QueryAPI\Neo4jRequestFactory;
6+
use Neo4j\QueryAPI\Exception\Neo4jException;
7+
use Neo4j\QueryAPI\Results\ResultSet;
108
use Neo4j\QueryAPI\ResponseParser;
9+
use Neo4j\QueryAPI\Neo4jRequestFactory;
10+
use PHPUnit\Framework\TestCase;
1111
use Psr\Http\Client\ClientInterface;
12-
use Psr\Http\Message\RequestInterface;
12+
use Psr\Http\Client\RequestExceptionInterface;
1313
use Psr\Http\Message\ResponseInterface;
14+
use Psr\Http\Message\RequestInterface;
15+
use Psr\Http\Message\StreamInterface;
1416

15-
/**
16-
* @api
17-
*/
1817
class TransactionUnitTest extends TestCase
1918
{
20-
private MockObject $client;
21-
private MockObject $requestFactory;
22-
private MockObject $responseParser;
2319
private Transaction $transaction;
24-
25-
private string $transactionId = 'txn123';
26-
private string $clusterAffinity = 'leader';
20+
private $clientMock;
21+
private $responseParserMock;
22+
private $requestFactoryMock;
23+
private $requestMock;
24+
private $responseMock;
25+
private string $transactionId = 'tx123';
26+
private string $clusterAffinity = 'LEADER';
2727

2828
#[\Override]
2929
protected function setUp(): void
3030
{
31-
$this->client = $this->createMock(ClientInterface::class);
32-
$this->requestFactory = $this->createMock(Neo4jRequestFactory::class);
33-
$this->responseParser = $this->createMock(ResponseParser::class);
31+
$this->clientMock = $this->createMock(ClientInterface::class);
32+
$this->responseParserMock = $this->createMock(ResponseParser::class);
33+
$this->requestFactoryMock = $this->createMock(Neo4jRequestFactory::class);
34+
$this->requestMock = $this->createMock(RequestInterface::class);
35+
$this->responseMock = $this->createMock(ResponseInterface::class);
3436

3537
$this->transaction = new Transaction(
36-
$this->client,
37-
$this->responseParser,
38-
$this->requestFactory,
38+
$this->clientMock,
39+
$this->responseParserMock,
40+
$this->requestFactoryMock,
3941
$this->clusterAffinity,
4042
$this->transactionId
4143
);
4244
}
4345

44-
public function testRunCallsBuildTransactionRunRequest(): void
46+
public function testRunExecutesQuerySuccessfully(): void
4547
{
46-
$query = "CREATE (:Person {name: \$name})";
47-
$parameters = ['name' => 'Alice'];
48+
$query = 'MATCH (n) RETURN n';
49+
$parameters = [];
50+
$resultSetMock = $this->createMock(ResultSet::class);
4851

49-
$mockRequest = $this->createMock(RequestInterface::class);
50-
$mockResponse = $this->createMock(ResponseInterface::class);
51-
$mockResultSet = $this->createMock(ResultSet::class);
52-
53-
$this->requestFactory->expects($this->once())
52+
$this->requestFactoryMock->expects($this->once())
5453
->method('buildTransactionRunRequest')
5554
->with($query, $parameters, $this->transactionId, $this->clusterAffinity)
56-
->willReturn($mockRequest);
55+
->willReturn($this->requestMock);
5756

58-
$this->client->expects($this->once())
57+
$this->clientMock->expects($this->once())
5958
->method('sendRequest')
60-
->with($mockRequest)
61-
->willReturn($mockResponse);
59+
->with($this->requestMock)
60+
->willReturn($this->responseMock);
6261

63-
$this->responseParser->expects($this->once())
62+
$this->responseParserMock->expects($this->once())
6463
->method('parseRunQueryResponse')
65-
->with($mockResponse)
66-
->willReturn($mockResultSet);
64+
->with($this->responseMock)
65+
->willReturn($resultSetMock);
6766

6867
$result = $this->transaction->run($query, $parameters);
69-
70-
$this->assertSame($mockResultSet, $result);
68+
$this->assertInstanceOf(ResultSet::class, $result);
7169
}
7270

73-
public function testCommitCallsBuildCommitRequest(): void
71+
72+
public function testHandleRequestExceptionWithoutResponse(): void
7473
{
75-
$mockRequest = $this->createMock(RequestInterface::class);
74+
$exceptionMock = $this->createMock(RequestExceptionInterface::class);
75+
76+
$reflection = new \ReflectionClass($exceptionMock);
77+
$property = $reflection->getParentClass()->getProperty('message');
78+
$property->setValue($exceptionMock, 'Request failed');
79+
80+
$this->expectException(Neo4jException::class);
81+
$this->expectExceptionMessage('Request failed');
82+
83+
$reflection = new \ReflectionClass($this->transaction);
84+
$method = $reflection->getMethod('handleRequestException');
85+
86+
$method->invoke($this->transaction, $exceptionMock);
87+
}
88+
89+
7690

77-
$this->requestFactory->expects($this->once())
91+
public function testCommitSendsCommitRequest(): void
92+
{
93+
$this->requestFactoryMock->expects($this->once())
7894
->method('buildCommitRequest')
7995
->with($this->transactionId, $this->clusterAffinity)
80-
->willReturn($mockRequest);
96+
->willReturn($this->requestMock);
8197

82-
$this->client->expects($this->once())
98+
$this->clientMock->expects($this->once())
8399
->method('sendRequest')
84-
->with($mockRequest);
100+
->with($this->requestMock);
85101

86102
$this->transaction->commit();
87103
}
88104

89-
public function testRollbackCallsBuildRollbackRequest(): void
105+
public function testRollbackSendsRollbackRequest(): void
90106
{
91-
$mockRequest = $this->createMock(RequestInterface::class);
92-
93-
$this->requestFactory->expects($this->once())
107+
$this->requestFactoryMock->expects($this->once())
94108
->method('buildRollbackRequest')
95109
->with($this->transactionId, $this->clusterAffinity)
96-
->willReturn($mockRequest);
110+
->willReturn($this->requestMock);
97111

98-
$this->client->expects($this->once())
112+
$this->clientMock->expects($this->once())
99113
->method('sendRequest')
100-
->with($mockRequest);
114+
->with($this->requestMock);
101115

102116
$this->transaction->rollback();
103117
}

0 commit comments

Comments
 (0)