pay/tests/Provider/AbstractProviderTest.php

156 lines
3.9 KiB
PHP
Raw Normal View History

2021-06-07 23:48:55 +08:00
<?php
namespace Yansongda\Pay\Tests\Provider;
use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\HttpClientInterface;
use Yansongda\Pay\Contract\PluginInterface;
2021-06-10 11:33:08 +08:00
use Yansongda\Pay\Exception\InvalidConfigException;
2021-06-07 23:48:55 +08:00
use Yansongda\Pay\Parser\NoHttpRequestParser;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Provider\AbstractProvider;
use Yansongda\Pay\Rocket;
2021-06-08 22:23:29 +08:00
use Yansongda\Supports\Collection;
2021-06-07 23:48:55 +08:00
class AbstractProviderTest extends TestCase
{
protected function setUp(): void
{
$config = [
'alipay' => [
'default' => [
'app_public_cert_path' => __DIR__.'/../Stubs/cert/appCertPublicKey_2016082000295641.crt',
'alipay_public_cert_path' => __DIR__.'/../Stubs/cert/alipayCertPublicKey_RSA2.crt',
'alipay_root_cert_path' => __DIR__.'/../Stubs/cert/alipayRootCert.crt',
],
]
];
Pay::config($config);
}
protected function tearDown(): void
{
Pay::clear();
}
public function testVerifyObjectPlugin()
{
$plugin = [new FooPlugin()];
$provider = new FooProviderStub();
$result = $provider->pay($plugin, []);
self::assertInstanceOf(ResponseInterface::class, $result);
}
public function testVerifyCallablePlugin()
{
$plugin = [function ($rocket, $next) {
$rocket->setDirection(NoHttpRequestParser::class)
->setDestination(new Response());
return $next($rocket);
}];
$provider = new FooProviderStub();
$result = $provider->pay($plugin, []);
self::assertInstanceOf(ResponseInterface::class, $result);
}
public function testVerifyNormalPlugin()
{
$plugin = [FooPlugin::class];
$provider = new FooProviderStub();
$result = $provider->pay($plugin, []);
self::assertInstanceOf(ResponseInterface::class, $result);
}
public function testIgnite()
{
$response = new Response();
$rocket = new Rocket();
$rocket->setRadar(new Request('get', ''));
$http = Mockery::mock(Client::class);
$http->shouldReceive('sendRequest')->andReturn($response);
Pay::set(HttpClientInterface::class, $http);
$provider = new FooProviderStub();
$result = $provider->ignite($rocket);
self::assertSame($response, $result->getDestination());
}
2021-06-10 11:33:08 +08:00
public function testIgniteWrongHttpClient()
{
$rocket = new Rocket();
$rocket->setRadar(new Request('get', ''));
Pay::set(HttpClientInterface::class, new Collection());
self::expectException(InvalidConfigException::class);
self::expectExceptionCode(InvalidConfigException::HTTP_CLIENT_CONFIG_ERROR);
$provider = new FooProviderStub();
$provider->ignite($rocket);
}
2021-06-07 23:48:55 +08:00
}
class FooProviderStub extends AbstractProvider
{
2021-06-08 22:23:29 +08:00
public function find($order): Collection
{
2021-06-17 09:48:38 +08:00
return new Collection();
2021-06-08 22:23:29 +08:00
}
public function cancel($order): Collection
{
2021-06-17 09:48:38 +08:00
return new Collection();
2021-06-08 22:23:29 +08:00
}
public function close($order): Collection
{
2021-06-17 09:48:38 +08:00
return new Collection();
2021-06-08 22:23:29 +08:00
}
public function refund(array $order): Collection
{
2021-06-17 09:48:38 +08:00
return new Collection();
2021-06-08 22:23:29 +08:00
}
public function verify($contents = null, ?array $params = null): Collection
{
2021-06-17 09:48:38 +08:00
return new Collection();
2021-06-08 22:23:29 +08:00
}
public function success(): ResponseInterface
{
}
2021-06-17 09:48:38 +08:00
public function mergeCommonPlugins(array $plugins): array
{
return [];
}
2021-06-07 23:48:55 +08:00
}
2021-06-17 09:48:38 +08:00
2021-06-07 23:48:55 +08:00
class FooPlugin implements PluginInterface
{
public function assembly(Rocket $rocket, Closure $next): Rocket
{
$rocket->setDirection(NoHttpRequestParser::class)
->setDestination(new Response());
return $next($rocket);
}
}