To understand this behavior, you need to see how PHPUnit works. getMockBuilder()->getMock() , dynamically creates the following code for the mock class:
class Mock_MyClass_2568ab4c extends MyClass implements PHPUnit_Framework_MockObject_MockObject { private static $__phpunit_staticInvocationMocker; private $__phpunit_invocationMocker; public function __clone() { $this->__phpunit_invocationMocker = clone $this->__phpunit_getInvocationMocker(); } public function sayHello() { $arguments = array(); $count = func_num_args(); if ($count > 0) { $_arguments = func_get_ ...
If MyClass is not already loaded at this time, it adds the following declaration:
class MyClass { }
This code will be processed using eval() (!).
Since PHPUnit will execute an InjectTest before MyClassTest , this means that the builder layout will define a dummy class, and MyClass already defined when MyClassTest::setUpBeforeClass is called. That is why the error. Hope I could explain. Otherwise, dig out the PHPUnit code.
Decision:
Drop the setUpBeforeClass() method and put the require_once statement on top of the tests. setUpBeforeClass() not intended to include classes. Refer to the docs .
Btw, having require_once on top, will work because PHPUnit will include every test file before starting the first test.
Tests / MyClassTest.php
require_once __DIR__ . '/../src/MyClass.php'; class MyClassTest extends PHPUnit_Framework_TestCase { private $subject; public function setUp() { $this->subject = new MyClass(); } public function testSayHello() { $this->assertEquals('Hello world', $this->subject->sayHello()); } }
Tests / InjectTest.php
require_once __DIR__ . '/../src/Inject.php'; class InjectTest extends PHPUnit_Framework_TestCase { public function testPrintGreetings() { $myClassMock = $this ->getMockBuilder('MyClass') ->setMethods(array('sayHello')) ->getMock(); $myClassMock ->expects($this->once()) ->method('sayHello') ->will($this->returnValue(TRUE)); $subject = new Inject($myClassMock); $this->assertEquals(TRUE, $subject->printGreetings()); } }
source share