draw / http-tester
0.4.0
2018-12-09 00:55 UTC
Requires
- guzzlehttp/psr7: ^1.4
- pdeans/http: ^1.1
- phpunit/phpunit: ^7.0|^6.0|^5.7
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-06 02:42:21 UTC
README
这个库旨在成为一个http调用的测试框架。它是框架无关的。默认情况下,它会对指定的url进行curl调用,但你也可以使用/创建一个适配器来使用你正在使用的框架。
该库可以通过 Composer/Packagist 安装。
在示例中,我们尝试实现一个浏览器流程,因此使用phpunit注解 @depends 和 @test 使得代码更易读。
以下是如何在 PHPUnit TestCase 中使用它的快速示例
<?php namespace Your\Project\Name; use PHPUnit\Framework\TestCase; use Draw\HttpTester\HttpTesterTrait; class SimpleTest extends TestCase { use HttpTesterTrait /** * @test */ public function notAuthorizeProfileAccess() { static::$client->get('http://your.domain.com/api/me') ->assertStatus(403); } /** * @test * @depends notAuthorizeProfileAccess */ public function login() { $testResponse = static::$client->post( 'http://your.domain.com/api/tokens', json_encode([ 'username' => 'my-username', 'password' => 'my-password' ]) ); $content = $testResponse ->assertSuccessful() ->assertCookie('session') // We are not debating the usage of cookie here ;) ->getResponseBodyContents(); // Continue with the test of you content $this->assertJson($content); } /** * @test * @depends login */ public function getMyProfile() { // The same client is during all test. Cookies are sent automatically between request $testResponse = static::$client->get('http://your.domain.com/api/me') $content = $testResponse ->assertSuccessful() ->getResponseBodyContents(); // Continue with the test of you content $this->assertJson($content); } }
如果你需要在其他上下文中使用它,并且仍然可以依赖PHPUnit断言,你可以简单地手动创建客户端并使用它
<?php use Draw\HttpTester\Client; $client = new Client(); $client->post( 'http://your.domain.com/api/tokens', json_encode([ 'username' => 'my-username', 'password' => 'my-password' ]) );
默认情况下,客户端将使用 DrawHttpTesterCurlRequestExecutioner,但你也可以通过实现 DrawHttpTesterRequestExecutionerInterface 来创建自己的。
## 当前支持的请求执行器
Curl DrawHttpTesterCurlRequestExecutioner draw/http-tester Laravel 4.2 DrawHttpTesterBridgeLaravel4Laravel4RequestExecutioner draw/http-tester
** 目前不可用 ** 还有更多功能可用,请 阅读文档!