我已经编写了用于读写cookie的实用程序类。我不知道该为我的实用程序类编写测试用例。
如何使用Zend框架2 Http/Client编写测试用例
这对测试这个实用程序类是强制性的吗?(因为它使用默认的zend框架方法)
class Utility
{
public function read($request, $key){//code}
public function write($reponse, $name, $value)
{
$path = '/';
$expires = 100;
$cookie = new SetCookie($name,$value, $expires, $path);
$response->getHeaders()->addHeader($cookie);
}
}
--提前感谢
是的:如果你依赖这段逻辑,我会测试这段代码。重要的是要知道,当您调用此方法时,cookie始终设置有给定的值。
查看如何测试这篇文章的一种方法是使用SlmLocale中的一个示例:ZF2区域设置检测模块,它可能会将区域设置写入cookie中。你可以在测试中找到代码。
在您的情况下:
use My'App'Utility;
use Zend'Http'Response;
public function setUp()
{
$this->utility = new Utility;
$this->response = new Response;
}
public function testCookieIsSet()
{
$this->utility->write($this->response, 'foo', 'bar');
$headers = $this->response->getHeaders();
$this->assertTrue($headers->has('Set-Cookie'));
}
public function testCookieHeaderContainsName()
{
$this->utility->write($this->response, 'foo', 'bar');
$headers = $this->response->getHeaders();
$cookie = $headers->get('Set-Cookie');
$this->assertEquals('foo', $cookie->getName());
}
public function testCookieHeaderContainsValue()
{
$this->utility->write($this->response, 'foo', 'bar');
$headers = $this->response->getHeaders();
$cookie = $headers->get('Set-Cookie');
$this->assertEquals('bar', $cookie->getValue());
}
public function testUtilitySetsDefaultPath()
{
$this->utility->write($this->response, 'foo', 'bar');
$headers = $this->response->getHeaders();
$cookie = $headers->get('Set-Cookie');
$this->assertEquals('/', $cookie->getPath());
}
public function testUtilitySetsDefaultExpires()
{
$this->utility->write($this->response, 'foo', 'bar');
$headers = $this->response->getHeaders();
$cookie = $headers->get('Set-Cookie');
$this->assertEquals(100, $cookie->getExpires());
}