Я использую фреймворк Laravel с пакетом Модуляризация .
Я создал модуль и провел на нем несколько тестов. Однако я не могу запустить свои тесты с покрытием (./vendor/bin/phpunit --coverage-text --colors=never
). Без флага покрытия тесты выполняются корректно (./vendor/bin/phpunit
).
вот две выполненные команды:
Среди всех моих тестов есть один тест, который я называю фабричным
class InstitutionControllerTest extends TestCase
{
/**
* @testdox Get the data of institutions
* @covers \Modules\Institution\Http\Controllers\InstitutionController::index
* */
public function testIndex():void
{
$institution = factory(Institution::class)->make();
$institutionCollection = new Collection($institution);
$mock = Mockery::mock(InstitutionRepository::class);
$mock->shouldReceive('all')->andReturn($institutionCollection);
$this->app->instance(InstitutionRepository::class, $mock);
$response = $this->json('GET', 'api/institutions');
$this->assertEquals(Response::HTTP_OK, $response->status());
$this->assertEquals($institutionCollection, $response->getContent());
}
}
Я добавил фабрику к провайдеру, как сказано в документации , но она не исправляет ошибку.
class InstitutionServiceProvider extends ServiceProvider
{
public function boot()
{
...
$this->registerFactories();
...
}
public function register()
{
$this->app->register(RouteServiceProvider::class);
$this->app->singleton(Factory::class, function () {
$faker = $this->app->make(\Faker\Generator::class);
return Factory::construct($faker, __DIR__ . '/../Database/factories');
});
}
...
public function registerFactories()
{
if (! app()->environment('production') && $this->app->runningInConsole()) {
app(Factory::class)->load(__DIR__ . '/../Database/factories');
}
}
....
}
Знаете ли вы, как я могу решить эту ошибку?
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit/</directory>
<directory suffix="Test.php">./Modules/**/Tests/Unit/</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature/</directory>
<directory suffix="Test.php">./Modules/**/Tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
<directory suffix=".php">./Modules</directory>
<exclude>
<directory suffix="blade.php">./Modules</directory>
<directory suffix=".php">./Modules/**/Routes</directory>
<directory suffix=".php">./Modules/**/Resources</directory>
<directory suffix=".php">./Modules/**/Tests</directory>
<directory suffix=".php">./Modules/**/Config</directory>
</exclude>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
</php>
</phpunit>
Мой заводской класс
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Modules\Institution\Entities\Institution;
// the variable $factory is Undefined
$factory->define(Institution::class, function (Faker $faker) {
return [
'name' => $faker->company
];
});