德木自动化MES系统SDK初始版本
This commit is contained in:
commit
a7fe8503fd
|
@ -0,0 +1,3 @@
|
|||
/.idea
|
||||
/vendor
|
||||
composer.lock
|
|
@ -0,0 +1 @@
|
|||
{"version":1,"defects":{"Feature\\OrderTest::testGetProductOrder":5},"times":{"Unit\\OrderTest::testGetWeatherWithInvalidType":0.041,"Feature\\OrderTest::testGetWeatherWithInvalidType":0.026,"Unit\\OrderTest::testGetWeatherWithInvalidFormat":0,"Feature\\OrderTest::testGetWeatherWithInvalidFormat":0,"Feature\\OrderTest::testGetWeatherWithGuzzleRuntimeException":0.074,"Feature\\OrderTest::testGetHttpClient":0.015,"Feature\\OrderTest::testSetGuzzleOptions":0.002,"Unit\\OrderTest::testGetWeatherWithGuzzleRuntimeException":0.101,"Unit\\OrderTest::testGetHttpClient":0.02,"Unit\\OrderTest::testSetGuzzleOptions":0.004,"Feature\\OrderTest::testGetProductOrder":0.202}}
|
|
@ -0,0 +1,25 @@
|
|||
# 德木自动化项目开放接口SDK
|
||||
|
||||
# 介绍
|
||||
用于德木自动化对外开放接口数据交互
|
||||
|
||||
# 要求
|
||||
- php版本:>=7.0
|
||||
|
||||
# 安装
|
||||
|
||||
```php
|
||||
composer require ykxiao/dmmes
|
||||
```
|
||||
|
||||
# 使用
|
||||
|
||||
```php
|
||||
$secretKey = 'gFBfirGATxafTeq74RAngaL74Ksdxhuy';
|
||||
|
||||
$w = new Order($secretKey);
|
||||
|
||||
// 获取工单数据
|
||||
|
||||
$response = $w->getProductOrder('SN5436745676543');
|
||||
```
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "ykxiao/dmmes",
|
||||
"description": "Automation Project SDK",
|
||||
"license": "MIT",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Ykxiao\\Dmmes\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "ykxiao"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^7.8",
|
||||
"ext-json": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.6",
|
||||
"mockery/mockery": "^1.6"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
bootstrap="./vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false">
|
||||
<testsuites>
|
||||
<testsuite name="Feature">
|
||||
<directory>./tests/Feature</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist processUncoveredFilesFromWhitelist="true">
|
||||
<directory suffix=".php">./src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2023/9/7
|
||||
* Time: 09:06
|
||||
* Description: 根异常类
|
||||
*/
|
||||
|
||||
namespace Ykxiao\Dmmes\Exceptions;
|
||||
|
||||
use Exception as BaseException;
|
||||
|
||||
class Exception extends BaseException
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2023/9/7
|
||||
* Time: 09:20
|
||||
* Description:
|
||||
*/
|
||||
|
||||
namespace Ykxiao\Dmmes\Exceptions;
|
||||
|
||||
class HttpException extends Exception
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2023/9/7
|
||||
* Time: 09:19
|
||||
* Description:
|
||||
*/
|
||||
|
||||
namespace Ykxiao\Dmmes\Exceptions;
|
||||
|
||||
class InvalidArgumentException extends Exception
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2023/9/6
|
||||
* Time: 20:01
|
||||
* Description: 工单
|
||||
*/
|
||||
|
||||
namespace Ykxiao\Dmmes\OrderActions;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Ykxiao\Dmmes\Exceptions\HttpException;
|
||||
use Ykxiao\Dmmes\Exceptions\InvalidArgumentException;
|
||||
use function in_array;
|
||||
use function strtolower;
|
||||
|
||||
class Order
|
||||
{
|
||||
protected $guzzleOptions = [];
|
||||
protected $apiUrl = 'https://api.dev.dwoodauto.com/api/v3/';
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $secretKey;
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
private $type;
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
private $format;
|
||||
|
||||
public function __construct($secretKey, $type = 'base', $format = 'json')
|
||||
{
|
||||
$this->secretKey = $secretKey;
|
||||
$this->type = $type;
|
||||
$this->format = $format;
|
||||
}
|
||||
|
||||
public function getHttpClient()
|
||||
{
|
||||
return new Client($this->guzzleOptions);
|
||||
}
|
||||
|
||||
public function setGuzzleOptions(array $options)
|
||||
{
|
||||
$this->guzzleOptions = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $params
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function baseFun($params)
|
||||
{
|
||||
$url = $this->apiUrl . 'production.status';
|
||||
|
||||
if (!in_array(strtolower($this->format), ['xml', 'json'])) {
|
||||
throw new InvalidArgumentException('Invalid response format: ' . $this->format);
|
||||
}
|
||||
|
||||
if (!in_array(strtolower($this->type), ['base', 'all'])) {
|
||||
throw new InvalidArgumentException('Invalid type value(base/all): ' . $this->type);
|
||||
}
|
||||
|
||||
$params['output'] = $this->format;
|
||||
$params['extensions'] = $this->type;
|
||||
|
||||
$signature = (new Sign())->generateHmacSignature($this->secretKey, $params);
|
||||
|
||||
$params = [
|
||||
'headers' => ['content-type' => 'application/json', 'accept' => 'application/json'],
|
||||
'body' => json_encode([
|
||||
'signature' => $signature,
|
||||
'data' => $params
|
||||
]),
|
||||
'http_errors' => false
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->getHttpClient()
|
||||
->request('POST', $url, $params)
|
||||
->getBody()
|
||||
->getContents();
|
||||
|
||||
return 'json' === $this->format ? json_decode($response, true) : $response;
|
||||
} catch (\Exception $e) {
|
||||
throw new HttpException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工单信息
|
||||
* @param $order_sn
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getProductOrder($order_sn)
|
||||
{
|
||||
$data = [
|
||||
'order_sn' => $order_sn
|
||||
];
|
||||
return $this->baseFun($data);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
/**
|
||||
* Author: ykxiao
|
||||
* Date: 2023/9/7
|
||||
* Time: 10:42
|
||||
* Description:
|
||||
*/
|
||||
|
||||
namespace Ykxiao\Dmmes\OrderActions;
|
||||
|
||||
class Sign
|
||||
{
|
||||
/**
|
||||
* 生成HMAC签名
|
||||
* @param $secretKey
|
||||
* @param $data
|
||||
* @return string
|
||||
*/
|
||||
public function generateHmacSignature($secretKey, $data)
|
||||
{
|
||||
return hash_hmac('sha256', json_encode($data), $secretKey);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue