Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .php-cs-fixer.cache

This file was deleted.

54 changes: 53 additions & 1 deletion tests/Integration/Neo4jTransactionIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PHPUnit\Framework\TestCase;

/**
* @api
* @api
*/
class Neo4jTransactionIntegrationTest extends TestCase
{
Expand Down Expand Up @@ -54,6 +54,7 @@ private function populateTestData(): void
$this->api->run('CREATE (:Person {name: $name})', ['name' => $name]);
}
}

public function testTransactionCommit(): void
{

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

public function testTransactionRollback(): void
{
$tsx = $this->api->beginTransaction();

$name = 'rollback_' . mt_rand(1, 100000);
$tsx->run("CREATE (x:Human {name: \$name})", ['name' => $name]);
$results = $tsx->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(1, $results);

$tsx->rollback();

// Ensure the node is not in the database
$results = $this->api->run("MATCH (x:Human {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(0, $results);
}

public function testCreateNodeAndCommit(): void
{
$tsx = $this->api->beginTransaction();

$name = 'committed_' . mt_rand(1, 100000);
$tsx->run("CREATE (x:Person {name: \$name})", ['name' => $name]);

$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(0, $results);

$tsx->commit();
$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(1, $results);
}

public function testCreateNodeAndRollback(): void
{
$tsx = $this->api->beginTransaction();

$name = 'rollback_' . mt_rand(1, 100000);
$tsx->run("CREATE (x:Person {name: \$name})", ['name' => $name]);

$results = $tsx->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(1, $results);

$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(0, $results);

$tsx->rollback();
$results = $this->api->run("MATCH (x:Person {name: \$name}) RETURN x", ['name' => $name]);
$this->assertCount(0, $results);
}



}
102 changes: 102 additions & 0 deletions tests/Unit/TransactionUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Neo4j\QueryAPI\Tests\Unit;

use Neo4j\QueryAPI\Results\ResultSet;
use PHPUnit\Framework\TestCase;
use Neo4j\QueryAPI\Transaction;
use Neo4j\QueryAPI\Neo4jRequestFactory;
use Neo4j\QueryAPI\ResponseParser;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @api
*/
class TransactionUnitTest extends TestCase
{
private ClientInterface $client;
private Neo4jRequestFactory $requestFactory;
private ResponseParser $responseParser;
private Transaction $transaction;

private string $transactionId = 'txn123';
private string $clusterAffinity = 'leader';

protected function setUp(): void
{
$this->client = $this->createMock(ClientInterface::class);
$this->requestFactory = $this->createMock(Neo4jRequestFactory::class);
$this->responseParser = $this->createMock(ResponseParser::class);

$this->transaction = new Transaction(
$this->client,
$this->responseParser,
$this->requestFactory,
$this->clusterAffinity,
$this->transactionId
);
}

public function testRunCallsBuildTransactionRunRequest()
{
$query = "CREATE (:Person {name: \$name})";
$parameters = ['name' => 'Alice'];

$mockRequest = $this->createMock(RequestInterface::class);
$mockResponse = $this->createMock(ResponseInterface::class);
$mockResultSet = $this->createMock(ResultSet::class);

$this->requestFactory->expects($this->once())
->method('buildTransactionRunRequest')
->with($query, $parameters, $this->transactionId, $this->clusterAffinity)
->willReturn($mockRequest);

$this->client->expects($this->once())
->method('sendRequest')
->with($mockRequest)
->willReturn($mockResponse);

$this->responseParser->expects($this->once())
->method('parseRunQueryResponse')
->with($mockResponse)
->willReturn($mockResultSet);

$result = $this->transaction->run($query, $parameters);

$this->assertSame($mockResultSet, $result);
}

public function testCommitCallsBuildCommitRequest()
{
$mockRequest = $this->createMock(RequestInterface::class);

$this->requestFactory->expects($this->once())
->method('buildCommitRequest')
->with($this->transactionId, $this->clusterAffinity)
->willReturn($mockRequest);

$this->client->expects($this->once())
->method('sendRequest')
->with($mockRequest);

$this->transaction->commit();
}

public function testRollbackCallsBuildRollbackRequest()
{
$mockRequest = $this->createMock(RequestInterface::class);

$this->requestFactory->expects($this->once())
->method('buildRollbackRequest')
->with($this->transactionId, $this->clusterAffinity)
->willReturn($mockRequest);

$this->client->expects($this->once())
->method('sendRequest')
->with($mockRequest);

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