Subscribe to PHP Freaks RSS

Replacing a built-in PHP function when testing a component

syndicated from planet-php.net on October 17, 2018

Recently I needed to test part of Slim that uses the built-in PHP functions header() and headers_sent(). To do this, I took advantage of PHP's namespace resolution rules where it will find a function within the same namespace first before finding one with the same name in the global namespace. The idea of how to do this came courtesy of Matthew Weier O'Phinney where this approach is used for similar testing in Zend-Diactoros.

This is the relevant part of the code I want to test:

namespace Slim;

// use statements...

class App { // ...

public function respond(ResponseInterface $response) { // Send response if (!headers_sent()) { // Headers foreach ($response->getHeaders() as $name => $values) { $first = stripos($name, 'Set-Cookie') === 0 ? false : true; foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), $first); $first = false; } } }

// method continues }

// ... }

This is the relevant test (simplified):

namespace Slim\Tests;

// use statements...

class AppTest extends PHPUnit_Framework_TestCase { // ...

public function testResponseReplacesPreviouslySetHeaders() { $app = new App();

$response = new Response(); $response = $response->withHeader('X-Foo', 'baz1') ->withAddedHeader('X-Foo', 'baz2');

$app->respond($response);

$expectedStack = [ ['header' => 'X-Foo: baz1', 'replace' => true, 'status_code' => null], ['header' => 'X-Foo: baz2', 'replace' => false, 'status_code' => null], ];

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

// ... }

This code sets up a Response object with two X-Foo headers, it then calls respond() and tests that there were two calls to header() with the replace parameter set to true only for the first one.

For this to work, we need to override PHP's header() and replace it with our own that stores the parameters into an array within the HeaderStackTestAsset class.

This is done by creating our own HeaderStackTestAsset class along with headers_sent() & header() functions that are called rather than PHP's:

<?php
// test/Assets/HeaderFunctions.php

namespace Slim\Tests\Assets {

class HeaderStackTestAsset { public static $headers = []; } }

namespace Slim {

function headers_sent() { return false; }

function header($header, $replace = true, $statusCode = null) { \Slim\Tests\Assets\HeaderStackTestAsset::$headers[] = [ 'header' => $header, 'replace' => $replace, 'status_code' => $statusCode, ]; } }

The HeaderStackTestAsset class is trivial and exists entirely as a holder for our $headers array.

We then define our headers_sent() function to always return false and then header() is set up to store the function parameters to the HeaderStackTestAsset::$headers array.

The key thing here is that the headers_sent() & header() functions are in the Slim namespace. As App is also in the same namespace, when header() is called within respond(), our version is invoked and so we can ensure that Slim does the right thing with the $replace parameter.

This is a very handy trick when you need it and works for any PHP function that's called in the global namespace (as long as the file doesn't explicitly import it using use).