-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
631 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Tests\Application\ResponseEmitter; | ||
|
||
use App\Application\ResponseEmitter\ResponseEmitter; | ||
use Tests\Assets\HeaderStack; | ||
use Tests\TestCase; | ||
|
||
|
||
class ResponseEmitterTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
HeaderStack::reset(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
HeaderStack::reset(); | ||
} | ||
|
||
public function testEmit() | ||
{ | ||
$response = $this->createResponse(); | ||
$response->getBody()->write('Hello'); | ||
|
||
$responseEmitter = new ResponseEmitter(); | ||
$responseEmitter->emit($response); | ||
|
||
$expectedStack = [ | ||
['header' => 'Access-Control-Allow-Credentials: true', 'replace' => true, 'status_code' => null], | ||
['header' => 'Access-Control-Allow-Origin: ', 'replace' => true, 'status_code' => null], | ||
['header' => 'Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization', 'replace' => true, 'status_code' => null], | ||
['header' => 'Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONS', 'replace' => true, 'status_code' => null], | ||
['header' => 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0', 'replace' => true, 'status_code' => null], | ||
['header' => 'Cache-Control: post-check=0, pre-check=0', 'replace' => false, 'status_code' => null], | ||
['header' => 'Pragma: no-cache', 'replace' => true, 'status_code' => null], | ||
['header' => 'HTTP/1.1 200 ', 'replace' => true, 'status_code' => 200], | ||
]; | ||
$this->assertSame($expectedStack, HeaderStack::stack()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/** | ||
* This is a direct copy of zend-diactoros/test/TestAsset/Functions.php and is used to override | ||
* header() and headers_sent() so we can test that they do the right thing. | ||
* | ||
* We put these into the Slim namespace, so that Slim\App will use these versions of header() and | ||
* headers_sent() when we test its output. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Assets; | ||
|
||
use function explode; | ||
use function trim; | ||
|
||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* This file exists to allow overriding the various output-related functions | ||
* in order to test what happens during the `Server::listen()` cycle. | ||
* | ||
* These functions include: | ||
* | ||
* - headers_sent(): we want to always return false so that headers will be | ||
* emitted, and we can test to see their values. | ||
* - header(): we want to aggregate calls to this function. | ||
* | ||
* The HeaderStack class then aggregates that information for us, and the test | ||
* harness resets the values pre and post test. | ||
* | ||
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
/** | ||
* Store output artifacts | ||
*/ | ||
class HeaderStack | ||
{ | ||
/** | ||
* @var string[][] | ||
*/ | ||
private static $data = []; | ||
|
||
/** | ||
* Reset state | ||
*/ | ||
public static function reset() | ||
{ | ||
self::$data = []; | ||
} | ||
|
||
/** | ||
* Push a header on the stack | ||
* | ||
* @param array $header | ||
*/ | ||
public static function push(array $header) | ||
{ | ||
self::$data[] = $header; | ||
} | ||
|
||
/** | ||
* Return the current header stack | ||
* | ||
* @return string[][] | ||
*/ | ||
public static function stack() | ||
{ | ||
return self::$data; | ||
} | ||
|
||
/** | ||
* Verify if there's a header line on the stack | ||
* | ||
* @param string $header | ||
* | ||
* @return bool | ||
*/ | ||
public static function has($header) | ||
{ | ||
foreach (self::$data as $item) { | ||
$components = explode(':', $item['header']); | ||
if (trim($components[0]) === $header) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/** | ||
* Slim Framework (https://slimframework.com) | ||
* | ||
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers; | ||
|
||
use Nyholm\Psr7\Factory\Psr17Factory; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestFactoryInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
use function array_merge; | ||
use function microtime; | ||
use function time; | ||
|
||
/** | ||
* Class PSR7ObjectProvider | ||
* | ||
* @package Slim\Tests\Providers | ||
*/ | ||
class PSR7ObjectProvider implements PSR7ObjectProviderInterface | ||
{ | ||
/** | ||
* @param string $uri | ||
* @param string $method | ||
* @param array $data | ||
* @return ServerRequestInterface | ||
*/ | ||
public function createServerRequest( | ||
string $uri, | ||
string $method = 'GET', | ||
array $data = [] | ||
): ServerRequestInterface { | ||
$headers = array_merge([ | ||
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | ||
'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', | ||
'HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8', | ||
'HTTP_HOST' => 'localhost', | ||
'HTTP_USER_AGENT' => 'Slim Framework', | ||
'QUERY_STRING' => '', | ||
'REMOTE_ADDR' => '127.0.0.1', | ||
'REQUEST_METHOD' => $method, | ||
'REQUEST_TIME' => time(), | ||
'REQUEST_TIME_FLOAT' => microtime(true), | ||
'REQUEST_URI' => '', | ||
'SCRIPT_NAME' => '/index.php', | ||
'SERVER_NAME' => 'localhost', | ||
'SERVER_PORT' => 80, | ||
'SERVER_PROTOCOL' => 'HTTP/1.1', | ||
], $data); | ||
|
||
return $this | ||
->getServerRequestFactory() | ||
->createServerRequest($method, $uri, $headers); | ||
} | ||
|
||
/** | ||
* @return ServerRequestFactoryInterface | ||
*/ | ||
public function getServerRequestFactory(): ServerRequestFactoryInterface | ||
{ | ||
return new Psr17Factory(); | ||
} | ||
|
||
/** | ||
* @param int $statusCode | ||
* @param string $reasonPhrase | ||
* @return ResponseInterface | ||
*/ | ||
public function createResponse(int $statusCode = 200, string $reasonPhrase = ''): ResponseInterface | ||
{ | ||
return $this | ||
->getResponseFactory() | ||
->createResponse($statusCode, $reasonPhrase); | ||
} | ||
|
||
/** | ||
* @return ResponseFactoryInterface | ||
*/ | ||
public function getResponseFactory(): ResponseFactoryInterface | ||
{ | ||
return new Psr17Factory(); | ||
} | ||
|
||
/** | ||
* @param string $contents | ||
* @return StreamInterface | ||
*/ | ||
public function createStream(string $contents = ''): StreamInterface | ||
{ | ||
return $this | ||
->getStreamFactory() | ||
->createStream($contents); | ||
} | ||
|
||
/** | ||
* @return StreamFactoryInterface | ||
*/ | ||
public function getStreamFactory(): StreamFactoryInterface | ||
{ | ||
return new Psr17Factory(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* Slim Framework (https://slimframework.com) | ||
* | ||
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Providers; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestFactoryInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
|
||
/** | ||
* Interface PSR7ObjectProviderInterface | ||
* | ||
* @package Slim\Tests\Providers | ||
*/ | ||
interface PSR7ObjectProviderInterface | ||
{ | ||
/** | ||
* @return ServerRequestFactoryInterface | ||
*/ | ||
public function getServerRequestFactory(): ServerRequestFactoryInterface; | ||
|
||
/** | ||
* @return ResponseFactoryInterface | ||
*/ | ||
public function getResponseFactory(): ResponseFactoryInterface; | ||
|
||
/** | ||
* @return StreamFactoryInterface | ||
*/ | ||
public function getStreamFactory(): StreamFactoryInterface; | ||
|
||
/** | ||
* @param string $uri | ||
* @param string $method | ||
* @param array $data | ||
* @return ServerRequestInterface | ||
*/ | ||
public function createServerRequest(string $uri, string $method = 'GET', array $data = []): ServerRequestInterface; | ||
|
||
/** | ||
* @param int $statusCode | ||
* @param string $reasonPhrase | ||
* @return ResponseInterface | ||
*/ | ||
public function createResponse(int $statusCode = 200, string $reasonPhrase = ''): ResponseInterface; | ||
|
||
/** | ||
* @param string $contents | ||
* @return StreamInterface | ||
*/ | ||
public function createStream(string $contents = ''): StreamInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,31 @@ | ||
<?php | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
use AdrianSuter\Autoload\Override\Override; | ||
use Slim\ResponseEmitter; | ||
use Tests\Assets\HeaderStack; | ||
|
||
$classLoader = require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
Override::apply($classLoader, [ | ||
ResponseEmitter::class => [ | ||
'connection_status' => function (): int { | ||
if (isset($GLOBALS['connection_status_return'])) { | ||
return $GLOBALS['connection_status_return']; | ||
} | ||
|
||
return connection_status(); | ||
}, | ||
'header' => function (string $string, bool $replace = true, int $statusCode = null): void { | ||
HeaderStack::push( | ||
[ | ||
'header' => $string, | ||
'replace' => $replace, | ||
'status_code' => $statusCode, | ||
] | ||
); | ||
}, | ||
'headers_sent' => function (): bool { | ||
return false; | ||
} | ||
] | ||
]); |