php - Laravel 5.6 单元测试在 null 上调用成员函数 beginTransaction()

标签 php mongodb laravel phpunit laravel-5.6

我有一个运行版本 5.6 的 Laravel 项目。 连接的数据库是 mongodb,带有 jenssegers/mongodb 包。

我编写了一个单元测试来测试与用户相关的功能。

该测试在配置的测试 mongodb 数据库中创建新用户。 我想在每次测试运行后刷新数据库,因此我使用 RefreshDatabase Trait。

使用 RefreshDatabase Trait 时,运行测试时出现以下错误:

There was 1 error:

1) Tests\Unit\UserTest::it_gets_top_user Error: Call to a member function beginTransaction() on null

当不使用 Trait 时,测试会在数据库中创建所有必要的内容并执行断言,不会出现错误。

测试如下所示:

/** @test */
public function it_gets_top_user()
{
    factory(\App\Users\User::class, 5)->create();

    $userOne = factory(\App\Users\User::class)->create([
        'growth' => 10
    ]);

    $topUser = Users::getTopUser();

    $collection = new Collection();

    $collection->push($userOne);


    $this->assertEquals($collection, $topUser);
} 

我在我的composer.json中使用以下版本:

"laravel/framework": "5.6.*",
"jenssegers/mongodb": "3.4.*",
"phpunit/phpunit": "~7.0",

服务器上使用以下版本:

  • PHP 7.2
  • MongoDB 3.4
  • PHP MongoDB 扩展 1.4.2

我使用安装在供应商目录中的 phpunit 来调用测试:

vendor/phpunit/phpunit/phpunit

最佳答案

问题似乎是,RefreshDatabase Trait 对于 MongoDB 环境根本不起作用。

我通过在 Laravel 项目的 testing/ 目录中创建自己的 RefreshDatabase Trait 解决了上述问题。

特征看起来像这样:

<?php

namespace Tests;

trait RefreshDatabase
{
    /**
     * Define hooks to migrate the database before and after each test.
     *
     * @return void
     */
    public function refreshDatabase()
    {
        $this->dropAllCollections();
    }

    /**
     * Drop all collections of the testing database.
     *
     * @return void
     */
    public function dropAllCollections()
    {
        $database = $this->app->make('db');

        $this->beforeApplicationDestroyed(function () use ($database) {
            // list all collections here
            $database->dropCollection('users');
        });
    }
}

为了启用此特征,我重写了 TestCase 类中的 setUpTraits 函数。现在看起来像这样:

/**
 * Boot the testing helper traits.
 *
 * @return array
 */
protected function setUpTraits()
{
    $uses = array_flip(class_uses_recursive(static::class));

    if (isset($uses[\Tests\RefreshDatabase::class])) {
        $this->refreshDatabase();
    }

    if (isset($uses[DatabaseMigrations::class])) {
        $this->runDatabaseMigrations();
    }

    if (isset($uses[DatabaseTransactions::class])) {
        $this->beginDatabaseTransaction();
    }

    if (isset($uses[WithoutMiddleware::class])) {
        $this->disableMiddlewareForAllTests();
    }

    if (isset($uses[WithoutEvents::class])) {
        $this->disableEventsForAllTests();
    }

    if (isset($uses[WithFaker::class])) {
        $this->setUpFaker();
    }

    return $uses;
}

最后,在我的所有测试类中,我可以像这样使用新创建的 Trait:

<?php

namespace Tests\Unit;

use Illuminate\Database\Eloquent\Collection;
use Tests\RefreshDatabase;
use Tests\TestCase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    // tests happen here
}

关于php - Laravel 5.6 单元测试在 null 上调用成员函数 beginTransaction(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49320912/

相关文章:

mysql - 需要自定义id

php - 如何将 HTML 模板转换为 Cake PHP 模板

javascript - 从 MongoDB 驱动程序调用 getUsers() 函数

MongoDB 服务器无法启动

mongodb puppet 模块安装失败

mongodb - 无法连接 MongoDB Compass

php - 连接表 MySql 的完整性约束违规

javascript - 我无法检查多个单选按钮

php - 使用 readfile 允许用户从我的服务器下载图像而看不到图像的 URL 是否会给服务器带来压力?

显示表中最后一行值的 PHP 数组