Added testing.

This commit is contained in:
李铭昕 2019-08-10 08:09:24 +08:00
parent 52b15ee87c
commit 57d45b8d44
2 changed files with 45 additions and 12 deletions

View File

@ -13,8 +13,6 @@ declare(strict_types=1);
namespace Hyperf\HttpServer;
use BadMethodCallException;
use Hyperf\HttpMessage\Cookie\Cookie;
use Hyperf\HttpMessage\Server\Response as ServerResponse;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Exception\Http\EncodingException;
@ -28,18 +26,9 @@ use Hyperf\Utils\Traits\Macroable;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use Psr\Http\Message\StreamInterface;
use SimpleXMLElement;
use Swoole\Http\Response as SwooleResponse;
use function get_class;
/**
* @method void send()
* @method ServerResponse withContent(string $content)
* @method ServerResponse withCookie(Cookie $cookie)
* @method null|SwooleResponse getSwooleResponse()
* @method ServerResponse setSwooleResponse(SwooleResponse $swooleResponse)
* @method void buildSwooleResponse(SwooleResponse $swooleResponse, ServerResponse $response)
*/
class Response implements ResponseInterface
class Response implements PsrResponseInterface, ResponseInterface
{
use Macroable;

View File

@ -12,6 +12,7 @@ declare(strict_types=1);
namespace HyperfTest\HttpServer;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Response;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Context;
@ -129,4 +130,47 @@ class ResponseTest extends TestCase
};
$this->assertSame($expected, $reflectionMethod->invoke($response, $xmlable));
}
public function testToJson()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$psrResponse = new \Hyperf\HttpMessage\Base\Response();
Context::set(PsrResponseInterface::class, $psrResponse);
$response = new Response();
$json = $response->json([
'kstring' => 'string',
'kint1' => 1,
'kint0' => 0,
'kfloat' => 0.12345,
'kfalse' => false,
'ktrue' => true,
'karray' => [
'kstring' => 'string',
'kint1' => 1,
'kint0' => 0,
'kfloat' => 0.12345,
'kfalse' => false,
'ktrue' => true,
],
]);
$this->assertSame('{"kstring":"string","kint1":1,"kint0":0,"kfloat":0.12345,"kfalse":false,"ktrue":true,"karray":{"kstring":"string","kint1":1,"kint0":0,"kfloat":0.12345,"kfalse":false,"ktrue":true}}', $json->getBody()->getContents());
}
public function testPsrResponse()
{
$container = Mockery::mock(ContainerInterface::class);
ApplicationContext::setContainer($container);
$psrResponse = new \Hyperf\HttpMessage\Base\Response();
Context::set(PsrResponseInterface::class, $psrResponse);
$response = new Response();
$response = $response->withBody(new SwooleStream('xxx'));
$this->assertInstanceOf(PsrResponseInterface::class, $response);
}
}