orm
orm copied to clipboard
[BUG] Cannot run unit tests in Lumen
Package Version: 1.3.* Lumen Version: 5.7.* and 5.8.*
Expected Behavior: Tests should run without a problem.
Actual Behavior: Got the following error:
There was 1 error:
1) ExampleTest::testExample
ErrorException: Cannot declare class EntityManager, because the name is already in use
C:\xampp\htdocs\lumen\bootstrap\app.php:103
C:\xampp\htdocs\lumen\tests\TestCase.php:12
C:\xampp\htdocs\lumen\vendor\laravel\lumen-framework\src\Testing\TestCase.php:56
C:\xampp\htdocs\lumen\vendor\laravel\lumen-framework\src\Testing\TestCase.php:73
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
Steps to reproduce the behaviour:
Grab a fresh install of lumen, install this package and configure bootstrap\app.php as documentation explains and run tests. I did not tested with Laravel.
I had a look on this. It seems lumen executes bootstrap\app.php more then once when running tests and class_alias() causes de problem.
So, instead of this:
class_alias('LaravelDoctrine\ORM\Facades\EntityManager', 'EntityManager');
class_alias('LaravelDoctrine\ORM\Facades\Registry', 'Registry');
class_alias('LaravelDoctrine\ORM\Facades\Doctrine', 'Doctrine');
I used this, and the problem was fixed:
if (!class_exists('EntityManager')) {
class_alias('LaravelDoctrine\ORM\Facades\EntityManager', 'EntityManager');
}
if (!class_exists('Registry')) {
class_alias('LaravelDoctrine\ORM\Facades\Registry', 'Registry');
}
if (!class_exists('Doctrine')) {
class_alias('LaravelDoctrine\ORM\Facades\Doctrine', 'Doctrine');
}
Since the docs do not tell to do this in Laravel, I'm not sure if this bugs happens there. But I suppose the same fix should be used in Laravel.
@juliocgs maybe create PR? thx for solution.