Skip to content

Commit

Permalink
Update Deps
Browse files Browse the repository at this point in the history
Add ResponseEmitterTest.php
  • Loading branch information
neilmillard committed Aug 18, 2024
1 parent 9628eca commit 32ab00b
Show file tree
Hide file tree
Showing 8 changed files with 631 additions and 139 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
"zircote/swagger-php": "^4.10"
},
"require-dev": {
"adriansuter/php-autoload-override": "^1.4",
"jangregor/phpstan-prophecy": "^1.0.2",
"nyholm/psr7": "^1.8",
"phpspec/prophecy-phpunit": "^v2.2.0",
"phpstan/extension-installer": "^1.4.1",
"phpstan/phpstan": "^1.10",
Expand Down
416 changes: 278 additions & 138 deletions composer.lock

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions tests/Application/ResponseEmitter/ResponseEmitterTest.php
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());
}
}
93 changes: 93 additions & 0 deletions tests/Assets/HeaderStack.php
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;
}
}
112 changes: 112 additions & 0 deletions tests/Providers/PSR7ObjectProvider.php
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();
}
}
62 changes: 62 additions & 0 deletions tests/Providers/PSR7ObjectProviderInterface.php
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;
}
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
use Exception;
use PHPUnit\Framework\TestCase as PHPUnit_TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Headers;
use Slim\Psr7\Request as SlimRequest;
use Slim\Psr7\Uri;
use Tests\Providers\PSR7ObjectProvider;

class TestCase extends PHPUnit_TestCase
{
Expand Down Expand Up @@ -88,4 +90,15 @@ protected function createRequest(

return new SlimRequest($method, $uri, $h, $cookies, $serverParams, $stream);
}

/**
* @param int $statusCode
* @param string $reasonPhrase
* @return ResponseInterface
*/
protected function createResponse(int $statusCode = 200, string $reasonPhrase = ''): ResponseInterface
{
$psr7ObjectProvider = new PSR7ObjectProvider();
return $psr7ObjectProvider->createResponse($statusCode, $reasonPhrase);
}
}
30 changes: 29 additions & 1 deletion tests/bootstrap.php
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;
}
]
]);

0 comments on commit 32ab00b

Please sign in to comment.