|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * This file is part of DTM-PHP. |
| 6 | + * |
| 7 | + * @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE |
| 8 | + */ |
| 9 | +namespace DtmClientTest\Cases; |
| 10 | + |
| 11 | +use DtmClient\Api\ApiInterface; |
| 12 | +use DtmClient\Api\RequestBranch; |
| 13 | +use DtmClient\BranchIdGeneratorInterface; |
| 14 | +use DtmClient\Constants\Operation; |
| 15 | +use DtmClient\Constants\Protocol; |
| 16 | +use DtmClient\Constants\TransType; |
| 17 | +use DtmClient\Grpc\Message\DtmRequest; |
| 18 | +use DtmClient\TCC; |
| 19 | +use DtmClient\TransContext; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + * @coversNothing |
| 24 | + */ |
| 25 | +class TCCTest extends AbstractTestCase |
| 26 | +{ |
| 27 | + public function testInit() |
| 28 | + { |
| 29 | + $api = \Mockery::mock(ApiInterface::class); |
| 30 | + $branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class); |
| 31 | + |
| 32 | + $api->shouldReceive('generateGid')->andReturn('GidStub'); |
| 33 | + |
| 34 | + $tcc = new TCC($api, $branchIdGenerator); |
| 35 | + $tcc->init(); |
| 36 | + $this->assertSame('GidStub', TransContext::getGid()); |
| 37 | + $this->assertSame(TransType::TCC, TransContext::getTransType()); |
| 38 | + $this->assertSame('', TransContext::getBranchId()); |
| 39 | + |
| 40 | + $tcc->init('test'); |
| 41 | + $this->assertSame('test', TransContext::getGid()); |
| 42 | + } |
| 43 | + |
| 44 | + public function testGlobalTransaction() |
| 45 | + { |
| 46 | + $api = \Mockery::mock(ApiInterface::class); |
| 47 | + $branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class); |
| 48 | + |
| 49 | + $api->shouldReceive('generateGid')->andReturn('GlobalTransactionGidStub'); |
| 50 | + $api->shouldReceive('prepare')->andReturnTrue(); |
| 51 | + $api->shouldReceive('abort')->andReturnTrue(); |
| 52 | + $api->shouldReceive('submit')->andReturnTrue(); |
| 53 | + |
| 54 | + $tcc = new TCC($api, $branchIdGenerator); |
| 55 | + |
| 56 | + $tcc->globalTransaction(function () { |
| 57 | + // Used only in test cases |
| 58 | + TransContext::set(static::class . 'globalTransaction', 1); |
| 59 | + }, 'GidStub'); |
| 60 | + $this->assertSame('GidStub', TransContext::getGid()); |
| 61 | + $this->assertSame(TransType::TCC, TransContext::getTransType()); |
| 62 | + $this->assertSame('', TransContext::getBranchId()); |
| 63 | + $this->assertSame(1, TransContext::get(static::class . 'globalTransaction')); |
| 64 | + } |
| 65 | + |
| 66 | + public function testCallBranchUseJsonRpcProtocol() |
| 67 | + { |
| 68 | + $api = \Mockery::mock(ApiInterface::class); |
| 69 | + $branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class); |
| 70 | + |
| 71 | + $branchIdGenerator->shouldReceive('generateSubBranchId')->andReturn('BranchIdStub'); |
| 72 | + |
| 73 | + $api->shouldReceive('getProtocol')->andReturn(Protocol::JSONRPC_HTTP); |
| 74 | + $api->shouldReceive('registerBranch')->andReturnUsing(function ($data) { |
| 75 | + TransContext::set(static::class . 'registerBranchBody', $data); |
| 76 | + }); |
| 77 | + $api->shouldReceive('transRequestBranch')->andReturnUsing(function ($data) { |
| 78 | + return $data; |
| 79 | + }); |
| 80 | + |
| 81 | + TransContext::setGid('testGid'); |
| 82 | + |
| 83 | + $tcc = new TCC($api, $branchIdGenerator); |
| 84 | + |
| 85 | + $response = $tcc->callBranch( |
| 86 | + ['data' => 'test'], |
| 87 | + '127.0.0.1/try', |
| 88 | + '127.0.0.1/confirm', |
| 89 | + '127.0.0.1/cancel' |
| 90 | + ); |
| 91 | + |
| 92 | + $branchRequest = new RequestBranch(); |
| 93 | + $branchRequest->method = 'POST'; |
| 94 | + $branchRequest->url = '127.0.0.1/try'; |
| 95 | + $branchRequest->branchId = 'BranchIdStub'; |
| 96 | + $branchRequest->op = Operation::TRY; |
| 97 | + $branchRequest->body = ['data' => 'test']; |
| 98 | + |
| 99 | + $this->assertEquals($branchRequest, $response); |
| 100 | + $this->assertSame([ |
| 101 | + 'data' => json_encode(['data' => 'test']), |
| 102 | + 'branch_id' => 'BranchIdStub', |
| 103 | + 'confirm' => '127.0.0.1/confirm', |
| 104 | + 'cancel' => '127.0.0.1/cancel', |
| 105 | + 'gid' => 'testGid', |
| 106 | + 'trans_type' => TransType::TCC, |
| 107 | + ], TransContext::get(static::class . 'registerBranchBody')); |
| 108 | + } |
| 109 | + |
| 110 | + public function testCallBranchUseHttpProtocol() |
| 111 | + { |
| 112 | + $api = \Mockery::mock(ApiInterface::class); |
| 113 | + $branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class); |
| 114 | + |
| 115 | + $branchIdGenerator->shouldReceive('generateSubBranchId')->andReturn('BranchIdStub'); |
| 116 | + |
| 117 | + $api->shouldReceive('getProtocol')->andReturn(Protocol::HTTP); |
| 118 | + $api->shouldReceive('registerBranch')->andReturnUsing(function ($data) { |
| 119 | + TransContext::set(static::class . 'registerBranchBody', $data); |
| 120 | + }); |
| 121 | + $api->shouldReceive('transRequestBranch')->andReturnUsing(function ($data) { |
| 122 | + return $data; |
| 123 | + }); |
| 124 | + |
| 125 | + TransContext::setGid('testGid'); |
| 126 | + |
| 127 | + $tcc = new TCC($api, $branchIdGenerator); |
| 128 | + |
| 129 | + $response = $tcc->callBranch( |
| 130 | + ['data' => 'test'], |
| 131 | + '127.0.0.1/try', |
| 132 | + '127.0.0.1/confirm', |
| 133 | + '127.0.0.1/cancel' |
| 134 | + ); |
| 135 | + |
| 136 | + $branchRequest = new RequestBranch(); |
| 137 | + $branchRequest->method = 'POST'; |
| 138 | + $branchRequest->url = '127.0.0.1/try'; |
| 139 | + $branchRequest->branchId = 'BranchIdStub'; |
| 140 | + $branchRequest->op = Operation::TRY; |
| 141 | + $branchRequest->body = ['data' => 'test']; |
| 142 | + |
| 143 | + $this->assertEquals($branchRequest, $response); |
| 144 | + $this->assertSame([ |
| 145 | + 'data' => json_encode(['data' => 'test']), |
| 146 | + 'branch_id' => 'BranchIdStub', |
| 147 | + 'confirm' => '127.0.0.1/confirm', |
| 148 | + 'cancel' => '127.0.0.1/cancel', |
| 149 | + 'gid' => 'testGid', |
| 150 | + 'trans_type' => TransType::TCC, |
| 151 | + ], TransContext::get(static::class . 'registerBranchBody')); |
| 152 | + } |
| 153 | + |
| 154 | + public function testCallBranchUseGrpcProtocol() |
| 155 | + { |
| 156 | + $api = \Mockery::mock(ApiInterface::class); |
| 157 | + $branchIdGenerator = \Mockery::mock(BranchIdGeneratorInterface::class); |
| 158 | + |
| 159 | + $branchIdGenerator->shouldReceive('generateSubBranchId')->andReturn('BranchIdStub'); |
| 160 | + |
| 161 | + $api->shouldReceive('getProtocol')->andReturn(Protocol::GRPC); |
| 162 | + $api->shouldReceive('registerBranch')->andReturnUsing(function ($data) { |
| 163 | + TransContext::set(static::class . 'registerBranchBody', $data); |
| 164 | + }); |
| 165 | + $api->shouldReceive('transRequestBranch')->andReturnUsing(function ($data) { |
| 166 | + return $data; |
| 167 | + }); |
| 168 | + |
| 169 | + TransContext::setGid('testGid'); |
| 170 | + |
| 171 | + $tcc = new TCC($api, $branchIdGenerator); |
| 172 | + |
| 173 | + $message = new DtmRequest(); |
| 174 | + |
| 175 | + $response = $tcc->callBranch( |
| 176 | + $message, |
| 177 | + '127.0.0.1/try', |
| 178 | + '127.0.0.1/confirm', |
| 179 | + '127.0.0.1/cancel' |
| 180 | + ); |
| 181 | + |
| 182 | + $branchRequest = new RequestBranch(); |
| 183 | + $branchRequest->grpcArgument = $message; |
| 184 | + $branchRequest->url = '127.0.0.1/try'; |
| 185 | + $branchRequest->grpcMetadata = [ |
| 186 | + 'dtm-gid' => 'testGid', |
| 187 | + 'dtm-trans_type' => TransType::TCC, |
| 188 | + 'dtm-branch_id' => 'BranchIdStub', |
| 189 | + 'dtm-op' => Operation::TRY, |
| 190 | + 'dtm-dtm' => TransContext::getDtm(), |
| 191 | + ]; |
| 192 | + |
| 193 | + $this->assertEquals($branchRequest, $response); |
| 194 | + $this->assertSame([ |
| 195 | + 'Gid' => 'testGid', |
| 196 | + 'TransType' => TransType::TCC, |
| 197 | + 'BranchID' => 'BranchIdStub', |
| 198 | + 'BusiPayload' => $message->serializeToString(), |
| 199 | + 'Data' => ['confirm' => '127.0.0.1/confirm', 'cancel' => '127.0.0.1/cancel'], |
| 200 | + ], TransContext::get(static::class . 'registerBranchBody')); |
| 201 | + } |
| 202 | +} |
0 commit comments