Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit 6.2 Fatal error: Class 'Eloquent' not found in /.../Database/Model.php

In PHPUnit 5.x I was having no issues. Once I upgraded to 6.2 I started getting the following error: Fatal error: Class 'Eloquent' not found in /my/app/Database/Model.php on line 14 I performed a composer dump-autoload already. Because it was working before, I assume it's because of a change in the newer version to how the TestCase.php should be set up, but I can't find anything solid.

In the test itself, I am calling the parent setup method like this

Test setup method:

public function setUp()
    {
        parent::setUp();
    }

TestCase.php:

class TestCase extends Illuminate\Foundation\Testing\TestCase {
    protected $baseUrl = 'http://whatever.thing';
    public $user;
    public $password;
    public $mockUser;
    public function createApplication() {
        //Server address used by the test suite's instance of $_SERVER.
        $_SERVER['REMOTE_ADDR'] = isset($_SERVER['REMOTE_ADDR'])? $_SERVER['REMOTE_ADDR']:'127.0.0.1';

        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }

}
like image 845
Justin Anthony Avatar asked Jan 01 '26 02:01

Justin Anthony


1 Answers

I figured out what went wrong during the upgrade. First, I had a model class that was extending Eloquent like this:

use Eloquent;

I changed it to this and it worked:

use Illuminate\Database\Eloquent\Model as Eloquent;

Then, I generated a fresh Laravel 5.4 project and looked at how they were implementing the new version of PHPUnit. I followed the new structure, as well as changed up my TestCase.php to the new way forward. For anyone looking:

TestCase.php:

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

CreatesApplication.php:

namespace Tests;

use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}
like image 170
Justin Anthony Avatar answered Jan 04 '26 11:01

Justin Anthony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!