dongqu2863 2014-09-23 12:30
浏览 48
已采纳

如何编写Zend framework 2 cookie的测试用例?

I have written the utility class which going to read and write cookies. I don't have an idea to write the test cases for my utility class.

How can i write test cases by using Zend framework 2 Http/Client?
Is this mandatory to test this utility class? (since it uses default zend framework methods)

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

--Thanks in advance

  • 写回答

1条回答 默认 最新

  • duanfei1975 2014-09-24 11:14
    关注

    Yes: if you rely on this piece of logic I would test this code. It is important to know that the cookie is always set with the given values when you call this method.

    A way to look how you can test the piece is with an example from SlmLocale: a ZF2 locale detection module that writes possibly the locale down into a cookie. You can find the code in the tests.

    In your case:

    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());
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序