Summer Sale! All accounts are 50% off this week.

ixudra's avatar

Mockery: cannot redeclare method

Updated my composer file and suddenly all my tests fail:

- AccountControllerTest: Index
PHP Fatal error:  Cannot redeclare Mockery_0_Illuminate_Auth_Access_Gate::allows() in /var/www/project/htdocs/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php(34) : eval()'d code on line 1003
PHP Stack trace:
PHP   1. {main}() /var/www/project/htdocs/vendor/codeception/codeception/codecept:0
PHP   2. Codeception\Application->run() /var/www/project/htdocs/vendor/codeception/codeception/codecept:43
PHP   3. Codeception\Application->run() /var/www/project/htdocs/vendor/codeception/codeception/src/Codeception/Application.php:108
PHP   4. Codeception\Application->doRun() /var/www/project/htdocs/vendor/symfony/console/Application.php:145
PHP   5. Codeception\Application->doRunCommand() /var/www/project/htdocs/vendor/symfony/console/Application.php:269
PHP   6. Codeception\Command\Run->run() /var/www/project/htdocs/vendor/symfony/console/Application.php:908
PHP   7. Codeception\Command\Run->execute() /var/www/project/htdocs/vendor/symfony/console/Command/Command.php:255
PHP   8. Codeception\Command\Run->runSuites() /var/www/project/htdocs/vendor/codeception/codeception/src/Codeception/Command/Run.php:390
PHP   9. Codeception\Codecept->run() /var/www/project/htdocs/vendor/codeception/codeception/src/Codeception/Command/Run.php:495
PHP  10. Codeception\Codecept->runSuite() /var/www/project/htdocs/vendor/codeception/codeception/src/Codeception/Codecept.php:159
PHP  11. Codeception\SuiteManager->run() /var/www/project/htdocs/vendor/codeception/codeception/src/Codeception/Codecept.php:192
PHP  12. Codeception\PHPUnit\Runner->doEnhancedRun() /var/www/project/htdocs/vendor/codeception/codeception/src/Codeception/SuiteManager.php:158
PHP  13. Codeception\Suite->run() /var/www/project/htdocs/vendor/codeception/phpunit-wrapper/src/Runner.php:118
PHP  14. AccountControllerTest->run() /var/www/project/htdocs/vendor/phpunit/phpunit/src/Framework/TestSuite.php:746
PHP  15. PHPUnit\Framework\TestResult->run() /var/www/project/htdocs/vendor/phpunit/phpunit/src/Framework/TestCase.php:796
PHP  16. AccountControllerTest->runBare() /var/www/project/htdocs/vendor/phpunit/phpunit/src/Framework/TestResult.php:687
PHP  17. AccountControllerTest->setUp() /var/www/project/htdocs/vendor/phpunit/phpunit/src/Framework/TestCase.php:838
PHP  18. Illuminate\Support\Facades\Facade::shouldReceive() /var/www/project/htdocs/tests/unit/base/BaseUnitTestCase.php:16
PHP  19. Illuminate\Support\Facades\Facade::createFreshMockInstance() /var/www/project/htdocs/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:66
PHP  20. Illuminate\Support\Facades\Facade::createMock() /var/www/project/htdocs/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:78
PHP  21. Mockery::mock() /var/www/project/htdocs/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:94
PHP  22. Mockery\Container->mock() /var/www/project/htdocs/vendor/mockery/mockery/library/Mockery.php:117
PHP  23. Mockery\Loader\EvalLoader->load() /var/www/project/htdocs/vendor/mockery/mockery/library/Mockery/Container.php:224

In EvalLoader.php(34) : eval()'d code line 1003:

Cannot redeclare Mockery_0_Illuminate_Auth_Access_Gate::allows()

Anyone got any ideas?

  • Laravel: 5.7.28
  • Mockery: 1.2.2
  • Codeception: 2.2.5
0 likes
4 replies
ixudra's avatar

@secmuhammed I don't remember specifically what the problem was, it's been too long. But if recall correctly, it was something to do with the versions of Laravel, Codeception and Mockery. I'd recommend to update everything to the most recent version, see if that fixes things.

Good luck

secmuhammed's avatar

unfortunately, everything is up to date and still the problem exists

felixpenrose's avatar

This is a bit old, but incase anyone stumbles across it. This is down to the fact that Laravel Facades are already mock'able. https://laravel.com/docs/10.x/mocking#mocking-facades

What this means is that instead of writing something like this

$mock = Mockery::mock(Log::class);
$mock->shouldReceive('info');

You can just write this, and it would have the same effect (without the error as above)

Log::shouldReceive('info');
1 like

Please or to participate in this conversation.