【问题标题】:How to test the client in the absence of API?如何在没有 API 的情况下测试客户端?
【发布时间】:2019-12-17 14:15:39
【问题描述】:

我决定编写一个向 API 发送 http 请求的客户端。请求有 3 种类型:GET、POST、PUT。我们需要使用 phpunit 编写单元测试,这将允许我们在不编写 API 的情况下测试功能。我的第一个想法是使用模拟对象。阅读了足够多的文献后,我无论如何都无法理解如何做到这一点。据我了解,无论我的请求去哪里,我都需要为 API 创建一个存根。请告诉我要朝哪个方向移动以解决问题。

<?php

namespace Client;

class CurlClient implements iClient
{
    private $domain;

    public function __construct($domain = "http://example.com")
    {
        $this->domain = $domain;
    }

    public function getAllComments()
    {
        $ch = curl_init($this->domain.'/comments');

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $comments = curl_exec($ch);

        $comments = json_decode($comments);

        curl_close($ch);

        return $comments;
    }

    public function addNewComment($data)
    {
        $ch = curl_init($this->domain.'/comment');

        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_exec($ch);

        $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

        $statusCode = (string)$statusCode;
        $statusCode = (int)$statusCode[0];

        curl_close($ch);

        return $statusCode == 2 ? true : false;
    }

    public function updateComment($id, $data)
    {
        $ch = curl_init($this->domain.'/comment/'.$id);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_exec($ch);

        $statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);

        $statusCode = (string)$statusCode;
        $statusCode = (int)$statusCode[0];

        curl_close($ch);

        return $statusCode == 2 ? true : false;
    }
}

【问题讨论】:

    标签: php http mocking phpunit


    【解决方案1】:

    这是一个使用phpunit 的简单模拟示例。 phpunit 的模拟功能非常广泛,更多信息phpunit documentation - test doubles

    <?php
    use PHPUnit\Framework\TestCase;
    
      // Use the getMockBuilder() method that is provided by the   
      // PHPUnit\Framework\TestCase class to set up a mock object
      //  for the CurlClient object.
    
    class CurlClientTest extends TestCase
    {
        public function testAddNewComment()
        {
            // Create a mock for the CurlClient class,
            // only mock the update() method.
            $client = $this->getMockBuilder(CurlClient::class)
                             ->setMethods(['addNewComment'])
                             ->getMock();
            $map = [
              ['hello', true],
              [123, false]
            ];
    
            $client->method('addNewComment')->will($this->returnValueMap($map));
    
            $this->assertEquals(true, $client->addNewComment('hello'));
            $this->assertEquals(false, $client->addNewComment(123));
        }
    }
    

    【讨论】:

    • 这个测试实际测试的是什么?没有正在运行的用户代码。在我看来,它测试了 phpunit 是否可以正确地进行模拟,但这并没有真正的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多