简体   繁体   English

Laravel 5单元测试和命名空间

[英]Laravel 5 Unit Testing and Namespacing

I'm running into a few issues regarding PHPUnit in Laravel 5 with Namespacing. 我正在使用Namespacing在Laravel 5中遇到有关PHPUnit的一些问题。 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 PHP致命错误:在第10行的/home/app/Models/User.php中找不到类'Eloquent'

Here is the relevant code block from User.php 这是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? 如何在不直接更改应用程序的情况下使PHPUnit环境与应用程序交互?

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. 我的猜测是这不是名称空间问题,而是您不会(使用Laravel的基础测试类(有意或无意由于与Laravel认为的规范有些偏差)),或者如果您使用了某种方式,则对它们进行了修改这样您就不会在测试中引导Laravel环境。 (describing how you're creating your tests would help people track this down) (描述您如何创建测试将有助于人们追踪到这一点)

The problem is there's no global Eloquent class in Laravel. 问题是,有没有全球Eloquent在Laravel类。 There is, however, an Eloquent alias. 但是,有一个Eloquent别名。 If you take a look at the app.php config file, you'll see an array of aliases. 如果查看app.php配置文件,您会看到一个别名数组。

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

Laravel sets up these aliases using the built in PHP function class_alias Laravel使用内置的PHP函数class_alias设置这些别名

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

This is what lets you use Eloquent as though it was a class. 这使您可以像Eloquent一样使用Eloquent (This is actually done though the means of the class alias autoloader ). (实际上是通过类别名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) (但是,如果您在测试中使用Laravel模型和其他框架功能,那么您将真的想弄清楚为什么您的测试环境无法引导Laravel环境)

Try adding parent::setUp() in your test's setUp function as well to make sure your testing environment gets setup properly. 尝试在测试的setUp函数中添加parent :: setUp(),以确保正确设置测试环境。

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

    $this->prepareForTests();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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