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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么