Skip to content

Commit 3981a4f

Browse files
p123-stackPratiksha
andauthored
transaction object tests (#19)
* DEBUG * removed php cs fixer cache --------- Co-authored-by: Pratiksha <pratiksha@Pratiksha-Nagels>
1 parent ebb72e9 commit 3981a4f

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

.php-cs-fixer.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/Integration/Neo4jTransactionIntegrationTest.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PHPUnit\Framework\TestCase;
1010

1111
/**
12-
* @api
12+
* @api
1313
*/
1414
class Neo4jTransactionIntegrationTest extends TestCase
1515
{
@@ -54,6 +54,7 @@ private function populateTestData(): void
5454
$this->api->run('CREATE (:Person {name: $name})', ['name' => $name]);
5555
}
5656
}
57+
5758
public function testTransactionCommit(): void
5859
{
5960

@@ -75,4 +76,55 @@ public function testTransactionCommit(): void
7576
$this->assertCount(1, $results); // Updated to expect 1 result
7677
}
7778

79+
public function testTransactionRollback(): void
80+
{
81+
$tsx = $this->api->beginTransaction();
82+
83+
$name = 'rollback_' . mt_rand(1, 100000);
84+
$tsx->run("CREATE (x:Human {name: \$name})", ['name' => $name]);
85+
$results = $tsx->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
86+
$this->assertCount(1, $results);
87+
88+
$tsx->rollback();
89+
90+
// Ensure the node is not in the database
91+
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
92+
$this->assertCount(0, $results);
93+
}
94+
95+
public function testCreateNodeAndCommit(): void
96+
{
97+
$tsx = $this->api->beginTransaction();
98+
99+
$name = 'committed_' . mt_rand(1, 100000);
100+
$tsx->run("CREATE (x:Person {name: \$name})", ['name' => $name]);
101+
102+
$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
103+
$this->assertCount(0, $results);
104+
105+
$tsx->commit();
106+
$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
107+
$this->assertCount(1, $results);
108+
}
109+
110+
public function testCreateNodeAndRollback(): void
111+
{
112+
$tsx = $this->api->beginTransaction();
113+
114+
$name = 'rollback_' . mt_rand(1, 100000);
115+
$tsx->run("CREATE (x:Person {name: \$name})", ['name' => $name]);
116+
117+
$results = $tsx->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
118+
$this->assertCount(1, $results);
119+
120+
$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
121+
$this->assertCount(0, $results);
122+
123+
$tsx->rollback();
124+
$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
125+
$this->assertCount(0, $results);
126+
}
127+
128+
129+
78130
}

tests/Unit/TransactionUnitTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)