我需要关于PHPUnit和一些方法的帮助。你们应该如何在PHPUnit中编写测试来达到以下属性和方法的高代码覆盖率?
我对PHPUnit还比较新,需要一些帮助。我只是为更基本的代码编写了一些测试用例。这个类为最终用户生成闪现消息,并将其存储在会话中。
非常感谢您的帮助。有任何想法吗?
private $sessionKey = 'statusMessage';
private $messageTypes = ['info', 'error', 'success', 'warning']; // Message types.
private $session = null;
private $all = null;
public function __construct() {
if(isset($_SESSION[$this->sessionKey])) {
$this->fetch();
}
}
public function fetch() {
$this->all = $_SESSION[$this->sessionKey];
}
public function add($type = 'debug', $message) {
$statusMessage = ['type' => $type, 'message' => $message];
if (is_null($this->all)) {
$this->all = array();
}
array_push($this->all, $statusMessage);
$_SESSION[$this->sessionKey] = $this->all;
}
public function clear() {
$_SESSION[$this->sessionKey] = null;
$this->all = null;
}
public function html() {
$html = null;
if(is_null($this->all))
return $html;
foreach ($this->all as $message) {
$type = $message['type'];
$message = $message['message'];
$html .= "<div class='message-" . $type . "'>" . $message . "</div>";
}
$this->clear();
return $html;
}
我已经设置了一个测试案例,就像这样:
protected function setUp() {
$this->flash = new ClassName();
}
我也尝试了一个测试用例:
public function testFetch() {
$this->assertEquals($this->flash->fetch(), "statusMessage", "Wrong session key.");
}
但我收到了一个错误消息,提示我:“未定义的变量:_SESSION”
如果我尝试使用以下代码:
public function testFetch() {
$_SESSION = array();
$this->assertEquals($this->flash->fetch(), "statusMessage", "Wrong session key.");
}
我收到了另一个错误消息:“未定义的索引:statusMessage”。
testWithoutSessionKey()
运行良好。 :) 你能否回答一下如何测试这些方法的问题?拜托了! :) 我已经更新了问题。 - Treps