dmmes/tests/Feature/OrderTest.php

91 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* Author: ykxiao
* Date: 2023/9/6
* Time: 19:49
* Description: 工单测试类
*/
namespace Feature;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Mockery\Matcher\AnyArgs;
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'));
}
public function testGetProductOrder()
{
$w = new Order('mock-key');
$w->getProductOrder('SN23454324565432');
$this->assertInstanceOf(ClientInterface::class, $w->getHttpClient());
}
}