Unit testing in Yii2 with Mockery Instance Mocks and PHPUnit

I am trying to setup unit testing in my API which runs on Yii2. I am not using Codeception because I am not as familiar with it and PHPStorm has more support for vanilla PHPUnit. I am using Mockery to setup Instance Mocks using the overload keyword as seen below.


$segmentMock = \Mockery::mock('overload:app\modules\schedules\models\Segment');

The expectation is setup like this:




$segmentMock->shouldReceive('getSegCount')

                    ->with($o, $d, $fd, $fn)

                    ->andReturn($count);



The test is running fine as long as we are only running one test. However, we are trying to run multiple tests that are going to have different mocks of the same object. When we try to run multiple tests, it fails with an error that says:


yii\base\ErrorException : Cannot redeclare class app\modules\schedules\models\Segment

The mockery documentation does say that this would happen but then it says

The PHPUnit documentation says to run them in separate processes you just have to add @runTestsInSeparateProcesses in the comment block for the class. When I do that, however, I am getting an InvalidRouteException:


An Error occurred while handling another error:

exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".' in /mnt/hgfs/api/vendor/yiisoft/yii2/base/Module.php:460



I am running the tests through the integrated tools in PHPStorm and have it configured to just test the single class that we are looking at. I am guessing that something PHPUnit is doing to run the separate processes is causing the system to throw the route exception but I am not sure what I would need to do to fix that so it can be run in multiple processes. I am not sure if the boostrap.php file could be used to resolve this issue but just in case this is what it looks like:




require('../vendor/autoload.php');

require('../vendor/yiisoft/yii2/Yii.php');


$config = require('../config/web.php');


(new yii\web\Application($config));



Has anyone run into this kind of issue or have an idea of what I can do to resolve it?

Thanks!!