|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Neo4j\QueryAPI\Tests\Unit; |
| 4 | + |
| 5 | +use Neo4j\QueryAPI\Results\ResultSet; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Neo4j\QueryAPI\Transaction; |
| 8 | +use Neo4j\QueryAPI\Neo4jRequestFactory; |
| 9 | +use Neo4j\QueryAPI\ResponseParser; |
| 10 | +use Psr\Http\Client\ClientInterface; |
| 11 | +use Psr\Http\Message\RequestInterface; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * @api |
| 16 | + */ |
| 17 | +class TransactionUnitTest extends TestCase |
| 18 | +{ |
| 19 | + private ClientInterface $client; |
| 20 | + private Neo4jRequestFactory $requestFactory; |
| 21 | + private ResponseParser $responseParser; |
| 22 | + private Transaction $transaction; |
| 23 | + |
| 24 | + private string $transactionId = 'txn123'; |
| 25 | + private string $clusterAffinity = 'leader'; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + $this->client = $this->createMock(ClientInterface::class); |
| 30 | + $this->requestFactory = $this->createMock(Neo4jRequestFactory::class); |
| 31 | + $this->responseParser = $this->createMock(ResponseParser::class); |
| 32 | + |
| 33 | + $this->transaction = new Transaction( |
| 34 | + $this->client, |
| 35 | + $this->responseParser, |
| 36 | + $this->requestFactory, |
| 37 | + $this->clusterAffinity, |
| 38 | + $this->transactionId |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function testRunCallsBuildTransactionRunRequest() |
| 43 | + { |
| 44 | + $query = "CREATE (:Person {name: \$name})"; |
| 45 | + $parameters = ['name' => 'Alice']; |
| 46 | + |
| 47 | + $mockRequest = $this->createMock(RequestInterface::class); |
| 48 | + $mockResponse = $this->createMock(ResponseInterface::class); |
| 49 | + $mockResultSet = $this->createMock(ResultSet::class); |
| 50 | + |
| 51 | + $this->requestFactory->expects($this->once()) |
| 52 | + ->method('buildTransactionRunRequest') |
| 53 | + ->with($query, $parameters, $this->transactionId, $this->clusterAffinity) |
| 54 | + ->willReturn($mockRequest); |
| 55 | + |
| 56 | + $this->client->expects($this->once()) |
| 57 | + ->method('sendRequest') |
| 58 | + ->with($mockRequest) |
| 59 | + ->willReturn($mockResponse); |
| 60 | + |
| 61 | + $this->responseParser->expects($this->once()) |
| 62 | + ->method('parseRunQueryResponse') |
| 63 | + ->with($mockResponse) |
| 64 | + ->willReturn($mockResultSet); |
| 65 | + |
| 66 | + $result = $this->transaction->run($query, $parameters); |
| 67 | + |
| 68 | + $this->assertSame($mockResultSet, $result); |
| 69 | + } |
| 70 | + |
| 71 | + public function testCommitCallsBuildCommitRequest() |
| 72 | + { |
| 73 | + $mockRequest = $this->createMock(RequestInterface::class); |
| 74 | + |
| 75 | + $this->requestFactory->expects($this->once()) |
| 76 | + ->method('buildCommitRequest') |
| 77 | + ->with($this->transactionId, $this->clusterAffinity) |
| 78 | + ->willReturn($mockRequest); |
| 79 | + |
| 80 | + $this->client->expects($this->once()) |
| 81 | + ->method('sendRequest') |
| 82 | + ->with($mockRequest); |
| 83 | + |
| 84 | + $this->transaction->commit(); |
| 85 | + } |
| 86 | + |
| 87 | + public function testRollbackCallsBuildRollbackRequest() |
| 88 | + { |
| 89 | + $mockRequest = $this->createMock(RequestInterface::class); |
| 90 | + |
| 91 | + $this->requestFactory->expects($this->once()) |
| 92 | + ->method('buildRollbackRequest') |
| 93 | + ->with($this->transactionId, $this->clusterAffinity) |
| 94 | + ->willReturn($mockRequest); |
| 95 | + |
| 96 | + $this->client->expects($this->once()) |
| 97 | + ->method('sendRequest') |
| 98 | + ->with($mockRequest); |
| 99 | + |
| 100 | + $this->transaction->rollback(); |
| 101 | + } |
| 102 | +} |
0 commit comments