Skip to content

Commit

Permalink
Added TCC test case
Browse files Browse the repository at this point in the history
Added TCC test case
  • Loading branch information
PandaLIU-1111 committed Oct 16, 2022
2 parents 4dc10cd + 630bdbf commit ddcb4d8
Showing 1 changed file with 202 additions and 0 deletions.
202 changes: 202 additions & 0 deletions tests/Cases/TCCTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

declare(strict_types=1);
/**
* This file is part of DTM-PHP.
*
* @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE
*/
namespace DtmClientTest\Cases;

use DtmClient\Api\ApiInterface;
use DtmClient\Api\RequestBranch;
use DtmClient\BranchIdGeneratorInterface;
use DtmClient\Constants\Operation;
use DtmClient\Constants\Protocol;
use DtmClient\Constants\TransType;
use DtmClient\Grpc\Message\DtmRequest;
use DtmClient\TCC;
use DtmClient\TransContext;

/**
* @internal
* @coversNothing
*/
class TCCTest extends AbstractTestCase
{
public function testInit()
{
$api = \Mockery::mock(ApiInterface::class);
$branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class);

$api->shouldReceive('generateGid')->andReturn('GidStub');

$tcc = new TCC($api, $branchIdGenerator);
$tcc->init();
$this->assertSame('GidStub', TransContext::getGid());
$this->assertSame(TransType::TCC, TransContext::getTransType());
$this->assertSame('', TransContext::getBranchId());

$tcc->init('test');
$this->assertSame('test', TransContext::getGid());
}

public function testGlobalTransaction()
{
$api = \Mockery::mock(ApiInterface::class);
$branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class);

$api->shouldReceive('generateGid')->andReturn('GlobalTransactionGidStub');
$api->shouldReceive('prepare')->andReturnTrue();
$api->shouldReceive('abort')->andReturnTrue();
$api->shouldReceive('submit')->andReturnTrue();

$tcc = new TCC($api, $branchIdGenerator);

$tcc->globalTransaction(function () {
// Used only in test cases
TransContext::set(static::class . 'globalTransaction', 1);
}, 'GidStub');
$this->assertSame('GidStub', TransContext::getGid());
$this->assertSame(TransType::TCC, TransContext::getTransType());
$this->assertSame('', TransContext::getBranchId());
$this->assertSame(1, TransContext::get(static::class . 'globalTransaction'));
}

public function testCallBranchUseJsonRpcProtocol()
{
$api = \Mockery::mock(ApiInterface::class);
$branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class);

$branchIdGenerator->shouldReceive('generateSubBranchId')->andReturn('BranchIdStub');

$api->shouldReceive('getProtocol')->andReturn(Protocol::JSONRPC_HTTP);
$api->shouldReceive('registerBranch')->andReturnUsing(function ($data) {
TransContext::set(static::class . 'registerBranchBody', $data);
});
$api->shouldReceive('transRequestBranch')->andReturnUsing(function ($data) {
return $data;
});

TransContext::setGid('testGid');

$tcc = new TCC($api, $branchIdGenerator);

$response = $tcc->callBranch(
['data' => 'test'],
'127.0.0.1/try',
'127.0.0.1/confirm',
'127.0.0.1/cancel'
);

$branchRequest = new RequestBranch();
$branchRequest->method = 'POST';
$branchRequest->url = '127.0.0.1/try';
$branchRequest->branchId = 'BranchIdStub';
$branchRequest->op = Operation::TRY;
$branchRequest->body = ['data' => 'test'];

$this->assertEquals($branchRequest, $response);
$this->assertSame([
'data' => json_encode(['data' => 'test']),
'branch_id' => 'BranchIdStub',
'confirm' => '127.0.0.1/confirm',
'cancel' => '127.0.0.1/cancel',
'gid' => 'testGid',
'trans_type' => TransType::TCC,
], TransContext::get(static::class . 'registerBranchBody'));
}

public function testCallBranchUseHttpProtocol()
{
$api = \Mockery::mock(ApiInterface::class);
$branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class);

$branchIdGenerator->shouldReceive('generateSubBranchId')->andReturn('BranchIdStub');

$api->shouldReceive('getProtocol')->andReturn(Protocol::HTTP);
$api->shouldReceive('registerBranch')->andReturnUsing(function ($data) {
TransContext::set(static::class . 'registerBranchBody', $data);
});
$api->shouldReceive('transRequestBranch')->andReturnUsing(function ($data) {
return $data;
});

TransContext::setGid('testGid');

$tcc = new TCC($api, $branchIdGenerator);

$response = $tcc->callBranch(
['data' => 'test'],
'127.0.0.1/try',
'127.0.0.1/confirm',
'127.0.0.1/cancel'
);

$branchRequest = new RequestBranch();
$branchRequest->method = 'POST';
$branchRequest->url = '127.0.0.1/try';
$branchRequest->branchId = 'BranchIdStub';
$branchRequest->op = Operation::TRY;
$branchRequest->body = ['data' => 'test'];

$this->assertEquals($branchRequest, $response);
$this->assertSame([
'data' => json_encode(['data' => 'test']),
'branch_id' => 'BranchIdStub',
'confirm' => '127.0.0.1/confirm',
'cancel' => '127.0.0.1/cancel',
'gid' => 'testGid',
'trans_type' => TransType::TCC,
], TransContext::get(static::class . 'registerBranchBody'));
}

public function testCallBranchUseGrpcProtocol()
{
$api = \Mockery::mock(ApiInterface::class);
$branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class);

$branchIdGenerator->shouldReceive('generateSubBranchId')->andReturn('BranchIdStub');

$api->shouldReceive('getProtocol')->andReturn(Protocol::GRPC);
$api->shouldReceive('registerBranch')->andReturnUsing(function ($data) {
TransContext::set(static::class . 'registerBranchBody', $data);
});
$api->shouldReceive('transRequestBranch')->andReturnUsing(function ($data) {
return $data;
});

TransContext::setGid('testGid');

$tcc = new TCC($api, $branchIdGenerator);

$message = new DtmRequest();

$response = $tcc->callBranch(
$message,
'127.0.0.1/try',
'127.0.0.1/confirm',
'127.0.0.1/cancel'
);

$branchRequest = new RequestBranch();
$branchRequest->grpcArgument = $message;
$branchRequest->url = '127.0.0.1/try';
$branchRequest->grpcMetadata = [
'dtm-gid' => 'testGid',
'dtm-trans_type' => TransType::TCC,
'dtm-branch_id' => 'BranchIdStub',
'dtm-op' => Operation::TRY,
'dtm-dtm' => TransContext::getDtm(),
];

$this->assertEquals($branchRequest, $response);
$this->assertSame([
'Gid' => 'testGid',
'TransType' => TransType::TCC,
'BranchID' => 'BranchIdStub',
'BusiPayload' => $message->serializeToString(),
'Data' => ['confirm' => '127.0.0.1/confirm', 'cancel' => '127.0.0.1/cancel'],
], TransContext::get(static::class . 'registerBranchBody'));
}
}

0 comments on commit ddcb4d8

Please sign in to comment.