Skip to content

Commit

Permalink
Merge pull request #31 from gaobinzhan/5.x
Browse files Browse the repository at this point in the history
adjust: test
  • Loading branch information
kiss291323003 authored Dec 23, 2020
2 parents 1242c01 + 6a7737c commit 34784ff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
6 changes: 6 additions & 0 deletions src/Client/RequestContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getRequestPath(): string

/**
* @param string $requestPath
* @return $this
*/
public function setRequestPath(string $requestPath): RequestContext
{
Expand All @@ -63,6 +64,7 @@ public function getServiceVersion(): ?int

/**
* @param int|null $serviceVersion
* @return $this
*/
public function setServiceVersion(?int $serviceVersion): RequestContext
{
Expand All @@ -80,6 +82,7 @@ public function getArg()

/**
* @param mixed $arg
* @return $this
*/
public function setArg($arg): RequestContext
{
Expand All @@ -97,6 +100,7 @@ public function getOnSuccess(): ?callable

/**
* @param callable|null $onSuccess
* @return $this
*/
public function setOnSuccess(?callable $onSuccess): RequestContext
{
Expand All @@ -114,6 +118,7 @@ public function getOnFail(): ?callable

/**
* @param callable|null $onFail
* @return $this
*/
public function setOnFail(?callable $onFail): RequestContext
{
Expand All @@ -131,6 +136,7 @@ public function getServiceNode(): ?ServiceNode

/**
* @param ServiceNode|null $serviceNode
* @return $this
*/
public function setServiceNode(?ServiceNode $serviceNode): RequestContext
{
Expand Down
53 changes: 29 additions & 24 deletions tests/ServiceModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,64 @@ function testServiceModuleName()

function testModuleNotFound()
{
$ret = json_decode($this->buildClient('/?path=ServiceOne.ModuleNotFound')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(\EasySwoole\Rpc\Protocol\Response::STATUS_MODULE_NOT_EXIST)
->__toString(),
$this->buildClient('/?path=ServiceOne.ModuleNotFound')
->getBody()
->jsonSerialize(),
$ret
);
}

function testActionNotFound()
{
$ret = json_decode($this->buildClient('/?path=ServiceOne.ModuleOne.notFound')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(\EasySwoole\Rpc\Protocol\Response::STATUS_ACTION_NOT_EXIST)
->__toString(),
$this->buildClient('/?path=ServiceTwo.ModuleOne.notFound')
->getBody()
->jsonSerialize(),
$ret
);
}

function testArgs()
{
$ret = json_decode($this->buildClient('/?path=ServiceOne.ModuleTwo.args&args=[1,2,3]')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
->setStatus(0)
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(\EasySwoole\Rpc\Protocol\Response::STATUS_OK)
->setResult("[1,2,3]")
->__toString(),
$this->buildClient('/?path=ServiceOne.ModuleTwo.args&args=[1,2,3]')
->getBody()
->jsonSerialize(),
$ret
);
}

function testAction()
{
$ret = json_decode($this->buildClient('/?path=ServiceTwo.ModuleOne.action')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
->setStatus(0)
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(\EasySwoole\Rpc\Protocol\Response::STATUS_OK)
->setMsg('hello action')
->__toString(),
$this->buildClient('/?path=ServiceTwo.ModuleOne.action')
->getBody()
->jsonSerialize(),
$ret
);
}

function testOnException()
{
$ret = json_decode($this->buildClient('/?path=ServiceOne.ModuleTwo.exception')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(-1)
->setMsg('the module exception')
->__toString(),
$this->buildClient('/?path=ServiceOne.ModuleTwo.exception')
->getBody()
->jsonSerialize(),
$ret
);
}

Expand All @@ -84,8 +89,8 @@ private function buildClient($path = '/')
return $client;
}

private function buildResponse(): Response
private function buildResponse($data = []): Response
{
return new Response();
return new Response($data);
}
}
22 changes: 12 additions & 10 deletions tests/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,27 @@ function testServiceName()

function testNotFound()
{
$ret = json_decode($this->buildClient('/?path=ServiceNotFound')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(\EasySwoole\Rpc\Protocol\Response::STATUS_NOT_AVAILABLE_NODE)
->__toString(),
$this->buildClient('/?path=ServiceNotFound')
->getBody()
->jsonSerialize(),
$ret
);
}


function testOnException()
{
$ret = json_decode($this->buildClient('/?path=ServiceException')
->getBody(), true);
$this->assertEquals(
$this->buildResponse()
$this->buildResponse(['responseUUID' => $ret['responseUUID']])
->setStatus(-1)
->setMsg('the service exception')
->__toString(),
$this->buildClient('/?path=ServiceException')
->getBody()
->jsonSerialize(),
$ret
);
}

Expand All @@ -48,9 +50,9 @@ private function buildClient($path = '/')
return $client;
}

private function buildResponse(): Response
private function buildResponse($data = []): Response
{
return new Response();
return new Response($data);
}

private function prepareService(): ServiceOne
Expand Down

0 comments on commit 34784ff

Please sign in to comment.