6

I have a symfony2 application and I'm using phpunit.

I have some unit tests where I use mocks to mock the AppKernel and functional tests which make "real" requests to the application. When running the unit tests or the functional tests alone, everything works fine.

It get's nasty when I want to run all tests at once. As soon as the unit tests are finished, phpunit stops, telling me:

Fatal error: Cannot redeclare class AppKernel in C:\Users\sebastian\workspace\ppInterface\app\AppKernel.php on line 35

I don't understand this, as I thought phpunit would run each test within it's own environment. This seems to be not the case. What can I do to get things right and "reset" the environment in which the tests run?

1 Answer 1

16

PHPUnit does not reset everything by default, although it's possible.

Includes - the problem in your case - are not resetted (and cannot in one single process). A solution would be to use require_once instead of require, or to use process isolation either in your phpunit.xml file or in the test case (@runTestsInSeparateProcesses) or test method (@runInSeparateProcess).

You can also influence the what is resetted between tests:

  • @backupGlobals
  • @backupStaticAttributes
Sign up to request clarification or add additional context in comments.

3 Comments

@runTestsInSeparateProcesses works great! I added it to the tests and now they all run without problems. I would however like to add it to my phpunit.xml but couldn't find any docs on where to add it. I would like to add it to a group defined within the <groups> in my xml. Is that possible?
Keep in mind that running tests in separate processes will start to slow down considerably as your application grows. You might want to consider running your tests in two groups: ones that use the mocks and ones that do not.
Of course you can configure it in your phpunit.xml: just add processIsolation = "true" to <phpunit>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.