Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
工匠';无需迁移';PHPUnit错误 提要_Phpunit_Php_Unit Testing_Laravel 5_Laravel Artisan - Fatal编程技术网

工匠';无需迁移';PHPUnit错误 提要

工匠';无需迁移';PHPUnit错误 提要,phpunit,php,unit-testing,laravel-5,laravel-artisan,Phpunit,Php,Unit Testing,Laravel 5,Laravel Artisan,我有一个包,其中包含一个数据库/migrations目录,其中包含许多迁移。我有一个tests目录,其中包含一个测试和一个断言 我希望使用对迁移进行单元测试 发生的问题 我运行PHP单元,但测试失败: $ phpunit PHPUnit 4.6.6 by Sebastian Bergmann and contributors. Configuration read from /var/www/.../phpunit.xml F Time: 594 ms, Memory: 15.25Mb

我有一个包,其中包含一个
数据库/migrations
目录,其中包含许多迁移。我有一个
tests
目录,其中包含一个测试和一个断言

我希望使用对迁移进行单元测试

发生的问题 我运行PHP单元,但测试失败:

$ phpunit
PHPUnit 4.6.6 by Sebastian Bergmann and contributors.

Configuration read from /var/www/.../phpunit.xml

F

Time: 594 ms, Memory: 15.25Mb

There was 1 failure:

1) MigrationTest::testTableExists
Failed asserting that false is true.

/var/www/../tests/MigrationTest.php:31

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
此命令不会在测试数据库中创建任何表,而是在测试数据库中创建迁移表

然后,我尝试在命令行上运行迁移:

$ php artisan migrate --env=testing --path=/vendor/../database/migrations/
Migration table created successfully.
Migrated: 2015_.._table
Migrated: 2015_.._table
Migrated: 2015_.._table
--env=testing
参数已被完全忽略,我的表已迁移到生产数据库(注意:生产数据库指的是开发,但默认/生产设置使用不正确)

我将开发数据库凭据添加到
phpunit.xml

<php>
    <env name="APP_ENV" value="testing"/>
    <env name="CACHE_DRIVER" value="array"/>
    <env name="SESSION_DRIVER" value="array"/>
    <env name="QUEUE_DRIVER" value="sync"/>
    <env name="DB_DATABASE" value="test"/>
    <env name="DB_USERNAME" value="test"/>
    <env name="DB_PASSWORD" value="test"/>
</php>

我的测试用例是:

<?php

class MigrationTest extends \Illuminate\Foundation\Testing\TestCase
{
    public function createApplication()
    {
        $app = require __DIR__. '/../../../../bootstrap/app.php';
        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }

    public function setUp()
    {
        parent::setUp();

        $this->artisan('migrate', ['--env' => 'testing', '--path' => __DIR__.'/../database/migrations']);
    }

    public function tearDown()
    {
        //$this->artisan('migrate:rollback', ['--path' => __DIR__.'/../']);

        parent::tearDown();
    }

    public function testTableExists()
    {
        $hasTable = Schema::hasTable('table_name');

        $this->assertTrue($hasTable);
    }
}

其中一个问题是使用了
--path
,因为迁移只能向上迁移,不能向下迁移。因此,最佳做法是使用以下任一方法:

  • Artisan
    供应商:发布
    并复制到
    /tests/
    ,或
  • /vendor///tests/
    目录添加到PHPUnit
我选择后者是因为这样可以保持应用程序更干净,意味着只有包路径需要与对
composer.json
的任何更改一起维护

因此,本质上是在phpunit.xml中添加多个
定义的情况

<testsuites>
    <testsuite name="Application Test Suite">
        <directory>./tests/</directory>
        <directory>./vendor/VENDOR/PACKAGE/tests/</directory>
    </testsuite>
</testsuites>

/测试/
/供应商/供应商/包装/测试/

我遇到了同样的问题,并得出结论,runDatabaseMigration()是在setUp()方法完成后启动的。除了将其提取到自定义设置方法并在每次测试中调用之外,我还无法找到一个优雅的解决方案。