chillerlan / phpunit-http
为您的测试添加 PSR-17 工厂和 PSR-18 客户端
1.0.1
2024-07-25 16:18 UTC
Requires
- php: ^8.1
Requires (Dev)
- chillerlan/php-httpinterface: ^6.0
- guzzlehttp/guzzle: ^7.9
- phpstan/phpstan: ^1.11
- phpstan/phpstan-deprecation-rules: ^1.2
- phpunit/phpunit: ^10.5
- slevomat/coding-standard: ^8.15
- squizlabs/php_codesniffer: ^3.10
README
为您的 PHPUnit 测试添加 PSR-17 工厂和 PSR-18 客户端。
概览
功能
为您的单元测试提供 PSR-18 HTTP 客户端和 PSR-17 工厂。仅此而已。
要求
- PHP 8.1+
- 所需的扩展可能因使用的 HTTP 客户端而异
文档
使用 composer 安装
将此库以及 PHPUnit 和可选的 HTTP 客户端添加到您的 composer.json
文件的 require-dev
部分
{ "require": { "php": "^8.1" }, "require-dev": { "chillerlan/phpunit-http": "^1.0", "guzzlehttp/guzzle": "^7.8", "phpunit/phpunit": "^10.5" } }
用法
在 PHPUnit 的 TestCase::setUp()
中包含 HttpFactoryTrait
并调用 initFactories()
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()); } }
您可以在 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>
盈利!
自定义 HTTP 客户端
您可以实现 HttpClientFactoryInterface
来创建自己的 HTTP 客户端工厂
final class MyHttpClientFactory implements HttpClientFactoryInterface{ public function getClient(string $cacert, ResponseFactoryInterface $responseFactory):ClientInterface{ return new MyHttpClient(['cacert' => $cacert, /* ... */]); } }
免责声明
自行承担风险!