增加回调逻辑,PHP版本,其他优化

This commit is contained in:
ykxiao
2023-09-08 10:42:34 +08:00
parent c760f26478
commit 540a9d0a58
5 changed files with 102 additions and 140 deletions

View File

@ -9,94 +9,34 @@
namespace Feature;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Mockery\Matcher\AnyArgs;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response;
use Mockery;
use PHPUnit\Framework\TestCase;
use Ykxiao\Dmmes\Exceptions\HttpException;
use Ykxiao\Dmmes\Exceptions\InvalidArgumentException;
use Ykxiao\Dmmes\OrderActions\Order;
class OrderTest extends TestCase
{
public function testGetWeatherWithInvalidType()
{
$w = new Order('mock-key', 'foo');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid type value(base/all): foo');
$w->getProductOrder('SN23454324565432');
$this->fail('Failed to assert getOrder throw exception with invalid argument.');
}
public function testGetWeatherWithInvalidFormat()
{
$w = new Order('mock-key', 'base', 'array');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid response format: array');
$w->getProductOrder('SN23454324565432');
$this->fail('Failed to assert getOrder throw exception with invalid argument.');
}
public function testGetWeatherWithGuzzleRuntimeException()
{
$client = \Mockery::mock(Client::class);
$client->allows()
->get(new AnyArgs())
->andThrow(new \Exception('request timeout'));
$w = \Mockery::mock(Order::class, ['mock-key'])->makePartial();
$w->allows()->getHttpClient()->andReturn($client);
$this->expectException(HttpException::class);
//$this->expectExceptionMessage('request timeout');
$w->getProductOrder('SN23454324565432');
}
public function testGetHttpClient()
{
$w = new Order('mock-key');
// 断言返回结果为 GuzzleHttp\ClientInterface 实例
$this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
}
public function testSetGuzzleOptions()
{
$w = new Order('mock-key');
// 设置参数前timeout 为 null
$this->assertNull($w->getHttpClient()->getConfig('timeout'));
// 设置参数
$w->setGuzzleOptions(['timeout' => 5000]);
// 设置参数后timeout 为 5000
$this->assertSame(5000, $w->getHttpClient()->getConfig('timeout'));
}
/**
* @return void
* @throws GuzzleException
*/
public function testGetProductOrder()
{
$w = new Order('mock-key');
$response = new Response(200, [], '{"success": true}');
$w->getProductOrder('SN23454324565432');
$this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
}
// 创建模拟 http client。
$client = Mockery::mock(Client::class);
public function testCreateProductOrder()
{
$w = new Order('mock-key');
$client->allows()->post('https://api.dev.dwoodauto.com/api/v3/production.status', [
'signature' => 'eretgfdsa34565432b453',
'data' => ['order_sn' => 'w45676543456']
])->andReturn($response);
$data = [
'cube_plan' => 32.12,
'owner' => 'ykxiao'
];
$w->createProductOrder($data);
$this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
$w = Mockery::mock(Order::class, ['mock-key'])->makePartial();
$w->allows()->getHttpClient()->andReturn($client); // $client 为上面创建的模拟实例。
$this->assertSame('', '');
}
}