Skip to content

Commit

Permalink
test: 新增单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaojie committed Jun 18, 2024
1 parent dbefede commit aeb0b04
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 6 deletions.
5 changes: 0 additions & 5 deletions src/Plugin/Epay/AddRadarPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public function assembly(Rocket $rocket, Closure $next): Rocket
return $next($rocket);
}

protected function getMethod(array $params): string
{
return strtoupper($params['_method'] ?? 'POST');
}

protected function getHeaders(): array
{
return [
Expand Down
7 changes: 7 additions & 0 deletions tests/Plugin/Epay/AddRadarPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@ public function testRadarPostNormal()
self::assertEquals('https://epaytest.jsbchina.cn:9999/eis/merchant/merchantServices.htm', (string) $result->getRadar()->getUri());
self::stringContains('name=yansongda', (string) $result->getRadar()->getBody());
self::assertEquals('POST', $result->getRadar()->getMethod());

//是否存在Content-Type和User-Agent
self::assertArrayHasKey('Content-Type', $result->getRadar()->getHeaders());
self::assertArrayHasKey('User-Agent', $result->getRadar()->getHeaders());
//验证值
self::assertEquals('text/html', $result->getRadar()->getHeader('Content-Type')[0]);
self::assertEquals('yansongda/pay-v3', $result->getRadar()->getHeader('User-Agent')[0]);
}
}
105 changes: 105 additions & 0 deletions tests/Plugin/Epay/CallbackPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
namespace Yansongda\Pay\Tests\Plugin\Epay;

use GuzzleHttp\Psr7\ServerRequest;
use Yansongda\Artful\Contract\ConfigInterface;
use Yansongda\Artful\Exception\InvalidConfigException;
use Yansongda\Artful\Exception\InvalidParamsException;
use Yansongda\Artful\Exception\InvalidResponseException;
use Yansongda\Artful\Rocket;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Plugin\Epay\CallbackPlugin;
use Yansongda\Pay\Tests\TestCase;
use Yansongda\Supports\Collection;
use Yansongda\Supports\Config;

class CallbackPluginTest extends TestCase
{
Expand Down Expand Up @@ -44,4 +51,102 @@ public function testNormal()
self::assertNotEmpty($result->getPayload()->all());
}

public function testErrorSign()
{
self::expectException(InvalidConfigException::class);
self::expectExceptionCode(Exception::SIGN_ERROR);
$payload = [
'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710',
'orderStatus'=> '1',
'totalFee'=> '0.02',
'outTradeNo'=> 'RC240613164110030315',
'orderNo'=> '20240613164114400729509',
'field1'=> '2',
'field2'=> '',
'field3'=> '20240613164139|20240613164134400800219',
'signType'=> 'RSA',
'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0='
];
$request = new ServerRequest('POST', 'http://localhost');
$request = $request->withParsedBody($payload);

$rocket = new Rocket();
$rocket->setParams(['request'=>Collection::wrap($request->getParsedBody())]);

$result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;});

self::assertNotEmpty($result->getPayload()->all());
}

public function testEmptySign()
{
self::expectException(InvalidResponseException::class);
self::expectExceptionCode(Exception::SIGN_ERROR);
$payload = [
'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710',
'orderStatus'=> '1',
'totalFee'=> '0.02',
'outTradeNo'=> 'RC240613164110030316',
'orderNo'=> '20240613164114400729509',
'field1'=> '2',
'field2'=> '',
'field3'=> '20240613164139|20240613164134400800219',
'signType'=> 'RSA',
'sign'=> ''
];
$request = new ServerRequest('POST', 'http://localhost');
$request = $request->withParsedBody($payload);

$rocket = new Rocket();
$rocket->setParams(['request'=>Collection::wrap($request->getParsedBody())]);

$result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;});

self::assertNotEmpty($result->getPayload()->all());
}

public function testErrorRequestType()
{
self::expectException(InvalidParamsException::class);
self::expectExceptionCode(Exception::PARAMS_CALLBACK_REQUEST_INVALID);
$payload = [
];
$request = new ServerRequest('POST', 'http://localhost');
$request = $request->withParsedBody($payload);

$rocket = new Rocket();
$rocket->setParams(['request'=>$request->getParsedBody()]);

$result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;});

self::assertNotEmpty($result->getPayload()->all());
}

public function testMissingEpayPublicCertPath()
{
self::expectException(InvalidConfigException::class);
self::expectExceptionCode(Exception::CONFIG_EPAY_INVALID);
Pay::set(ConfigInterface::class, new Config());
$payload = [
'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710',
'orderStatus'=> '1',
'totalFee'=> '0.02',
'outTradeNo'=> 'RC240613164110030316',
'orderNo'=> '20240613164114400729509',
'field1'=> '2',
'field2'=> '',
'field3'=> '20240613164139|20240613164134400800219',
'signType'=> 'RSA',
'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0='
];
$request = new ServerRequest('POST', 'http://localhost');
$request = $request->withParsedBody($payload);

$rocket = new Rocket();
$rocket->setParams(['request'=>Collection::wrap($request->getParsedBody())]);

$result = $this->plugin->assembly($rocket, function ($rocket) {return $rocket;});

self::assertNotEmpty($result->getPayload()->all());
}
}
1 change: 1 addition & 0 deletions tests/Plugin/Epay/ResponsePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function testHttpWrong()
{
self::expectException(InvalidResponseException::class);
self::expectExceptionCode(Exception::RESPONSE_ERROR);
self::expectExceptionMessage('epay返回状态码异常,请检查参数是否错误');
$body = 'errCode=1&field1=&field2=&field3=&orderNo=20240617144526400259379&orderStatus=1&outTradeNo=YC202406170003&partnerId=6a13eab71c4f4b0aa4757eda6fc59710&payUrl=http://weixintest.jsbchina.cn/epcs/qr/login.htm?qrCode=2018060611052793473720240617144526688568&respBizDate=20240617&respCode=000000&respMsg=交易成功&totalFee=0.01&validTime=2&signType=RSA&sign=jN3Ha6J9UUIe9M0L/XeexEdaRL9GB6nMV12wNC7LQvTS6V4nKHj4Qzw6M8cNsA9L0Tb3QFT83B0qO3FJnruDrcHKqBLZb4FkoKKN/WiDBuA2UZQjG4+CBejoGJWfpkWSsei9tXUk36TB27lc2ZlYXSEwuuDwM7M9yvlYysc3fjg=';

$rocket = (new Rocket())->setDestinationOrigin(new Response(500, [], $body));
Expand Down
2 changes: 2 additions & 0 deletions tests/Plugin/Epay/VerifySignaturePluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public function testMulconnectorSign()
public function testEmptySign()
{
self::expectException(InvalidSignException::class);
self::expectExceptionCode(Exception::RESPONSE_MISSING_NECESSARY_PARAMS);

$body = 'errCode=1&field1=&field2=&field3=&orderNo=20240617144526400259379&orderStatus=1&outTradeNo=YC202406170003&partnerId=6a13eab71c4f4b0aa4757eda6fc59710&payUrl=http://weixintest.jsbchina.cn/epcs/qr/login.htm?qrCode=2018060611052793473720240617144526688568&respBizDate=20240617&respCode=000000&respMsg=交易成功&totalFee=0.01&validTime=2';

$rocket = (new Rocket())->setDestinationOrigin(new Response(200, [], $body));
Expand Down
52 changes: 51 additions & 1 deletion tests/Provider/EpayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\ServerRequest;
use Mockery;
use Psr\Http\Message\ResponseInterface;
use Yansongda\Artful\Contract\HttpClientInterface;
Expand Down Expand Up @@ -125,7 +126,7 @@ public function testClose()
{
$this->expectException(InvalidParamsException::class);

Pay::epay()->cancel([
Pay::epay()->close([
'outTradeNo' => 'YC202406170003',
]);
}
Expand Down Expand Up @@ -153,6 +154,55 @@ public function testCallback()

}

public function testCallbackByFromGlobals()
{
$payload = [
'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710',
'orderStatus'=> '1',
'totalFee'=> '0.02',
'outTradeNo'=> 'RC240613164110030316',
'orderNo'=> '20240613164114400729509',
'field1'=> '2',
'field2'=> '',
'field3'=> '20240613164139|20240613164134400800219',
'signType'=> 'RSA',
'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0='
];
$_POST = array_merge($_POST, $payload);
$result = Pay::epay()->callback();
self::assertNotEmpty($result->all());
self::assertArrayHasKey('outTradeNo', $result->all());
self::assertArrayHasKey('orderNo', $result->all());
self::assertArrayHasKey('orderStatus', $result->all());
self::assertArrayHasKey('field3', $result->all());

}

public function testCallbackByServerRequest()
{
$payload = [
'partnerId'=> '6a13eab71c4f4b0aa4757eda6fc59710',
'orderStatus'=> '1',
'totalFee'=> '0.02',
'outTradeNo'=> 'RC240613164110030316',
'orderNo'=> '20240613164114400729509',
'field1'=> '2',
'field2'=> '',
'field3'=> '20240613164139|20240613164134400800219',
'signType'=> 'RSA',
'sign'=> 'DPKX4mZAVd/LwMDOt1OJgryBuPeH78y7B78smze+m+vvzae5MBf0O3BoTvVJQHD/RPVftHVvnYHeKvIjCC2bCrxoY9Sv2N8Hbr5HfjIikk0a2qaIQp6TTvecMP9JitzSuZP+sih+uxMkRM5Nrg8weGbePaQ6nODNWiSGDhV+Jq0='
];
$serverRequest = new ServerRequest('POST', 'http://localhost', [], null);
$serverRequest = $serverRequest->withParsedBody($payload);
$result = Pay::epay()->callback($serverRequest);
self::assertNotEmpty($result->all());
self::assertArrayHasKey('outTradeNo', $result->all());
self::assertArrayHasKey('orderNo', $result->all());
self::assertArrayHasKey('orderStatus', $result->all());
self::assertArrayHasKey('field3', $result->all());

}

public function testSuccess()
{
$result = Pay::epay()->success();
Expand Down

0 comments on commit aeb0b04

Please sign in to comment.