简体   繁体   中英

Laravel 5 Unit Testing and Namespacing

I'm running into a few issues regarding PHPUnit in Laravel 5 with Namespacing. I'm attempting to do a simple test on one of my routes, which of course calls a Model, but I'm getting an unexpected error when it makes the call.

PHP Fatal error: Class 'Eloquent' not found in /home/app/Models/User.php on line 10

Here is the relevant code block from User.php

<?php

use Laravel\Cashier\Billable;
use Laravel\Cashier\Contracts\Billable as BillableContract;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends \Eloquent implements AuthenticatableContract, CanResetPasswordContract, BillableContract {

It appears that I'm having some issues with Namespacing in the Testing environment. I do not want to change my Model, as it works precisely as I'd like it to with my application.

How can I make the PHPUnit environment interact with my application without making direct changes to the application?

My guess is this isn't a namespace issue, but rather that you're not (either deliberately or inadvertently due to some deviation from what Laravel considers the norm) using Laravel's base test classes, or if you are you've somehow modified them such that you're not bootstrapping a Laravel environment in your tests. (describing how you're creating your tests would help people track this down)

The problem is there's no global Eloquent class in Laravel. There is, however, an Eloquent alias. If you take a look at the app.php config file, you'll see an array of aliases.

#File: config\app.php
'aliases' => [
    //...
    'Eloquent'  => 'Illuminate\Database\Eloquent\Model',
    //...

Laravel sets up these aliases using the built in PHP function class_alias

class_alias('Illuminate\Database\Eloquent\Model', 'Eloquent');

This is what lets you use Eloquent as though it was a class. (This is actually done though the means of the class alias autoloader ).

For some reason this isn't happening in your testing environment. If you can't figure out why, I'd try the following at the start of your test

class_alias('Illuminate\Database\Eloquent\Model', 'Eloquent');

That will manually setup the alias, which may let you run your tests. (However, if you're using Laravel models and other framework features in your tests, you'll really want to figure out why your test environment isn't bootstrapping a Laravel environment)

Try adding parent::setUp() in your test's setUp function as well to make sure your testing environment gets setup properly.

/**
 * Default preparation for each test
 *
 */
public function setUp()
{
    parent::setUp(); // Don't forget this!

    $this->prepareForTests();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM