jeudi 26 février 2015

How to test specific methods with PHPUnit

I need help with PHPUnit and some methods. How should you guys write tests in PHPUnit to reach a high code coverage for the following properties and methods?


I'm pretty new to PHPUnit and could need some help. I've just write some test cases for more basic code. This class generates flash messages for the end user, and store it in a session.


Extremely grateful for some help.



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;
}

Aucun commentaire:

Enregistrer un commentaire