tests: 增加微信服务商测试用例

This commit is contained in:
yansongda 2021-08-28 18:25:54 +08:00
parent 27e78d0869
commit 459ccf5fa5
10 changed files with 297 additions and 4 deletions

View File

@ -27,4 +27,20 @@ class GeneralPluginTest extends TestCase
self::assertEquals('POST', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_NORMAL].'yansongda/pay'), $radar->getUri());
}
public function testPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider']);
$plugin = new WechatGeneralPluginStub();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('POST', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_SERVICE].'yansongda/pay/partner'), $radar->getUri());
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\App;
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\App;
use Yansongda\Pay\Plugin\Wechat\Pay\App\InvokePrepayPlugin;
use Yansongda\Pay\Rocket;

View File

@ -0,0 +1,56 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\App;
use Yansongda\Pay\Plugin\Wechat\Pay\App\PrepayPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class PrepayPluginTest extends TestCase
{
public function testWechatIdNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('yansongda', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}
public function testWechatIdPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection());
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('wx55955316af4ef16', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
public function testWechatIdPartnerDirect()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_appid' => '123']));
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('123', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Common;
use GuzzleHttp\Psr7\Uri;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\Pay\Common\ClosePlugin;
use Yansongda\Pay\Provider\Wechat;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class ClosePluginTest extends TestCase
{
public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection(['out_trade_no' => '123']));
$plugin = new ClosePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_NORMAL].'v3/pay/transactions/out-trade-no/123/close'), $radar->getUri());
self::assertEquals('1600314069', $payload->get('mchid'));
self::assertArrayNotHasKey('sp_mchid', $payload->all());
self::assertArrayNotHasKey('sub_mchid', $payload->all());
}
public function testNormalNoOutTradeNo()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$plugin = new ClosePlugin();
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(Exception::MISSING_NECESSARY_PARAMS);
$plugin->assembly($rocket, function ($rocket) { return $rocket; });
}
public function testPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['out_trade_no' => '123']));
$plugin = new ClosePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_NORMAL].'v3/pay/partner/transactions/out-trade-no/123/close'), $radar->getUri());
self::assertEquals('1600314069', $payload->get('sp_mchid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
self::assertArrayNotHasKey('mchid', $payload->all());
}
public function testPartnerDirectPayload()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['out_trade_no' => '123', 'sub_mchid' => '123']));
$plugin = new ClosePlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('123', $payload->get('sub_mchid'));
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Common;
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Common;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Plugin\Wechat\Pay\Common\InvokePrepayPlugin;

View File

@ -0,0 +1,80 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Common;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Wechat\Pay\Common\PrepayPlugin;
use Yansongda\Pay\Provider\Wechat;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class PrepayPluginTest extends TestCase
{
public function testNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('POST', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_NORMAL].'v3/pay/transactions/jsapi'), $radar->getUri());
self::assertArrayHasKey('appid', $payload->all());
self::assertArrayHasKey('mchid', $payload->all());
self::assertArrayNotHasKey('sp_appid', $payload->all());
self::assertArrayNotHasKey('sp_mchid', $payload->all());
self::assertArrayNotHasKey('sub_appid', $payload->all());
self::assertArrayNotHasKey('sub_mchid', $payload->all());
self::assertEquals('wx55955316af4ef13', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}
public function testPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection());
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$radar = $result->getRadar();
$payload = $result->getPayload();
self::assertInstanceOf(RequestInterface::class, $radar);
self::assertEquals('POST', $radar->getMethod());
self::assertEquals(new Uri(Wechat::URL[Pay::MODE_SERVICE].'v3/pay/partner/transactions/jsapi'), $radar->getUri());
self::assertArrayNotHasKey('appid', $payload->all());
self::assertArrayNotHasKey('mchid', $payload->all());
self::assertArrayHasKey('sp_appid', $payload->all());
self::assertArrayHasKey('sp_mchid', $payload->all());
self::assertArrayHasKey('sub_appid', $payload->all());
self::assertArrayHasKey('sub_mchid', $payload->all());
self::assertEquals('wx55955316af4ef15', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
public function testPartnerDirectPayload()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_appid' => '123']));
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('123', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Mini;
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Mini;
use Yansongda\Pay\Plugin\Wechat\Pay\Mini\InvokePrepayPlugin;
use Yansongda\Pay\Rocket;

View File

@ -0,0 +1,56 @@
<?php
namespace Yansongda\Pay\Tests\Plugin\Wechat\Pay\Mini;
use Yansongda\Pay\Plugin\Wechat\Pay\Mini\PrepayPlugin;
use Yansongda\Pay\Rocket;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
class PrepayPluginTest extends TestCase
{
public function testWechatIdNormal()
{
$rocket = new Rocket();
$rocket->setParams([])->setPayload(new Collection());
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('wx55955316af4ef14', $payload->get('appid'));
self::assertEquals('1600314069', $payload->get('mchid'));
}
public function testWechatIdPartner()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection());
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('wx55955316af4ef17', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
public function testWechatIdPartnerDirect()
{
$rocket = new Rocket();
$rocket->setParams(['_config' => 'service_provider'])->setPayload(new Collection(['sub_appid' => '123']));
$plugin = new PrepayPlugin();
$result = $plugin->assembly($rocket, function ($rocket) { return $rocket; });
$payload = $result->getPayload();
self::assertEquals('123', $payload->get('sub_appid'));
self::assertEquals('1600314070', $payload->get('sub_mchid'));
}
}

View File

@ -15,4 +15,9 @@ class WechatGeneralPluginStub extends GeneralPlugin
{
return 'yansongda/pay';
}
protected function getPartnerUri(Rocket $rocket): string
{
return 'yansongda/pay/partner';
}
}

View File

@ -20,6 +20,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
],
'wechat' => [
'default' => [
'app_id' => 'yansongda',
'mp_app_id' => 'wx55955316af4ef13',
'mch_id' => '1600314069',
'mini_app_id' => 'wx55955316af4ef14',
@ -43,7 +44,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
],
'sub_mp_app_id' => 'wx55955316af4ef15',
'sub_app_id' => 'wx55955316af4ef16',
'sub_mini_appid' => 'wx55955316af4ef17',
'sub_mini_app_id' => 'wx55955316af4ef17',
'sub_mch_id' => '1600314070',
'mode' => Pay::MODE_SERVICE,
]