PHPUnit Test Suite - Cannot Update Mocking & Concrete Class Classes

Here is my problem.

I have a test suite that tests several classes. My classes use dependency injection.

I have a class called scheduleHandler that passes all the tests. Then my other ruleHandler rule has a method that requires an instance of scheduleHandler. I don’t want to go through a real handler schedule, so I tried to create a scheduleHandler layout for input.

The problem is that since the scheduleHandler class is tested in the set above ruleHandler when the mock is created, I get: -

PHP Fatal error: Cannot redeclare class scheduleHandler 

If I do not use the test suite and run the tests individually, everything is fine.

Does anyone know a way around this?

+6
source share
3 answers

My best guess is:

 var_dump(class_exists('scheduleHandler', false)); 

returns false for you. This means that the class does not exist yet. Now, if your autoloader does not find the class when phpunit tries to extend it, phpunit will create the class itself.

If you are later on the road, then you will need a REAL class, from which those to the classes will collide.

To verify this, make sure you require your REAL scheduleHandler class before creating the layout.

+4
source

Try using namespaces in your Mock creation. If you do not use them in your project code, then I hope it redefines the global namespace and does not cause a conflict

$this->getMock('\SomeTestingFramework\SomeTestClass\scheduleHandler');

+3
source

Try $this->getMock('scheduleHandler', array(), array(), '', false) . This will force PHPUnit to skip the call to scheduleHandler::__construct , which probably caused the error by loading the class twice.

0
source

Source: https://habr.com/ru/post/899139/


All Articles