0
votes

When running a full test suite in PHPUnit that is defined in my phpunit.xml all of my unit tests run and pass.

If I then run a particular group of tests I get a Fatal error as classes I am trying to mock can't be found.

I have a Bootstrap.php file that set's up an autoloader and from what I can see the Bootstrap is used in both cases.

Anyone experienced this before, or have any suggestions?

1
Maybe show your test that is failing, bootstrap.php and the command line you are using? - Steven Scott

1 Answers

0
votes

I have had that error happen on occasion. Without know more about your autoloader or your setup, you can fix the problem with your mocks by using disableAutoload on the classes.

Using the MockBuilder interface:

$mock = $this->getMockBuilder('SomeClass')->disableAutoload()->getMock();

Or

$mock = $this->getMock('SomeClass', array(), null, null, true, true, true)
                                                disables Autoload ----^

http://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs.examples.StubTest.php

  • By default, all methods of the given class are replaced with a test double that just returns NULL unless a return value is configured using will($this->returnValue()), for instance.

  • When the second (optional) parameter is provided, only the methods whose names are in the array are replaced with a configurable test double. The behavior of the other methods is not changed. Providing NULL as the parameter means that no methods will be replaced.

  • The third (optional) parameter may hold a parameter array that is passed to the original class' constructor (which is not replaced with a dummy implementation by default).

  • The fourth (optional) parameter can be used to specify a class name for the generated test double class.

  • The fifth (optional) parameter can be used to disable the call to the original class' constructor.

  • The sixth (optional) parameter can be used to disable the call to the original class' clone constructor.

  • The seventh (optional) parameter can be used to disable __autoload() during the generation of the test double class.