Skip to content

Commit

Permalink
Merge branch 'tidy-up-header-test-assets' into 3.x
Browse files Browse the repository at this point in the history
Closes #2520
  • Loading branch information
akrabat committed Nov 27, 2018
2 parents 46229b1 + 874bbcb commit b1d3475
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 63 deletions.
10 changes: 5 additions & 5 deletions tests/AppTest.php
Expand Up @@ -26,7 +26,7 @@
use Slim\Http\Response;
use Slim\Http\Uri;
use Slim\Router;
use Slim\HeaderStackTestAsset;
use Slim\Tests\Assets\HeaderStack;
use Slim\Tests\Mocks\MockAction;

/**
Expand All @@ -43,12 +43,12 @@ class AppTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
HeaderStackTestAsset::reset();
HeaderStack::reset();
}

public function tearDown()
{
HeaderStackTestAsset::reset();
HeaderStack::reset();
}

public static function setupBeforeClass()
Expand Down Expand Up @@ -1797,7 +1797,7 @@ public function testResponseReplacesPreviouslySetHeaders()
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
];

$this->assertSame($expectedStack, HeaderStackTestAsset::stack());
$this->assertSame($expectedStack, HeaderStack::stack());
}

public function testResponseDoesNotReplacePreviouslySetSetCookieHeaders()
Expand Down Expand Up @@ -1834,7 +1834,7 @@ public function testResponseDoesNotReplacePreviouslySetSetCookieHeaders()
['header' => 'HTTP/1.1 200 OK', 'replace' => true, 'status_code' => 200],
];

$this->assertSame($expectedStack, HeaderStackTestAsset::stack());
$this->assertSame($expectedStack, HeaderStack::stack());
}

public function testExceptionErrorHandlerDoesNotDisplayErrorDetails()
Expand Down
61 changes: 3 additions & 58 deletions tests/Assets/HeaderFunctions.php
Expand Up @@ -8,6 +8,8 @@
*/
namespace Slim;

use Slim\Tests\Assets\HeaderStack;

/**
* Zend Framework (http://framework.zend.com/)
*
Expand All @@ -28,63 +30,6 @@
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
*/

/**
* Store output artifacts
*/
class HeaderStackTestAsset
{
/**
* @var string[][]
*/
private static $data = [];

/**
* Reset state
*/
public static function reset()
{
self::$data = [];
}

/**
* Push a header on the stack
*
* @param string[] $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) {
if ($item['header'] === $header) {
return true;
}
}

return false;
}
}

/**
* Have headers been sent?
*
Expand All @@ -104,7 +49,7 @@ function headers_sent()
*/
function header($string, $replace = true, $statusCode = null)
{
HeaderStackTestAsset::push(
HeaderStack::push(
[
'header' => $string,
'replace' => $replace,
Expand Down
84 changes: 84 additions & 0 deletions tests/Assets/HeaderStack.php
@@ -0,0 +1,84 @@
<?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.
*
*/
namespace Slim\Tests\Assets;

/**
* 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 string[] $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) {
if ($item['header'] === $header) {
return true;
}
}

return false;
}
}

0 comments on commit b1d3475

Please sign in to comment.