Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use PHPUnit to test a function if that function is supposed to kill PHP?

Essentially I have a method of a class called killProgram, which is intended to send a hTTP redirect and then kill PHP.

How am I supposed to test this? When I run phpunit it doesn't return anything for that test, and closes completely.

Right now I'm considering having the killProgram function throw an exception which shouldn't get handled, which would allow me to assert that an exception was thrown.

Is there a better way?

like image 523
Zee Spencer Avatar asked Aug 28 '09 15:08

Zee Spencer


People also ask

How do I run a PHPUnit test?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is a PHPUnit test?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

Why do we use PHPUnit?

PHPUnit is used for unit testing your PHP projects. The word Unit refers to a block of code, method or an individual or independent class. Unit testing is a software testing process in which code blocks are checked to see whether the produced result matches the expectations.

What is assertion in PHPUnit?

The assertion methods are declared static and can be invoked from any context using PHPUnit\Framework\Assert::assertTrue() , for instance, or using $this->assertTrue() or self::assertTrue() , for instance, in a class that extends PHPUnit\Framework\TestCase .


2 Answers

It's obviously an old question but my suggestion would be to move the code that die()'s into a separate method that you can then mock.

As an example, instead of having this:

class SomeClass {     public function do()     {         exit(1);         // or         die('Message');     } } 

do this:

class SomeClass {     public function do()     {         $this->terminate(123);         // or         $this->terminate('Message');     }      protected function terminate($code = 0)     {         exit($code);     }      // or      protected function terminate($message = '')     {         die($message);     } } 

That way you can easily mock the terminate method and you don't have to worry about the script terminating without you being able to catch it.

Your test would look something like this:

class SomeClassTest extends \PHPUnit_Framework_TestCase {      /**      * @expectedExceptionCode 123      */     public function testDoFail()     {         $mock = $this->getMock('SomeClass');         $mock->expects($this->any())              ->method('terminate')              ->will($this->returnCallback(function($code) {                  throw new \Exception($code);              }));          // run to fail         $mock->do();     } } 

I haven't tested the code but should be pretty close to a working state.

like image 126
Andris Avatar answered Sep 21 '22 14:09

Andris


As every tests are run by the same PHPUnit process, if you use exit/die in your PHP code, you will kill everything -- as you noticed ^^

So, you have to find another solution, yes -- like returning instead of dying ; or throwing an exception (you can test if some tested code has thrown an expected exception).

Maybe PHPUnit 3.4 and it's --process-isolation switch (see Optionally execute each test using a separate PHP process) might help (by not having everything dying), but you still wouldn't be able to get the result of the test, if PHPUnit doesn't get the control back.

I've had this problem a couple of times ; solved it by returning instead of dying -- even returning several times, if needed, to go back "high enough" in the call stack ^^
In the end, I suppose I don't have any "die" anymore in my application... It's probably better, when thinking about MVC, btw.

like image 37
Pascal MARTIN Avatar answered Sep 22 '22 14:09

Pascal MARTIN