chillerlan/phpunit-http
Composer 安装命令:
composer require chillerlan/phpunit-http
包简介
Add PSR-17 factories and a PSR-18 client to your tests
README 文档
README
Add PSR-17 factories and a PSR-18 client for use in your PHPUnit tests.
Overview
Features
A PSR-18 HTTP client and PSR-17 factories for your unit tets. That's it.
Requirements
- PHP 8.4+
- required extensions may vary depending on the used HTTP client
Documentation
Installation with composer
Add this library along with PHPUnit and an optional HTTP client to the require-dev section of your composer.json:
{
"require": {
"php": "^8.4"
},
"require-dev": {
"chillerlan/phpunit-http": "^2.0",
"guzzlehttp/guzzle": "^7.10",
"phpunit/phpunit": "^13.0"
}
}
Usage
Include the HttpFactoryTrait and call initFactories() from within PHPUnit's TestCase::setUp():
class MyUnitTest extends \PHPUnit\Framework\TestCase{ // include the factory trait use \chillerlan\PHPUnitHttp\HttpFactoryTrait; // you can define the factories either as properties in your test class or in phpunit.xml protected string $REQUEST_FACTORY = MyRequestFactory::class; protected string $RESPONSE_FACTORY = MyResponseFactory::class; protected string $STREAM_FACTORY = MyStreamFactory::class; protected string $URI_FACTORY = MyUriFactory::class; // these three factories may not always be needed and/or implemented, // you can just unset or simply omit the properties protected string $HTTP_CLIENT_FACTORY = \chillerlan\PHPUnitHttp\GuzzleHttpClientFactory::class; protected string $SERVER_REQUEST_FACTORY; protected string $UPLOADED_FILE_FACTORY; // a CA bundle is required when using a http client protected const CACERT = __DIR__.'/cacert.pem'; // in PHPUnit's setUp, call the factory initializer protected function setUp():void{ try{ $this->initFactories(realpath($this::CACERT)); } catch(\Throwable $e){ $this->markTestSkipped('unable to init http factories: '.$e->getMessage()); } } // use the factories public function testSomething():void{ $uri = $this->uriFactory->createUri('https://example.com'); $request = $this->requestFactory->createRequest('GET', $uri); $response = $this->httpClient->sendRequest($request); // do stuff $this::assertSame(200, $response->getStatusCode()); } }
Instead of setting the properties in the test classes, you can define them as constants in your phpunit.xml:
<?xml version="1.0"?> <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" cacheDirectory=".build/phpunit-cache" > <testsuites> <testsuite name="my test suite"> <directory>tests</directory> </testsuite> </testsuites> <source> <include> <directory>src</directory> </include> </source> <coverage> <report> <clover outputFile=".build/coverage/clover.xml"/> </report> </coverage> <php> <!-- define your factories here --> <const name="REQUEST_FACTORY" value="MyLibrary\MyRequestFactory"/> <const name="RESPONSE_FACTORY" value="MyLibrary\MyResponseFactory"/> <const name="STREAM_FACTORY" value="MyLibrary\MyStreamFactory"/> <const name="URI_FACTORY" value="MyLibrary\MyUriFactory"/> <!-- <const name="SERVER_REQUEST_FACTORY" value=""/> <const name="UPLOADED_FILE_FACTORY" value=""/> --> <const name="HTTP_CLIENT_FACTORY" value="chillerlan\PHPUnitHttp\GuzzleHttpClientFactory"/> </php> </phpunit>
Profit!
Custom HTTP client
You can implement the HttpClientFactoryInterface to create your own HTTP client factory:
final class MyHttpClientFactory implements HttpClientFactoryInterface{ public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{ return new MyHttpClient(['cacert' => $cacert, /* ... */]); } }
Disclaimer
Use at your own risk!
chillerlan/phpunit-http 适用场景与选型建议
chillerlan/phpunit-http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「phpunit」 「factories」 「psr-17」 「psr-18」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chillerlan/phpunit-http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chillerlan/phpunit-http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chillerlan/phpunit-http 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
HTTP client library
A library to generate application code based on database definition.
统计信息
- 总下载量: 1.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-14