ns3777k / hoverfly
v2.0.0
2023-02-19 16:32 UTC
Requires
- php: >=8.0
- ext-json: *
- guzzlehttp/guzzle: ^7.0
- netresearch/jsonmapper: ^4.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.14
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-19 20:13:40 UTC
README
为什么我会使用它呢?
考虑有一个功能测试,它向应用程序发送请求。在处理请求时,应用程序可以使用多个外部服务,如天气预报、计费或预订系统。我们不希望测试外部服务,因为它们可能不稳定,需要互联网连接,可能会限制每秒的请求率并增加延迟。在测试过程中,我们只需要 某种东西 来根据规范响应我们的请求,它不需要是一个真实的服务,这正是hoverfly和这个客户端发挥作用的地方。
安装
$ composer require --dev ns3777k/hoverfly
示例
您的测试必须配置为使用hoverfly代理服务器(使用HTTP_PROXY
)并忽略自身的代理(使用NO_PROXY
)。
<?php require_once __DIR__ . '/vendor/autoload.php'; use Hoverfly\Client; use Hoverfly\Model\Response; class SomeTest { private $hoverfly; public function __construct() { $this->hoverfly = new Client(['base_uri' => getenv('HOVERFLY_URL')]); } public function _before() { $this->hoverfly->deleteJournal(); $this->hoverfly->deleteSimulation(); } public function testFeature(ApiTester $I) { $this->hoverfly->simulate( $this->hoverfly->buildSimulation() ->serviceExact('test.ru') ->getExact('/test') ->withState('customer', 'individual') ->willReturn( Response::json(['test' => true]) ->setDelay(3000) ->addTransitionsState('step', 'order') ->addTransitionsState('customer', 'individual') ->addRemovesState('basket') ) ); $I->sendPOST('/api/v1/faq/9999999/dislike', ['comment' => 'test']); } }