Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpStorm, PHPUnit and setcookie

I'm trying to make some unit tests using a setcookie() function in a pretty good IDE PhpStorm. But I get a following error every time:

Cannot modify header information - headers already sent by (output started at /tmp/phpunit.php:418)

Probably the reason of this error is print('some text') with flush() before setcookie() calling. But flushing is performed in a /tmp/phpunit.php file generated by PhpStorm. While setcookie() is called from my sources. So I can't edit the generated file to do some kind of output buffering. Also there is some another moment: PhpStorm executes /tmp/phpunit.php script like this:

/usr/bin/php /tmp/phpunit.php -config /var/www/.../protected/tests/phpunit.xml d /var/www/.../protected/tests/unit/user

Please help me to workaround this issue. How can I run unit tests from PhpStorm directly?

like image 740
alexey_detr Avatar asked Mar 29 '11 09:03

alexey_detr


People also ask

How do I run a test case in Phpstorm?

Create a test configuration Open the Run/Debug Configuration dialog by doing one of the following: From the list on the main toolbar, select Run | Edit Configurations. From the main menu, select Run | Edit Configurations. Press Alt+Shift+F10 and select Edit Configuration from the context menu.

Can not find PHPUnit library to install use PHP composer Phar install?

First go to Settings -> Languages and Frameworks -> PHP and Add a remote interpreter, then go to Settings -> Languages and Frameworks -> PHP -> PHPUnit click the + on top and click by Remote Interpreter . If you're using Composer autoloader, then enter your full Vagrant path to your autoloader file.


1 Answers

One possible way around this is to use a 'mock' replacement for the setcookie() function.

This is a common technique in unit testing, where you want to test something that relies on an external class or function that you don't want to affect the current test.

The way to do it would be to create a stub function definition for setcookie() within you unit test code. This would then be called during the test instead of the real setcookie() function. Exactly how you implement this stub function is up to you, and would depend on what your code uses it for.

The major problem you'll have with this approach is that PHP doesn't allow you to override existing functions by default - if you try this on a standard PHP installation, you'll get "error: function cannot be redeclared".

The solution to this problem is PHP's Runkit extension, which is explicitly designed for this kind of testing, and allows you to rename an existing function, including built-in ones.

If you configure the PHP installation in your testing environment to include the Runkit extension, you will be able to do this kind of test.

Hope that helps.

like image 59
Spudley Avatar answered Sep 23 '22 05:09

Spudley