Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Commit

Permalink
Use PSR7 instead of Slim (#10)
Browse files Browse the repository at this point in the history
* Use PSR7 as response

* replace response in test
  • Loading branch information
nick-zh authored and pachico committed Jun 12, 2019
1 parent 7436675 commit c552c91
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
],
"require": {
"php": "^7.0",
"dflydev/fig-cookies": "^1.0"
"dflydev/fig-cookies": "^1.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"eaglewu/swoole-ide-helper": "dev-master",
Expand Down
22 changes: 11 additions & 11 deletions src/Bridge/ResponseMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Pachico\SlimSwoole\Bridge;

use Slim\App;
use Slim\Http;
use Psr\Http\Message\ResponseInterface;
use swoole_http_response;

class ResponseMerger implements ResponseMergerInterface
Expand All @@ -22,39 +22,39 @@ public function __construct(App $app)
}

/**
* @param Http\Response $slimResponse
* @param Response $response
* @param swoole_http_response $swooleResponse
*
* @return swoole_http_response
*/
public function mergeToSwoole(
Http\Response $slimResponse,
ResponseInterface $response,
swoole_http_response $swooleResponse
): swoole_http_response {
$container = $this->app->getContainer();

$settings = $container->get('settings');
if (isset($settings['addContentLengthHeader']) && $settings['addContentLengthHeader'] == true) {
$size = $slimResponse->getBody()->getSize();
$size = $response->getBody()->getSize();
if ($size !== null) {
$swooleResponse->header('Content-Length', (string) $size);
}
}

if (!empty($slimResponse->getHeaders())) {
foreach ($slimResponse->getHeaders() as $key => $headerArray) {
if (!empty($response->getHeaders())) {
foreach ($response->getHeaders() as $key => $headerArray) {
$swooleResponse->header($key, implode('; ', $headerArray));
}
}

$swooleResponse->status($slimResponse->getStatusCode());
$swooleResponse->status($response->getStatusCode());

if ($slimResponse->getBody()->getSize() > 0) {
if ($slimResponse->getBody()->isSeekable()) {
$slimResponse->getBody()->rewind();
if ($response->getBody()->getSize() > 0) {
if ($response->getBody()->isSeekable()) {
$response->getBody()->rewind();
}

$swooleResponse->write($slimResponse->getBody()->getContents());
$swooleResponse->write($response->getBody()->getContents());
}

return $swooleResponse;
Expand Down
6 changes: 3 additions & 3 deletions src/Bridge/ResponseMergerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Pachico\SlimSwoole\Bridge;

use Slim\Http;
use Psr\Http\Message\ResponseInterface;
use swoole_http_response;

interface ResponseMergerInterface
{
/**
* @param Http\Response $slimResponse
* @param ResponseInterface $response
* @param swoole_http_response $swooleResponse
*
* @return swoole_http_response
*/
public function mergeToSwoole(
Http\Response $slimResponse,
ResponseInterface $response,
swoole_http_response $swooleResponse
): swoole_http_response;
}
27 changes: 14 additions & 13 deletions tests/Unit/Bridge/ResponseMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Pachico\SlimSwooleUnitTest\Bridge;

use Pachico\SlimSwoole\Bridge;
use Psr\Http\Message\ResponseInterface;
use Slim\Http;

class ResponseMergerTest extends \Pachico\SlimSwooleUnitTest\AbstractTestCase
Expand All @@ -11,12 +12,12 @@ class ResponseMergerTest extends \Pachico\SlimSwooleUnitTest\AbstractTestCase
/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
private $swooleResponse;
private $psrResponse;

/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
private $slimResponse;
private $swooleResponse;

/**
* @var PHPUnit_Framework_MockObject_MockObject
Expand Down Expand Up @@ -44,20 +45,20 @@ public function setUp()

$this->swooleResponse = $this->getMockBuilder('\swoole_http_response')->disableOriginalConstructor()->getMock();
$this->body = $this->getMockForAbstractClass(\Psr\Http\Message\StreamInterface::class);
$this->slimResponse = $this->getMockBuilder(Http\Response::class)->disableOriginalConstructor()->getMock();
$this->slimResponse->expects($this->any())->method('getBody')->willReturn($this->body);
$this->psrResponse = $this->getMockBuilder(ResponseInterface::class)->getMockForAbstractClass();
$this->psrResponse->expects($this->any())->method('getBody')->willReturn($this->body);
$this->container = $this->getMockBuilder(\Slim\Container::class)->disableOriginalConstructor()->getMock();
$this->app = $this->getMockBuilder(\Slim\App::class)->disableOriginalConstructor()->getMock();
$this->app->expects($this->any())->method('getContainer')->willReturn($this->container);

$this->sut = new Bridge\ResponseMerger($this->app);
}

public function testMergeToSwooleReturnsSlimResponse()
public function testMergeToSwooleReturnsPsrResponse()
{
// Arrange
// Act
$output = $this->sut->mergeToSwoole($this->slimResponse, $this->swooleResponse);
$output = $this->sut->mergeToSwoole($this->psrResponse, $this->swooleResponse);
// Assert
$this->assertInstanceOf('\swoole_http_response', $output);
}
Expand All @@ -71,7 +72,7 @@ public function testContentLengthGetsCopiedIfSettingsSaySo()
$this->body->expects($this->any())->method('getSize')->willReturn(77);
$this->swooleResponse->expects($headerSpy = $this->once())->method('header')->with('Content-Length', '77');
// Act
$this->sut->mergeToSwoole($this->slimResponse, $this->swooleResponse);
$this->sut->mergeToSwoole($this->psrResponse, $this->swooleResponse);
// Assert
$this->assertSame(1, $headerSpy->getInvocationCount());
}
Expand All @@ -85,21 +86,21 @@ public function testContentLengthDoesNotGetCopiedIfSettingsSaySo()
$this->body->expects($this->any())->method('getSize')->willReturn(77);
$this->swooleResponse->expects($headerSpy = $this->never())->method('header')->with('Content-Length', '77');
// Act
$this->sut->mergeToSwoole($this->slimResponse, $this->swooleResponse);
$this->sut->mergeToSwoole($this->psrResponse, $this->swooleResponse);
// Assert
$this->assertSame(0, $headerSpy->getInvocationCount());
}

public function testHeadersGetCopied()
{
// Arrange
$this->slimResponse->expects($this->any())->method('getHeaders')->willReturn([
$this->psrResponse->expects($this->any())->method('getHeaders')->willReturn([
'foo' => ['bar'],
'fiz' => ['bam']
]);
$this->swooleResponse->expects($headerSpy = $this->exactly(2))->method('header');
// Act
$this->sut->mergeToSwoole($this->slimResponse, $this->swooleResponse);
$this->sut->mergeToSwoole($this->psrResponse, $this->swooleResponse);
// Assert
$this->assertSame(2, $headerSpy->getInvocationCount());
}
Expand All @@ -114,7 +115,7 @@ public function testBodyContentGetsCopiedIfNotEmpty()
$this->body->expects($this->once())->method('getContents')->willReturn('abc');
$this->swooleResponse->expects($writeSpy = $this->once())->method('write')->with('abc');
// Act
$this->sut->mergeToSwoole($this->slimResponse, $this->swooleResponse);
$this->sut->mergeToSwoole($this->psrResponse, $this->swooleResponse);

// Assert
$this->assertSame(1, $rewindSpy->getInvocationCount());
Expand All @@ -124,10 +125,10 @@ public function testBodyContentGetsCopiedIfNotEmpty()
public function testStatusCodeGetsCopied()
{
// Arrange
$this->slimResponse->expects($this->once())->method('getStatusCode')->willReturn(400);
$this->psrResponse->expects($this->once())->method('getStatusCode')->willReturn(400);
$this->swooleResponse->expects($setStatusSpy = $this->once())->method('status')->with(400);
// Act
$this->sut->mergeToSwoole($this->slimResponse, $this->swooleResponse);
$this->sut->mergeToSwoole($this->psrResponse, $this->swooleResponse);

// Assert
$this->assertSame(1, $setStatusSpy->getInvocationCount());
Expand Down

0 comments on commit c552c91

Please sign in to comment.