我有一个由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.
感谢您的帮助。
答案 0 :(得分:0)
尝试一下:
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');
}