我需要在控制台命令上运行几个测试,每个测试在运行测试之前对数据库进行一些小的更改。我的结构如下:
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());
}
}
使用app / phpunit.xml.dist在phpstorm中进行设置:
<?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>
我发现我无法执行测试,因为“测试类未指定或无效”
我尝试使用范围:类,方法,但我不确定还有什么可以遗漏?
答案 0 :(得分:1)
如PHPUnit docs中所述:
如果您将PHPUnit命令行测试运行器指向目录,它将查找* Test.php文件。
这意味着默认情况下,PHPUnit仅考虑以Test.php
结尾的类。
看起来像这样:
<directory suffix=".test">./sourceFolder</directory>
有关详细信息,请参阅this good answer。
答案 1 :(得分:0)
将Parent作为后缀添加到Parent类解决了问题。