如何使用PHPUnit测试特定方法

3

我需要关于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”。

  1. 将所有装饰(HTML)从此代码中移出
  2. 将闪存消息存储与其他逻辑分离。
  3. (缺少上下文,无法翻译)
  4. 利润!!!1111
- zerkms
我不能更改这段代码。需要按照现在的状态进行测试。你有任何想法吗?请帮忙。 :) - Treps
尝试这样做:`function testWithoutSessionKey() { $_SESSION = array(); $yourClass = new YourclassName(); $this->assertNull($yourClass->html()); } function testWithSomeSessionKey() { $_SESSION = array( 'statusMessage' => array(...)); $yourClass = new YourclassName(); $this->assertSame($expect, $yourClass->html()); }` 希望这可以帮到你。 - Matteo
@Matteo:testWithoutSessionKey() 运行良好。 :) 你能否回答一下如何测试这些方法的问题?拜托了! :) 我已经更新了问题。 - Treps
你走在正确的道路上!你在代码中发现了一个错误!现在你可以修复它(你必须在fetch方法中检查键是否设置,然后再访问它)。我认为第二种方法是好的。我应该只发布你在答案中放置的代码。 - Matteo
fetch方法不返回任何内容,因此您无法测试返回值。因此,您可以使用预期输出测试html()方法的结果。 - Matteo
1个回答

2
尝试类似这样的内容:
    function testWithoutSessionKey() { 
$_SESSION = array(); 
$yourClass = new YourclassName(); 
$this->assertNull($yourClass->html()); } 

function testWithSomeSessionKey() { 
$_SESSION = array( 'statusMessage' => array(...)); 
$yourClass = new YourclassName(); 
$this->assertSame($expect, $yourClass->html()); 
} 
  1. 你不能在setup中实例化你的类,因为你的构造函数需要SESSION变量可能存在(这样你可以测试其中是否有一些值)。
  2. 你只能评估(断言)一个方法的输出,所以你不能断言方法fetch的返回消息。

在你的方法testFetch中,你发现了一个bug!感谢这个测试。尝试像在构造函数中那样进行检查以修复它:

public function fetch() {
if (isset($_SESSION[$this->sessionKey]))
    $this->all = $_SESSION[$this->sessionKey];
}    

希望这有所帮助。

非常感谢你,伙计! :) - Treps

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接