doupeng6890 2014-02-17 05:13
浏览 28

使用phpspec测试Symfony 2与Guzzle API客户端的捆绑

Good morning!

I have recently created a Symfony 2 bundle that integrates with a third party API.
I have used Guzzle with the awesome service description to create the client and call the API.

Now I want to improve the quality and add some testing to it. After some reading I came up to use phpspec for the functional testing of the client and Behat for the behavior.
I have read that you can mock the API responses but I can’t figure out how. Though phpspec has a mocking library inside (prophecy), I can’t find any examples how to mock the responses in prophecy.

I have my spec created with

bin/phpspec desc NewFusion/Bundle/HyperMediaBundle/Service/VehicleService

And it has created my spec class in the Bundle/spec/ folder.

<?php

namespace spec\NewFusion\Bundle\HyperMediaBundle\Service;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class VehicleReferenceDataServiceSpec extends ObjectBehavior
{
    function it_is_initializable()
    {
        $this->shouldHaveType('NewFusion\Bundle\HyperMediaBundle\Service\VehicleReferenceDataService');
    }

    function it_should_return_make_name() {
        $this->getMake(array('makeCode' => 1))->shouldReturn(TRUE);
    }
}

This is the response I want the mock with this test:

HTTP/1.1 200 OK
Cache-Control: no-cache,no-store,must-revalidate
Connection: Keep-Alive
Content-Type: application/vnd.siren+json; charset=UTF-8
Date: 12 Feb 2014 07:43:00 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT, 0
Keep-Alive: timeout=15, max=100
Pragma: no-cache
Server: Jetty(8.y.z-SNAPSHOT)
Transfer-Encoding: chunked
Via: 1.1 test.local

{
"class": [
"Make"
],
"properties": {
"makeCode": "1",
"name": "Alfa Romeo"
},
"links": [
{
"rel": [
"self"
],
"href": "https://test.local/v1/api/vehiclereferencedata/make/1"
},
{ "rel": [
"models"
],
"href": "https://test.local/make/1/model"
}
]
}

I hope someone will get me on track :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 个人网站被恶意大量访问,怎么办
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大