我是 PHPUnit 的新手,想知道是否可以编写一个忽略特定方法的测试。
该代码就像检查 $data 是否有效,如果发现不规则数据,则向 slack 发送消息。
我的问题是,是否可以在不发送警报消息的情况下运行测试,例如忽略 sendAlert 函数?
如果可能,我想知道如何编写它,如果没有,我想知道为什么以及如何使这段代码可测试。
谢谢!!
示例代码)
public static function isValid($data) {
// some code here
if (Valid) {
return true;
} else {
// some code here to find irregular
if (irregular) {
self::sendAlert($data);
}
return false;
}
}
private static function sendAlert($data) {
// send alert to slack
Example_Model_Slack::post($slackMsg, $channel);
}
<?
class Example_Model_Slack
{
public static function post($text, $channel = '') {
// make $params from $text and $channel
// POST
$stream = [
'http' => [
'method' => 'POST',
'protocol_version' => 1.1,
'content' => http_build_query($params),
],
];
return file_get_contents(self::POST_URL, false, stream_context_create($stream));
}
}