0

我有一个用 slim、Eloquent 和 Phinx 制作的项目,我正在集成 PHPunit。

除了我需要在执行测试之前执行信息播种器的新测试之外,一切正常。

种子

<?php


use Phinx\Seed\AbstractSeed;

class Permissions extends AbstractSeed
{
    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    public function run()
    {

        $data = [
            [
                'id'    => '1',
                'level' => 'admin',
                'created_at' => date('Y-m-d H:i:s'),
            ],
        ];

        $level = $this->table('users_levels');
        $level->insert($data)
            ->save();

        $data = [
            [
                'users_level_id'    => '1',
                'method' => 'GET',
                'url' => '/api/events/{date:\d{4}-\d{1,2}-\d{1,2}}',
                'created_at' => date('Y-m-d H:i:s'),
            ],
            [
                'users_level_id'    => '1',
                'method' => 'POST',
                'url' => '/api/event',
                'created_at' => date('Y-m-d H:i:s'),
            ],
        ];

        $urls = $this->table('level_urls');
        $urls->insert($data)
            ->save();


    }
}

我从控制台运行它完美无缺,

C:\xampp\htdocs\CirceApi> .\vendor\bin\phinx seed:run
Phinx by CakePHP - https://phinx.org. 0.8.1

using config file .\phinx.php
using config parser php
using migration paths
 - C:\xampp\htdocs\CirceApi\database\migrations
using seed paths
 - C:\xampp\htdocs\CirceApi\database\seeds
warning no environment specified, defaulting to: development
using database circe

 == Permissions: seeding
 == Permissions: seeded 0.1773s

但是当我以以下方式启动它进行测试时

use Phinx\Console\PhinxApplication;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\NullOutput;    
protected function runMigration()
    {
        $app = new PhinxApplication();
        $app->setAutoExit(false);
        $app->doRun(new StringInput("migrate"), new NullOutput());
        $app->doRun(new StringInput("seed:run"), new NullOutput());

    }

它返回以下错误。

14) Tests\Profile\ProfileTest::it_returns_404_status_code_when_profile_is_not_found
PDOException: SQLSTATE[HY000]: General error: 1 table users_levels has no column named level

C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\PdoAdapter.php:215
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\AdapterWrapper.php:191
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Adapter\TimedOutputAdapter.php:125
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Table.php:667
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Table.php:610
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Db\Table.php:697
C:\xampp\htdocs\CirceApi\database\seeds\Permissions.php:29
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Migration\Manager\Environment.php:156
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php:403
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Migration\Manager.php:536
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Console\Command\SeedRun.php:110
C:\xampp\htdocs\CirceApi\vendor\symfony\console\Command\Command.php:255
C:\xampp\htdocs\CirceApi\vendor\symfony\console\Application.php:901
C:\xampp\htdocs\CirceApi\vendor\symfony\console\Application.php:262
C:\xampp\htdocs\CirceApi\vendor\robmorgan\phinx\src\Phinx\Console\PhinxApplication.php:83
C:\xampp\htdocs\CirceApi\tests\UseDatabaseTrait.php:19
C:\xampp\htdocs\CirceApi\tests\BaseTestCase.php:44

ERRORS!
Tests: 17, Assertions: 7, Errors: 14.

谢谢您的帮助。

4

1 回答 1

1

尝试这个:

use Phinx\Console\Command\SeedRun;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;


$phinxApplication = new Application();
$phinxApplication->add(new SeedRun());

$phinxSeedRunCommand = $phinxApplication->find('seed:run');
$phinxCommandTester = new CommandTester($phinxSeedRunCommand);
$phinxCommandTester->execute(['command' => $phinxSeedRunCommand->getName()]);

$phinxDisplay = $phinxCommandTester->getDisplay();
$phinxStatusCode = $phinxCommandTester->getStatusCode();
if ($phinxStatusCode > 0) {
    throw new RuntimeException('Seed:run failed');
}
于 2018-12-06T22:57:42.370 回答