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

Rob Allen:
Replacing a built-in PHP function when testing a component
Oct 22, 2018 @ 15:55:58

Rob Allen has a new post to his site sharing a method you can use in your testing to replace a built-in PHP function with something customized for your needs.

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.

He starts off with the code he wants to test - a response method - and a simplified version of the test. This method makes use of the headers_sent and header functions in PHP but those needed to be overridden in order to make the test actually work. He includes the changes to make to the test to override these methods because of how namespaces resolve (using the global PHP namespace last).

tagged: replace builtinfunction tutorial namespace testing unittest slim

Link: https://akrabat.com/replacing-a-built-in-php-function-when-testing-a-component/


Trending Topics: