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 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-07-17 15:37:43 +08:00
|
|
|
use Yansongda\Pay\Tests\TestCase;
|
2021-06-08 22:23:29 +08:00
|
|
|
use Yansongda\Supports\Collection;
|
2021-06-07 23:48:55 +08:00
|
|
|
|
|
|
|
class AbstractProviderTest extends TestCase
|
|
|
|
{
|
|
|
|
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()
|
|
|
|
{
|
2021-07-17 12:42:03 +08:00
|
|
|
$response = new Response(200, [], 'yansongda/pay');
|
2021-06-07 23:48:55 +08:00
|
|
|
$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);
|
|
|
|
|
2021-07-17 12:42:03 +08:00
|
|
|
self::assertEquals('yansongda/pay', $result->getDestination()->getBody()->getContents());
|
2021-06-07 23:48:55 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-06-28 09:31:18 +08:00
|
|
|
public function callback($contents = null, ?array $params = null): Collection
|
2021-06-08 22:23:29 +08:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
}
|