我需要在控制台命令上运行几个测试,每个测试在运行测试之前都会对数据库进行一些小的更改。我的结构如下:
class ActionRemindCommandEmail extends KernelTestCase
{
private $mailCollector;
/**
* @return mixed
*/
public function getMailCollector()
{
return $this->mailCollector;
}
/**
* @param mixed $mailCollector
*/
public function setMailCollector($mailCollector)
{
$this->mailCollector = $mailCollector;
}
protected function setUp()
{
exec('mysql database < prep.sql');
}
public function testExecute()
{
exec('echo "' . str_replace('"', '\\"', $this->getPrepSql() ) . '" | mysql database');
self::bootKernel();
$application = new Application(self::$kernel);
$application->add(new ActionRemindCommand());
$client = static::createClient();
$client->enableProfiler();
$this->setMailCollector($client->getProfile()->getCollector('swiftmailer'));
$command = $application->find('app:ahp:remind');
$commandTester = new CommandTester($command);
$commandTester->execute(array(
'command' => $command->getName(),
'email' => ''
));
$output = $commandTester->getDisplay();
$this->assertions();
}
}
第一个实际测试看起来像这样
class ActionRemindCommandVetNotifyWhenSavedOnlyTest extends ActionRemindCommandEmailTest
{
protected function getPrepSql()
{
return "INSERT INTO `action` (farmer_id`, `group_id`, `name`, `due`, `active`, `completed`, `notify_vet_email`, `notify_farmer_email`, `notify_vet_text`, `notify_farmer_text`, `notify_tech_text`, `notify_clinic_email`, `notify_farmer_text2`, `notified_vet_email`, `notified_farmer_email`, `notified_clinic_email`, `notified_vet_text`, `notified_farmer_text`, `notified_farmer_text2`, `notified_tech_text`, `notify_vet_email_advance`, `notify_farmer_email_advance`, `notify_clinic_email_advance`, `notify_vet_text_advance`, `notify_farmer_text_advance`, `notify_farmer_text2_advance`, `notify_tech_text_advance`, `no_time`, `notes`, `pending_offline_id`) VALUES "
. " (116, NULL, 'Action 1', '"
. date('Y-m-d H:i:s',mktime(12,0,0,intval(date('n')), intval(date('j'))+14,intval(date('Y'))))
. "', 1, 0, 1, 0, 0, 0, 0, 0, 0, '000', '000', '000', '000', '000', '000', '000', -1, -1, -1, 24, 24, 24, 24, 0, 'undefined', 'N1478139417184')";
}
public function testExecute()
{
parent::testExecute();
}
protected function assertions()
{
$this->assertEquals(1, $this->getMailCollector()->getMessageCount());
}
}
在 phpstorm 中使用 app/phpunit.xml.dist 进行设置:
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="bootstrap.php.cache"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<!--
<php>
<server name="KERNEL_DIR" value="/path/to/your/app/" />
</php>
-->
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
我发现我无法执行测试,因为“测试类未指定或无效”
我尝试使用范围:类,方法,但我不确定还可能缺少什么?
如 PHPUnit 文档中所指定:
如果您将 PHPUnit 命令行测试运行程序指向一个目录,它将查找 *Test.php 文件。
这意味着默认情况下 PHPUnit 只考虑以
Test.php
结尾的类。
看起来像这样:
<directory suffix=".test">./sourceFolder</directory>
请参阅这个好答案了解更多详细信息。
在 Parent 类中添加 Test 作为后缀解决了问题。