delete: 移除 Yansongda\Pay\Direction\ArrayDirection 类 (#818)

This commit is contained in:
yansongda 2023-07-01 23:22:37 +08:00 committed by GitHub
parent de827f7a59
commit 25830a9cff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 24 additions and 154 deletions

View File

@ -1,3 +1,9 @@
## v3.5.0
### deleted
- deleted: 移除 `Yansongda\Pay\Direction\ArrayDirection` 类(#818)
## v3.4.2
### changed

View File

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace Yansongda\Pay\Direction;
use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\DirectionInterface;
use Yansongda\Pay\Contract\PackerInterface;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidResponseException;
class ArrayDirection implements DirectionInterface
{
/**
* @throws InvalidResponseException
*/
public function parse(PackerInterface $packer, ?ResponseInterface $response): array
{
if (is_null($response)) {
throw new InvalidResponseException(Exception::RESPONSE_NONE);
}
$body = (string) $response->getBody();
if (!is_null($result = $packer->unpack($body))) {
return $result;
}
throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]);
}
}

View File

@ -7,21 +7,27 @@ namespace Yansongda\Pay\Direction;
use Psr\Http\Message\ResponseInterface;
use Yansongda\Pay\Contract\DirectionInterface;
use Yansongda\Pay\Contract\PackerInterface;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\ServiceNotFoundException;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Supports\Collection;
class CollectionDirection implements DirectionInterface
{
/**
* @throws ContainerException
* @throws ServiceNotFoundException
* @throws InvalidResponseException
*/
public function parse(PackerInterface $packer, ?ResponseInterface $response): Collection
{
return new Collection(
Pay::get(ArrayDirection::class)->parse($packer, $response)
);
if (is_null($response)) {
throw new InvalidResponseException(Exception::RESPONSE_NONE);
}
$body = (string) $response->getBody();
if (!is_null($result = $packer->unpack($body))) {
return new Collection($result);
}
throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]);
}
}

View File

@ -12,7 +12,6 @@ use Yansongda\Pay\Contract\HttpClientInterface;
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Contract\ProviderInterface;
use Yansongda\Pay\Contract\ShortcutInterface;
use Yansongda\Pay\Direction\ArrayDirection;
use Yansongda\Pay\Event;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\Exception;
@ -77,13 +76,7 @@ abstract class AbstractProvider implements ProviderInterface
Event::dispatch(new Event\PayFinish($rocket));
$destination = $rocket->getDestination();
if (ArrayDirection::class === $rocket->getDirection() && $destination instanceof Collection) {
return $destination->toArray();
}
return $destination;
return $rocket->getDestination();
}
/**

View File

@ -1,81 +0,0 @@
<?php
namespace Yansongda\Pay\Tests\Direction;
use GuzzleHttp\Psr7\Response;
use Yansongda\Pay\Exception\Exception;
use Yansongda\Pay\Exception\InvalidResponseException;
use Yansongda\Pay\Packer\JsonPacker;
use Yansongda\Pay\Packer\QueryPacker;
use Yansongda\Pay\Direction\ArrayDirection;
use Yansongda\Pay\Tests\TestCase;
class ArrayDirectionTest extends TestCase
{
protected ArrayDirection $parser;
protected function setUp(): void
{
parent::setUp();
$this->parser = new ArrayDirection();
}
public function testResponseNull()
{
self::expectException(InvalidResponseException::class);
self::expectExceptionCode(Exception::RESPONSE_NONE);
$this->parser->parse(new JsonPacker(), null);
}
public function testWrongFormat()
{
self::expectException(InvalidResponseException::class);
self::expectExceptionCode(Exception::UNPACK_RESPONSE_ERROR);
$response = new Response(200, [], '{"name": "yansongda"}a');
$this->parser->parse(new JsonPacker(), $response);
}
public function testNormal()
{
$response = new Response(200, [], '{"name": "yansongda"}');
$result = $this->parser->parse(new JsonPacker(), $response);
self::assertEquals(['name' => 'yansongda'], $result);
}
public function testReadContents()
{
$response = new Response(200, [], '{"name": "yansongda"}');
$response->getBody()->read(2);
$result = $this->parser->parse(new JsonPacker(), $response);
self::assertEquals(['name' => 'yansongda'], $result);
}
public function testQueryBody()
{
$response = new Response(200, [], 'name=yansongda&age=29');
$result = $this->parser->parse(new QueryPacker(), $response);
self::assertEqualsCanonicalizing(['name' => 'yansongda', 'age' => '29'], $result);
}
public function testJsonWith()
{
$url = 'https://yansongda.cn?name=yansongda&age=29';
$response = new Response(200, [], json_encode(['h5_url' => $url]));
$result = $this->parser->parse(new JsonPacker(), $response);
self::assertEquals('https://yansongda.cn?name=yansongda&age=29', $result['h5_url']);
}
}

View File

@ -14,7 +14,6 @@ use Yansongda\Pay\Contract\HttpClientInterface;
use Yansongda\Pay\Contract\PackerInterface;
use Yansongda\Pay\Contract\PluginInterface;
use Yansongda\Pay\Contract\ShortcutInterface;
use Yansongda\Pay\Direction\ArrayDirection;
use Yansongda\Pay\Direction\CollectionDirection;
use Yansongda\Pay\Direction\NoHttpRequestDirection;
use Yansongda\Pay\Exception\Exception;
@ -121,23 +120,6 @@ class AbstractProviderTest extends TestCase
$provider->ignite($rocket);
}
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);
}
public function testNoCommonPlugins()
{
$provider = new Foo2ProviderStub();
@ -212,8 +194,7 @@ class BarPlugin implements PluginInterface
{
public function assembly(Rocket $rocket, Closure $next): Rocket
{
$rocket->setDirection(ArrayDirection::class)
->setRadar(new Request('get', ''));
$rocket->setRadar(new Request('get', ''));
$rocket = $next($rocket);

View File

@ -48,15 +48,12 @@ ThinkPHP 框架在 [https://github.com/top-think/framework/pull/2614](https://gi
## array
API 调用场景下的返回类型,`array` 和 `Collection` 是可以自定义的,默认情况下均返回 `Collection` 实例。
API 调用场景下的返回类型,默认情况下均返回 `Collection` 实例。
如果想返回 array 类型的数据,只需要
```php
use Yansongda\Pay\Contract\DirectionInterface;
use Yansongda\Pay\Direction\ArrayDirection;
Pay::set(DirectionInterface::class, ArrayDirection::class);
$collection->toArray();
```
是不是很简单方便?