News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Matthew Weier O'Phinney:
Testing Code That Emits Output
August 25, 2014 @ 09:45:08

In this latest post to his site Matthew Weier O'Phinney gives his suggestion on how to test (unit test) code that provides some kind of direct output. In his case, his script is outputting header information directly, not as a part of a response string.

Here's the scenario: you have code that will emit headers and content, for instance, a front controller. How do you test this? The answer is remarkably simple, but non-obvious: namespaces.

He talks some about the use of namespaces in PHP classes (and methods, and constants...) and how things can be importing using them. He gives an example of an object that outputs some header and body information (an "Output" abstract class). He shows how to use the class in a simple test, calling "reset" in the setup and teardown methods and asserting the contents of the headers and body for expected content.

0 comments voice your opinion now!
test unittest code phpunit output direct namespace tutorial

Link: http://mwop.net/blog/2014-08-11-testing-output-generating-code.html

blog comments powered by Disqus

Similar Posts

Zend Developer Zone: Creating Multi-Language Web Applications with Zend_Translate

Evert Pot's Blog: Creating a Gopher server with PHP and InetD

Carson McDonald's Blog: Google OAuth for Installed Apps PHP Example

PHPBuilder.com: Loops & Decisions in PHP - The ABC's of PHP Part 8

SitePoint PHP Blog: Bitcoin and PHP with Coinbase’s API – Demo App


Community Events

Don't see your event here?
Let us know!


unittest voicesoftheelephpant framework conference podcast laravel5 psr7 language api release extension community library introduction wordpress interview development laravel series opinion

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework