-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from PandaLIU-1111/update-test-case
增加单元测试,并且增加非协程环境的上下文支持
- Loading branch information
Showing
11 changed files
with
190 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace DtmClient\Context; | ||
|
||
use Hyperf\Context\Context as HyperfContext; | ||
|
||
class Context implements ContextInterface | ||
{ | ||
protected static array $nonCoContext = []; | ||
|
||
public static function set(string $id, $value) | ||
{ | ||
if (static::isUseCoroutineExtension()) { | ||
return HyperfContext::set($id, $value); | ||
} | ||
|
||
static::$nonCoContext[$id] = $value; | ||
return $value; | ||
} | ||
|
||
public static function get(string $id, $default = null, $coroutineId = null) | ||
{ | ||
if (static::isUseCoroutineExtension()) { | ||
return HyperfContext::get($id, $default, $coroutineId); | ||
} | ||
|
||
return static::$nonCoContext[$id] ?? $default; | ||
} | ||
|
||
public static function getContainer() | ||
{ | ||
if (static::isUseCoroutineExtension()) { | ||
return HyperfContext::getContainer(); | ||
} | ||
|
||
return static::$nonCoContext; | ||
} | ||
|
||
|
||
private static function isUseCoroutineExtension(): bool | ||
{ | ||
return extension_loaded('Swow') || extension_loaded('Swoole'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace DtmClient\Context; | ||
|
||
interface ContextInterface | ||
{ | ||
public static function set(string $id, $value); | ||
|
||
public static function get(string $id, $default = null, $coroutineId = null); | ||
|
||
public static function getContainer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace DtmClientTest\Cases; | ||
|
||
use DtmClient\Api\GrpcApi; | ||
use DtmClient\Api\HttpApi; | ||
use DtmClient\Api\JsonRpcHttpApi; | ||
use DtmClient\ApiFactory; | ||
use DtmClient\Constants\Protocol; | ||
use Hyperf\Contract\ConfigInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class ApiFactoryTest extends AbstractTestCase | ||
{ | ||
public function testGetHttpApi() | ||
{ | ||
$configInterface = \Mockery::mock(ConfigInterface::class); | ||
$configInterface->shouldReceive('get')->andReturn(Protocol::HTTP); | ||
$container = \Mockery::mock(ContainerInterface::class); | ||
|
||
$container->shouldReceive('get')->withArgs([ConfigInterface::class])->andReturn($configInterface); | ||
|
||
$httpApi = \Mockery::mock(HttpApi::class); | ||
$container->shouldReceive('get')->withArgs([HttpApi::class])->andReturn($httpApi); | ||
|
||
|
||
$api = (new ApiFactory())($container); | ||
$this->assertInstanceOf(HttpApi::class, $api); | ||
} | ||
|
||
public function testGetGrpcApi() | ||
{ | ||
$configInterface = \Mockery::mock(ConfigInterface::class); | ||
$configInterface->shouldReceive('get')->andReturn(Protocol::GRPC); | ||
$container = \Mockery::mock(ContainerInterface::class); | ||
|
||
$container->shouldReceive('get')->withArgs([ConfigInterface::class])->andReturn($configInterface); | ||
|
||
$httpApi = \Mockery::mock(GrpcApi::class); | ||
$container->shouldReceive('get')->withArgs([GrpcApi::class])->andReturn($httpApi); | ||
|
||
|
||
$api = (new ApiFactory())($container); | ||
$this->assertInstanceOf(GrpcApi::class, $api); | ||
} | ||
|
||
public function testGetJsonRpcHttpApi() | ||
{ | ||
$configInterface = \Mockery::mock(ConfigInterface::class); | ||
$configInterface->shouldReceive('get')->andReturn(Protocol::JSONRPC_HTTP); | ||
$container = \Mockery::mock(ContainerInterface::class); | ||
|
||
$container->shouldReceive('get')->withArgs([ConfigInterface::class])->andReturn($configInterface); | ||
|
||
$httpApi = \Mockery::mock(JsonRpcHttpApi::class); | ||
$container->shouldReceive('get')->withArgs([JsonRpcHttpApi::class])->andReturn($httpApi); | ||
|
||
|
||
$api = (new ApiFactory())($container); | ||
$this->assertInstanceOf(JsonRpcHttpApi::class, $api); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace DtmClientTest\Cases; | ||
|
||
use DtmClient\Barrier; | ||
use DtmClient\Constants\DbType; | ||
use DtmClient\Constants\Protocol; | ||
use DtmClient\Constants\TransType; | ||
use DtmClient\MySqlBarrier; | ||
use DtmClient\TransContext; | ||
use Hyperf\Contract\ConfigInterface; | ||
|
||
class BarrierTest extends AbstractTestCase | ||
{ | ||
public function testCall() | ||
{ | ||
$configInterface = $this->createMock(ConfigInterface::class); | ||
$configInterface->method('get')->willReturn(DbType::MySQL); | ||
|
||
$mySqlBarrier = $this->createMock(MySqlBarrier::class); | ||
|
||
$mySqlBarrier->method('call')->willReturn(true); | ||
|
||
$barrier = new Barrier($configInterface, $mySqlBarrier); | ||
$this->assertTrue($barrier->call(function () { | ||
return true; | ||
})); | ||
} | ||
|
||
public function testBarrierFrom() | ||
{ | ||
$configInterface = $this->createMock(ConfigInterface::class); | ||
$configInterface->method('get')->willReturn(Protocol::GRPC); | ||
|
||
$mySqlBarrier = $this->createMock(MySqlBarrier::class); | ||
|
||
$barrier = new Barrier($configInterface, $mySqlBarrier); | ||
$barrier->barrierFrom(TransType::TCC, 'gid', 'branchId', 'try', 'phase2Url', 'testDtm'); | ||
|
||
$this->assertSame(TransContext::toArray(), [ | ||
'trans_type' => 'tcc', | ||
'gid' => 'gid', | ||
'branch_id' => 'branchId', | ||
'op' => 'try', | ||
'phase2_url' => 'phase2Url', | ||
'dtm' => 'testDtm' | ||
]); | ||
|
||
} | ||
} |