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;
|
2022-03-05 21:59:11 +08:00
|
|
|
use Yansongda\Pay\Exception\Exception;
|
2021-06-10 11:33:08 +08:00
|
|
|
use Yansongda\Pay\Exception\InvalidConfigException;
|
2022-09-12 23:11:53 +08:00
|
|
|
use Yansongda\Pay\Parser\ArrayParser;
|
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);
|
|
|
|
|
2022-07-21 22:42:59 +08:00
|
|
|
self::assertEquals('yansongda/pay', (string) $result->getDestination()->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIgnitePreRead()
|
|
|
|
{
|
|
|
|
$response = new Response(200, [], 'yansongda/pay');
|
|
|
|
$response->getBody()->read(1);
|
|
|
|
|
|
|
|
$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::assertEquals('yansongda/pay', (string) $result->getDestination()->getBody());
|
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);
|
2022-03-05 21:59:11 +08:00
|
|
|
self::expectExceptionCode(Exception::HTTP_CLIENT_CONFIG_ERROR);
|
2021-06-10 11:33:08 +08:00
|
|
|
|
|
|
|
$provider = new FooProviderStub();
|
|
|
|
$provider->ignite($rocket);
|
|
|
|
}
|
2022-09-12 23:11:53 +08:00
|
|
|
|
|
|
|
public function testArrayDirection()
|
|
|
|
{
|
|
|
|
$response = new Response(200, [], '{"name":"yansongda"}');
|
|
|
|
|
|
|
|
$http = Mockery::mock(Client::class);
|
|
|
|
$http->shouldReceive('sendRequest')->andReturn($response);
|
|
|
|
|
|
|
|
Pay::set(HttpClientInterface::class, $http);
|
|
|
|
|
|
|
|
$plugin = [BarPlugin::class];
|
|
|
|
|
|
|
|
$provider = new FooProviderStub();
|
|
|
|
$result = $provider->pay($plugin, []);
|
|
|
|
|
|
|
|
self::assertIsArray($result);
|
|
|
|
}
|
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
|
|
|
|
{
|
2022-07-21 22:42:59 +08:00
|
|
|
return new Response(
|
|
|
|
200,
|
|
|
|
['Content-Type' => 'application/json'],
|
|
|
|
json_encode(['code' => 'SUCCESS', 'message' => '成功']),
|
|
|
|
);
|
2021-06-08 22:23:29 +08:00
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 23:11:53 +08:00
|
|
|
|
|
|
|
class BarPlugin implements PluginInterface
|
|
|
|
{
|
|
|
|
public function assembly(Rocket $rocket, Closure $next): Rocket
|
|
|
|
{
|
|
|
|
$rocket->setDirection(ArrayParser::class)
|
|
|
|
->setRadar(new Request('get', ''));
|
|
|
|
|
|
|
|
$rocket = $next($rocket);
|
|
|
|
|
|
|
|
$rocket->setDestination(new Collection(['name' => 'yansongda']));
|
|
|
|
|
|
|
|
return $rocket;
|
|
|
|
}
|
|
|
|
}
|