l2iterative/mock-webserver-ext

对donatj/mock-webserver的简单扩展,增加了额外的匹配功能

1.1.0 2024-01-07 06:47 UTC

This package is not auto-updated.

Last update: 2024-09-30 08:38:51 UTC


README

a group of actors sitting in a parade

这个简洁的库旨在通过将InterNations/http-mock的接口适配到donatj/mock-webserver中,使donatj/mock-webserver能够处理更复杂的请求定义。

代码的大部分似乎是InterNations/http-mockMatcherExpectation实现的局部重写。因此,代码的荣誉应归于InterNations/http-mock

为什么要扩展donatj/mock-webserver

我们本可以选择不使用donatj/mock-webserver,而是使用InterNations/http-mock。我们没有选择这条路,因为InterNations/http-mock已经有些过时,而donatj/mock-webserver仍在积极维护。

我们更喜欢donatj/mock-webserver的另一个原因是它更简单的依赖关系,因为InterNations/http-mock似乎使用了Symfony框架,这可能是一个庞大的架构。

如何使用

测试脚本提供了一个说明性的示例。

<?php
$complex_response = new ComplexResponse();
$complex_response
    ->when_path_is('/api/users')
    ->when_method_is('POST')
    ->when_header_is('x-api-key', new RegexMatcher('/^mock/'))
    ->when_header_exists('x-api-version')
    ->when_header_not_exists('x-api-version-old')
    ->when_query_param_is('GET', 'session_uid', new RegexMatcher('/^s/'))
    ->when_query_param_exists('POST', 'image')
    ->when_query_param_not_exists('POST', 'debug')
    ->then(new Response('{"id": 1, "name": "John Doe"}', [], 200));
?>

由于ComplexResponse实现了ResponseInterface,它可以作为一个即插即用的替代品。

许可证

此软件包位于MIT许可证下。有关更多详细信息,请参阅LICENSE