Skip to content

Commit

Permalink
Merge pull request #44 from PandaLIU-1111/update-test-case
Browse files Browse the repository at this point in the history
增加单元测试,并且增加非协程环境的上下文支持
  • Loading branch information
huangzhhui authored Oct 16, 2022
2 parents e33991d + eebaec7 commit c7f53f5
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Create Release
id: create_release
uses: actions/create-release@v1
Expand Down
14 changes: 11 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
Expand Down Expand Up @@ -60,10 +60,10 @@ jobs:
sudo make install
sudo sh -c "echo extension=swow > /etc/php/${{ matrix.php-version }}/cli/conf.d/swow.ini"
php --ri swow
- name: Setup Packages
run: composer update -o --no-scripts
- name: Run Test Cases
run: |
composer update -o --no-scripts
composer require symfony/finder:^5.0
composer require hyperf/di
composer require hyperf/grpc-client
Expand All @@ -77,5 +77,13 @@ jobs:
composer require hyperf/json-rpc
composer require hyperf/rpc-client
composer require symfony/serializer:^5.0
- name: Setup Swow Packages
if: ${{ matrix.engine == 'swow' }}
run: |
composer require hyperf/engine-swow
- name: Run Test Cases
run: |
composer analyse
composer test
8 changes: 3 additions & 5 deletions src/Api/JsonRpcHttpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@

class JsonRpcHttpApi extends AbstractServiceClient implements ApiInterface
{
protected string $serviceName = 'dtmserver';

protected string $protocol = 'jsonrpc-http';

protected ConfigInterface $config;

protected JsonRpcClientManager $jsonRpcClientManager;

public function __construct(ContainerInterface $container, DtmPathGenerator $pathGenerator, JsonRpcClientManager $jsonRpcClientManager)
{
parent::__construct($container);

// Compatible with hyperf2.2 and hyperf3.0
$this->serviceName = 'dtmserver';
$this->protocol = 'jsonrpc-http';
$this->pathGenerator = $pathGenerator;
$this->config = $container->get(ConfigInterface::class);
$this->jsonRpcClientManager = $jsonRpcClientManager;
Expand Down
45 changes: 45 additions & 0 deletions src/Context/Context.php
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');
}

}
12 changes: 12 additions & 0 deletions src/Context/ContextInterface.php
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();
}
2 changes: 1 addition & 1 deletion src/TransContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/
namespace DtmClient;

use DtmClient\Context\Context;
use DtmClient\Util\Str;
use Hyperf\Context\Context;

/**
* All properties in this class are read-only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
* @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE
*/
namespace DtmClientTest\Cases;
namespace DtmClientTest\Cases\Api;

use DtmClient\Api\GrpcApi;
use DtmClient\Grpc\GrpcClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*
* @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE
*/
namespace DtmClientTest\Cases;
namespace DtmClientTest\Cases\Api;

use DtmClient\Api\GrpcApi;
use DtmClient\Grpc\GrpcClientManager;
use DtmClientTest\Cases\AbstractTestCase;
use Hyperf\Contract\ConfigInterface;
use Hyperf\GrpcClient\BaseClient;
use Hyperf\Utils\ApplicationContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*
* @license https://github.com/dtm-php/dtm-client/blob/master/LICENSE
*/
namespace DtmClientTest\Cases;
namespace DtmClientTest\Cases\Api;

use DtmClient\Api\HttpApi;
use DtmClientTest\Cases\AbstractTestCase;
use GuzzleHttp\Client;
use Hyperf\Contract\ConfigInterface;
use Mockery;
Expand Down
62 changes: 62 additions & 0 deletions tests/Cases/ApiFactoryTest.php
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);
}
}
50 changes: 50 additions & 0 deletions tests/Cases/BarrierTest.php
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'
]);

}
}

0 comments on commit c7f53f5

Please sign in to comment.