Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
Easy Http SDK
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
4
Issues
4
List
Board
Labels
Milestones
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Back End
Easy Http SDK
Commits
cbeda114
Commit
cbeda114
authored
Apr 26, 2023
by
谢宇轩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 优化代码,重新设计单元测试的模块
parent
f33ef6c0
Changes
9
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
98 additions
and
10 deletions
+98
-10
Application.php
src/Application.php
+0
-1
ApplicationException.php
src/Exception/ApplicationException.php
+1
-1
GuiltyResultException.php
src/Exception/GuiltyResultException.php
+6
-3
SdkException.php
src/Exception/SdkException.php
+1
-1
TimeOutExcetpion.php
src/Exception/TimeOutExcetpion.php
+1
-1
UnknowResultException.php
src/Exception/UnknowResultException.php
+6
-2
Option.php
src/Option.php
+4
-1
ApplicationTest.php
tests/unit/ApplicationTest.php
+0
-0
OptionTest.php
tests/unit/OptionTest.php
+79
-0
No files found.
src/Application.php
View file @
cbeda114
...
...
@@ -14,7 +14,6 @@ use Jiwei\EasyHttpSdk\Exception\SdkException;
use
Jiwei\EasyHttpSdk\Exception\TimeOutExcetpion
;
use
Jiwei\EasyHttpSdk\Exception\UnknowResultException
;
use
Jiwei\EasyHttpSdk\Http\SdkRequest
;
use
Jiwei\EasyHttpSdk\Middleware\JwtMiddleware
;
use
Psr\Cache\CacheItemPoolInterface
;
use
Psr\Http\Client\ClientInterface
;
use
Psr\Http\Message\RequestInterface
;
...
...
src/Exception/ApplicationException.php
View file @
cbeda114
...
...
@@ -23,7 +23,7 @@ class ApplicationException extends ConnectException implements SdkExceptionInter
string
$message
,
SdkRequest
$request
=
null
,
array
$handlerContext
=
[],
?
\
Throwable
$previous
=
null
?
Throwable
$previous
=
null
)
{
$this
->
endpoint
=
is_null
(
$request
)
?
""
:
$request
->
getEndpoint
();
$this
->
action
=
is_null
(
$request
)
?
""
:
$request
->
getAction
();
...
...
src/Exception/GuiltyResultException.php
View file @
cbeda114
...
...
@@ -18,8 +18,12 @@ class GuiltyResultException extends UnexpectedValueException
* @param int $code
* @param Throwable|null $previous
*/
public
function
__construct
(
$message
=
""
,
$options
=
[],
$code
=
0
,
Throwable
$previous
=
null
)
{
public
function
__construct
(
string
$message
=
""
,
array
$options
=
[],
int
$code
=
0
,
Throwable
$previous
=
null
)
{
$this
->
options
=
$options
;
parent
::
__construct
(
$message
,
$code
,
$previous
);
}
...
...
@@ -31,5 +35,4 @@ class GuiltyResultException extends UnexpectedValueException
{
return
$this
->
options
;
}
}
src/Exception/SdkException.php
View file @
cbeda114
...
...
@@ -27,7 +27,7 @@ class SdkException extends BadResponseException implements SdkExceptionInterface
SdkRequest
$request
,
ResponseInterface
$response
,
array
$handlerContext
=
[],
?
\
Throwable
$previous
=
null
?
Throwable
$previous
=
null
)
{
$this
->
endpoint
=
$request
->
getEndpoint
();
...
...
src/Exception/TimeOutExcetpion.php
View file @
cbeda114
...
...
@@ -22,7 +22,7 @@ class TimeOutExcetpion extends ApplicationException implements SdkExceptionInter
*/
public
function
__construct
(
SdkRequest
$request
,
?
\Throwable
$previous
=
null
,
?
Throwable
$previous
=
null
)
{
parent
::
__construct
(
$this
->
message
,
$request
,
[],
$previous
);
...
...
src/Exception/UnknowResultException.php
View file @
cbeda114
...
...
@@ -18,8 +18,12 @@ class UnknowResultException extends UnexpectedValueException
* @param int $code
* @param Throwable|null $previous
*/
public
function
__construct
(
$message
=
""
,
$result
=
""
,
$code
=
0
,
Throwable
$previous
=
null
)
{
public
function
__construct
(
string
$message
=
""
,
string
$result
=
""
,
int
$code
=
0
,
Throwable
$previous
=
null
)
{
$this
->
result
=
$result
;
parent
::
__construct
(
$message
,
$code
,
$previous
);
}
...
...
src/Option.php
View file @
cbeda114
...
...
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace
Jiwei\EasyHttpSdk
;
use
Closure
;
use
InvalidArgumentException
;
use
Jiwei\EasyHttpSdk\Middleware\Auth\AuthMiddlewareInterface
;
use
Jiwei\EasyHttpSdk\Middleware\Auth\JwtMiddleware
;
use
Jiwei\EasyHttpSdk\Policy\DefaultErrorHandlingPolicy
;
...
...
@@ -76,6 +77,9 @@ abstract class Option
*/
public
function
setStage
(
string
$stage
)
:
self
{
if
(
empty
(
static
::
ENDPOINT_HOSTS
[
$stage
])){
throw
new
InvalidArgumentException
(
"set an undefine stage"
);
}
$this
->
stage
=
$stage
;
return
$this
;
}
...
...
@@ -113,7 +117,6 @@ abstract class Option
*/
public
function
getBaseUrl
()
:
string
{
return
static
::
ENDPOINT_HOSTS
[
$this
->
getStage
()]
??
""
;
}
...
...
tests/unit/
Base
Test.php
→
tests/unit/
Application
Test.php
View file @
cbeda114
File moved
tests/unit/OptionTest.php
0 → 100644
View file @
cbeda114
<?php
use
Jiwei\EasyHttpSdk\Option
;
use
PHPUnit\Framework\TestCase
;
final
class
OptionTest
extends
TestCase
{
protected
mixed
$option
=
null
;
/**
* 测试Option实现实例的初始化(以匿名函数形式)
*
* @return void
*/
public
function
setUp
()
:
void
{
$this
->
option
=
new
class
()
extends
Option
{
/** @var string API鉴权地址 */
private
const
AUTH_API_ROUTE
=
"/api/access/token"
;
/** @var int API鉴权过期时间 */
const
AUTH_CACHE_EXPIRES_AT
=
2400
;
/** @var array<string, string> API HOST */
const
ENDPOINT_HOSTS
=
[
"local"
=>
"127.0.0.1"
,
"development"
=>
"dev.test.com"
,
"production"
=>
"pro.test.com"
,
];
public
function
authorization
()
:
Closure
{
return
function
()
{
echo
sprintf
(
"根据Auth API[%s]获取了TOKEN"
,
self
::
AUTH_API_ROUTE
);
return
"i am a token"
;
};
}
};
}
/**
* 测试
*
* @return void
*/
public
function
testOptionDefaultDebug
()
{
$checkDebug
=
$this
->
option
->
isDebug
();
$this
->
assertEquals
(
$checkDebug
,
false
,
"默认的 debug 级别应该是 false "
);
}
/**
* Undocumented function
*
* @return void
*/
public
function
testOptionSetUndefineStage
()
{
$defaultStage
=
$this
->
option
->
getStage
();
$this
->
assertEquals
(
$defaultStage
,
"development"
,
"默认的 stage 应该是 development "
);
$this
->
expectException
(
InvalidArgumentException
::
class
);
$this
->
option
->
setStage
(
"WoooHaa"
);
}
/**
* Undocumented function
*
* @return void
*/
public
function
testOptionBaseUrl
()
{
$defaultUrl
=
$this
->
option
->
getBaseUrl
();
$this
->
assertEquals
(
$defaultUrl
,
"dev.test.com"
,
"默认的 stage 应该是 development,所以默认的 url 是 devtest"
);
$this
->
option
->
setStage
(
"production"
);
$proUrl
=
$this
->
option
->
getBaseUrl
();
$this
->
assertEquals
(
$proUrl
,
"pro.test.com"
,
"设置 Pro stage 应该可以正常显示"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment