PHPUnit:使用@dataProvider时"未找到"类'Eloquent'

Jud*_*man 6 php phpunit laravel composer-php

@dataProvider在使用Laravel应用程序中使用PHPUnit编写单元测试时,我遇到了一个问题.我收到的错误是:

PHP致命错误:第7行的/path/to/project/app/models/ExampleClass.php中找不到类'Eloquent'

它看起来像是使用的常数dataProvider导致致命.

composer.json:

"psr-4": {
    "Acme\\Models\\": "app/models"
}
Run Code Online (Sandbox Code Playgroud)

phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false"
         syntaxCheck="false"
>
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./app/tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

示例模型:

<?php
namespace Acme\Models;

use Eloquent;

class ExampleClass extends Eloquent
{
    /**
     * @var bool
     */
    const TRUE = true;
}
Run Code Online (Sandbox Code Playgroud)

示例测试类:

<?php

use Acme\Models\ExampleClass;

class ExampleClassTest extends TestCase
{
    /**
     * Example test.
     *
     * @param int $value
     * @return void
     * @dataProvider testExampleTestDataProvider
     */
    public function testExampleTest($value)
    {
        $this->assertTrue($value);
    }

    /**
     * Data provider for testExampleTest.
     *
     * @return array
     */
    public function testExampleTestDataProvider()
    {
        return array(
            array(ExampleClass::TRUE),
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

PHP Stack trace:
PHP   1. {main}() /usr/local/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:612
PHP   3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:138
PHP   4. PHPUnit_TextUI_Command->handleArguments() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:148
PHP   5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:696
PHP   6. PHPUnit_Util_Configuration->getTestSuite() phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php:837
PHP   7. PHPUnit_Framework_TestSuite->addTestFiles() phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php:924
PHP   8. PHPUnit_Framework_TestSuite->addTestFile() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:400
PHP   9. PHPUnit_Framework_TestSuite->addTestSuite() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:374
PHP  10. PHPUnit_Framework_TestSuite->__construct() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:289
PHP  11. PHPUnit_Framework_TestSuite->addTestMethod() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:188
PHP  12. PHPUnit_Framework_TestSuite::createTest() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:842
PHP  13. PHPUnit_Util_Test::getProvidedData() /path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:465
PHP  14. ReflectionMethod->invoke() /path/to/project/vendor/phpunit/phpunit/src/Util/Test.php:392
PHP  15. ExampleClassTest->testExampleTestDataProvider() /path/to/project/vendor/phpunit/phpunit/src/Util/Test.php:392
PHP  16. spl_autoload_call() /path/to/project/vendor/phpunit/phpunit/src/Util/Test.php:27
PHP  17. Composer\Autoload\ClassLoader->loadClass() /path/to/project/vendor/phpunit/phpunit/src/Util/Test.php:0
PHP  18. Composer\Autoload\includeFile() /path/to/project/vendor/composer/ClassLoader.php:278
PHP  19. include() /path/to/project/vendor/composer/ClassLoader.php:386
Run Code Online (Sandbox Code Playgroud)

Tho*_*ley 7

我有这个问题.它与该Laravel别名(如事实做Config::,Log::...)尚未在该时加载的@dataProvider方法被调用.这里有两个我能想到的解决方案.

解决方案1

修改您的@dataProvider不使用模型类.就我而言,我在@dataProvider方法中创建了模型对象,如下所示:

public function people() {
    $person1 = new Person();
    $person1->name = "me";

    $person2 = new Person();
    $person2->name = "you";

    return [$person1, $person2];
}
Run Code Online (Sandbox Code Playgroud)

由于该方法中Person引用了类@dataProvider,因此它将尝试加载该类.然后它会失败,因为EloquentLaravel尚未创建类别名.

为了解决这个问题,我可以返回数据,并在测试中创建实际的模型对象:

public function people() {
    return ["me", "you"];
}

public function testPerson($name) {
    $person = new Person();
    $person->name = $name;

    // Assertions...
}
Run Code Online (Sandbox Code Playgroud)

在你的情况下,这意味着返回[['true']],而不是[[ExampleClass::TRUE]].

解决方案2

我认为没有令人信服的理由在Eloquent这里使用类别名.事实上,我不知道为什么它存在(除了它"看起来"更好?).我把它带到了IRC频道,没有得到回复......所以如果有理由在这里使用别名,那我就不知道了.

也就是说,如果您的模型类扩展了底层\Illuminate\Database\Eloquent\Model类而不是Eloquent别名,那么您的测试将按原样开始工作.

<?php
namespace Acme\Models;

use \Illuminate\Database\Eloquent\Model;

class ExampleClass extends Model
{
    /**
     * @var bool
     */
    const TRUE = true;
}
Run Code Online (Sandbox Code Playgroud)