|
2 | 2 |
|
3 | 3 | namespace Neo4j\QueryAPI\Tests\Unit;
|
4 | 4 |
|
5 |
| -use Neo4j\QueryAPI\Results\ResultSet; |
6 |
| -use PHPUnit\Framework\MockObject\MockObject; |
7 |
| -use PHPUnit\Framework\TestCase; |
8 | 5 | use Neo4j\QueryAPI\Transaction;
|
9 |
| -use Neo4j\QueryAPI\Neo4jRequestFactory; |
| 6 | +use Neo4j\QueryAPI\Exception\Neo4jException; |
| 7 | +use Neo4j\QueryAPI\Results\ResultSet; |
10 | 8 | use Neo4j\QueryAPI\ResponseParser;
|
| 9 | +use Neo4j\QueryAPI\Neo4jRequestFactory; |
| 10 | +use PHPUnit\Framework\TestCase; |
11 | 11 | use Psr\Http\Client\ClientInterface;
|
12 |
| -use Psr\Http\Message\RequestInterface; |
| 12 | +use Psr\Http\Client\RequestExceptionInterface; |
13 | 13 | use Psr\Http\Message\ResponseInterface;
|
| 14 | +use Psr\Http\Message\RequestInterface; |
| 15 | +use Psr\Http\Message\StreamInterface; |
14 | 16 |
|
15 |
| -/** |
16 |
| - * @api |
17 |
| - */ |
18 | 17 | class TransactionUnitTest extends TestCase
|
19 | 18 | {
|
20 |
| - private MockObject $client; |
21 |
| - private MockObject $requestFactory; |
22 |
| - private MockObject $responseParser; |
23 | 19 | 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'; |
27 | 27 |
|
28 | 28 | #[\Override]
|
29 | 29 | protected function setUp(): void
|
30 | 30 | {
|
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); |
34 | 36 |
|
35 | 37 | $this->transaction = new Transaction(
|
36 |
| - $this->client, |
37 |
| - $this->responseParser, |
38 |
| - $this->requestFactory, |
| 38 | + $this->clientMock, |
| 39 | + $this->responseParserMock, |
| 40 | + $this->requestFactoryMock, |
39 | 41 | $this->clusterAffinity,
|
40 | 42 | $this->transactionId
|
41 | 43 | );
|
42 | 44 | }
|
43 | 45 |
|
44 |
| - public function testRunCallsBuildTransactionRunRequest(): void |
| 46 | + public function testRunExecutesQuerySuccessfully(): void |
45 | 47 | {
|
46 |
| - $query = "CREATE (:Person {name: \$name})"; |
47 |
| - $parameters = ['name' => 'Alice']; |
| 48 | + $query = 'MATCH (n) RETURN n'; |
| 49 | + $parameters = []; |
| 50 | + $resultSetMock = $this->createMock(ResultSet::class); |
48 | 51 |
|
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()) |
54 | 53 | ->method('buildTransactionRunRequest')
|
55 | 54 | ->with($query, $parameters, $this->transactionId, $this->clusterAffinity)
|
56 |
| - ->willReturn($mockRequest); |
| 55 | + ->willReturn($this->requestMock); |
57 | 56 |
|
58 |
| - $this->client->expects($this->once()) |
| 57 | + $this->clientMock->expects($this->once()) |
59 | 58 | ->method('sendRequest')
|
60 |
| - ->with($mockRequest) |
61 |
| - ->willReturn($mockResponse); |
| 59 | + ->with($this->requestMock) |
| 60 | + ->willReturn($this->responseMock); |
62 | 61 |
|
63 |
| - $this->responseParser->expects($this->once()) |
| 62 | + $this->responseParserMock->expects($this->once()) |
64 | 63 | ->method('parseRunQueryResponse')
|
65 |
| - ->with($mockResponse) |
66 |
| - ->willReturn($mockResultSet); |
| 64 | + ->with($this->responseMock) |
| 65 | + ->willReturn($resultSetMock); |
67 | 66 |
|
68 | 67 | $result = $this->transaction->run($query, $parameters);
|
69 |
| - |
70 |
| - $this->assertSame($mockResultSet, $result); |
| 68 | + $this->assertInstanceOf(ResultSet::class, $result); |
71 | 69 | }
|
72 | 70 |
|
73 |
| - public function testCommitCallsBuildCommitRequest(): void |
| 71 | + |
| 72 | + public function testHandleRequestExceptionWithoutResponse(): void |
74 | 73 | {
|
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 | + |
76 | 90 |
|
77 |
| - $this->requestFactory->expects($this->once()) |
| 91 | + public function testCommitSendsCommitRequest(): void |
| 92 | + { |
| 93 | + $this->requestFactoryMock->expects($this->once()) |
78 | 94 | ->method('buildCommitRequest')
|
79 | 95 | ->with($this->transactionId, $this->clusterAffinity)
|
80 |
| - ->willReturn($mockRequest); |
| 96 | + ->willReturn($this->requestMock); |
81 | 97 |
|
82 |
| - $this->client->expects($this->once()) |
| 98 | + $this->clientMock->expects($this->once()) |
83 | 99 | ->method('sendRequest')
|
84 |
| - ->with($mockRequest); |
| 100 | + ->with($this->requestMock); |
85 | 101 |
|
86 | 102 | $this->transaction->commit();
|
87 | 103 | }
|
88 | 104 |
|
89 |
| - public function testRollbackCallsBuildRollbackRequest(): void |
| 105 | + public function testRollbackSendsRollbackRequest(): void |
90 | 106 | {
|
91 |
| - $mockRequest = $this->createMock(RequestInterface::class); |
92 |
| - |
93 |
| - $this->requestFactory->expects($this->once()) |
| 107 | + $this->requestFactoryMock->expects($this->once()) |
94 | 108 | ->method('buildRollbackRequest')
|
95 | 109 | ->with($this->transactionId, $this->clusterAffinity)
|
96 |
| - ->willReturn($mockRequest); |
| 110 | + ->willReturn($this->requestMock); |
97 | 111 |
|
98 |
| - $this->client->expects($this->once()) |
| 112 | + $this->clientMock->expects($this->once()) |
99 | 113 | ->method('sendRequest')
|
100 |
| - ->with($mockRequest); |
| 114 | + ->with($this->requestMock); |
101 | 115 |
|
102 | 116 | $this->transaction->rollback();
|
103 | 117 | }
|
|
0 commit comments