From 09d6af69981fb472e3c9891fc2b3ee5a1ef52109 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Thu, 11 May 2023 14:34:22 +0200 Subject: [PATCH] feat(changelog): add changelog, remove example, add a test for using multiple traits --- CHANGELOG.md | 60 +++++++++++ example/HttpClientOverrideTest.php | 33 ------ example/HttpbinTest.php | 118 ---------------------- example/smoke.yml | 49 --------- src/HttpClient/HttpClientApiCaseTrait.php | 14 +-- tests/WebAndApiTests.php | 54 ++++++++++ 6 files changed, 121 insertions(+), 207 deletions(-) create mode 100644 CHANGELOG.md delete mode 100644 example/HttpClientOverrideTest.php delete mode 100644 example/HttpbinTest.php delete mode 100644 example/smoke.yml create mode 100644 tests/WebAndApiTests.php diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..d1de026 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,60 @@ +## Changes + +## 0.12.0 - 11/05/2023 + + * **[BC BREAK]** No more yield, use php fiber instead + * **[BC BREAK]** Make http test case as an option + * **[BC BREAK]** No more global test case case + * **[BC BREAK]** Use PHP attribute instead of annotation + * Add a new test case trait for API + +### Migrating from 0.11 + +The API for asynit has changed, you need to update your test cases. + +#### Before + +```php +get('https://httpbin.org'); + $response = yield $this->get('http://httpbin.org', ['Authorization' => 'Bearer {token}']); + $this->assertStatusCode(200, $response); + } + + public function getToken() + { + return 'my_token'; + } +} +``` + +#### After + +```php + +get('http://httpbin.org', ['Authorization' => 'Bearer {token}']); + $this->assertStatusCode(200, $response); + } + + public function getToken() + { + return 'my_token'; + } +} +``` \ No newline at end of file diff --git a/example/HttpClientOverrideTest.php b/example/HttpClientOverrideTest.php deleted file mode 100644 index fb73e49..0000000 --- a/example/HttpClientOverrideTest.php +++ /dev/null @@ -1,33 +0,0 @@ -createUri('http://httpbin.org'); - - return new \Http\Client\Common\PluginClient($asyncClient, [ - new \Http\Client\Common\Plugin\BaseUriPlugin($uri), - ]); - } - - public function testFoo2() - { - yield $this->get('/delay/3'); - } - - public function testFoo3() - { - yield $this->get('/delay/3'); - } - - public function testFoo4() - { - yield $this->get('/delay/3'); - } - - public function testFoo5() - { - yield $this->get('/delay/3'); - } -} diff --git a/example/HttpbinTest.php b/example/HttpbinTest.php deleted file mode 100644 index 16010bd..0000000 --- a/example/HttpbinTest.php +++ /dev/null @@ -1,118 +0,0 @@ -get('http://httpbin.org'); - - return $response->getHeaderLine('Server'); - } - - public function testFoo() - { - $response = yield $this->get('http://httpbin.org/delay/3'); - - $this->assertStatusCode(200, $response); - } - - public function testFoo2() - { - yield $this->get('http://httpbin.org/delay/3'); - } - - public function testFoo3() - { - yield $this->get('http://httpbin.org/delay/2'); - } - -// public function testFoo4() -// { -// yield $this->get('http://httpbin.org/delay/7'); -// } - - public function testFoo5() - { - yield $this->get('http://httpbin.org/delay/1'); - } - - public function testFoo6() - { - yield $this->get('http://httpbin.org/delay/1'); - } - - public function testFoo7() - { - yield $this->get('http://httpbin.org/delay/1'); - } - - public function testFoo8() - { - yield $this->get('http://httpbin.org/delay/1'); - } - - public function testFoo9() - { - $promises = []; - $promises[] = $this->get('http://httpbin.org/delay/1'); - $promises[] = $this->get('http://httpbin.org/delay/1'); - $promises[] = $this->get('http://httpbin.org/delay/1'); - $promises[] = $this->get('http://httpbin.org/delay/1'); - $promises[] = $this->get('http://httpbin.org/delay/1'); - - yield $promises; - } - - /** - * @\Asynit\Annotation\Depend("testGet") - */ - public function testDummy($token) - { - yield $this->get('http://httpbin.org'); - - $this->assertEquals('foo', $token); - - yield $this->get('http://httpbin.org'); - - $this->assertEquals('meinheld/0.6.1', $token); - } - - /** - * @\Asynit\Annotation\Depend("testDummy") - */ - public function testIgnored() - { - throw new \Exception(); - } - - /** - * @\Asynit\Annotation\Depend("testGet") - * @\Asynit\Annotation\Depend("testFoo") - */ - public function testDummy1($token) - { - $this->assertEquals('meinheld/0.6.1', $token); - - $this->get('http://httpbin.org'); - } - - /** - * @\Asynit\Annotation\Depend("testGet") - * @\Asynit\Annotation\Depend("testReturn") - */ - public function testDummy2($token, $return) - { - $this->assertEquals('tata', $return); - $this->assertEquals('meinheld/0.6.1', $token); - } -} diff --git a/example/smoke.yml b/example/smoke.yml deleted file mode 100644 index 3d6a7d6..0000000 --- a/example/smoke.yml +++ /dev/null @@ -1,49 +0,0 @@ -"https://jolicode.com/": - status: 200 - -"https://jolicode.com/equipe": - status: 200 - discovery: - enabled: true - match: 'https://jolicode.com/equipe/(.*)' - depth: 3 - limit: 1000 - -"https://jolicode.com/nos-valeurs": - status: 200 - -"https://jolicode.com/nos-metiers": - status: 200 - -"https://jolicode.com/formation": - status: 200 - -"https://jolicode.com/nos-clients": - status: 200 - -"https://jolicode.com/jobs": - status: 200 - -"https://jolicode.com/contact": - status: 200 - -"https://jolicode.com/blog/": - status: 200 - -"https://jolicode.com/blog/joliday-2017-avec-des-joysticks-des-bulles-et-des-leds": - status: 200 - -"https://jolicode.com/blog/joliday-2016-avec-du-carton-du-malt-et-un-doigt": - status: 200 - -"https://jolicode.com/blog/le-printemps-des-conferences": - status: 200 - -"https://jolicode.com/blog/resume-de-la-laracon-eu-2016": - status: 200 - -"https://jolicode.com/blog/tag/laracon": - status: 200 - -"https://jolicode.com/blog/archives/2016/9": - status: 200 diff --git a/src/HttpClient/HttpClientApiCaseTrait.php b/src/HttpClient/HttpClientApiCaseTrait.php index 8fb01fd..4b6aba3 100644 --- a/src/HttpClient/HttpClientApiCaseTrait.php +++ b/src/HttpClient/HttpClientApiCaseTrait.php @@ -15,35 +15,35 @@ protected function getApiContentType(): string final protected function get(string $uri, array|null $json = null, array $headers = [], ?string $version = null): ApiResponse { - return new ApiResponse($this->sendRequest($this->createRequest('GET', $uri, $headers, $json, $version))); + return new ApiResponse($this->sendRequest($this->createApiRequest('GET', $uri, $headers, $json, $version))); } final protected function post(string $uri, array|null $json = null, array $headers = [], ?string $version = null): ApiResponse { - return new ApiResponse($this->sendRequest($this->createRequest('POST', $uri, $headers, $json, $version))); + return new ApiResponse($this->sendRequest($this->createApiRequest('POST', $uri, $headers, $json, $version))); } final protected function patch(string $uri, array|null $json = null, array $headers = [], ?string $version = null): ApiResponse { - return new ApiResponse($this->sendRequest($this->createRequest('PATCH', $uri, $headers, $json, $version))); + return new ApiResponse($this->sendRequest($this->createApiRequest('PATCH', $uri, $headers, $json, $version))); } final protected function put(string $uri, array|null $json = null, array $headers = [], ?string $version = null): ApiResponse { - return new ApiResponse($this->sendRequest($this->createRequest('PUT', $uri, $headers, $json, $version))); + return new ApiResponse($this->sendRequest($this->createApiRequest('PUT', $uri, $headers, $json, $version))); } final protected function delete(string $uri, array|null $json = null, array $headers = [], ?string $version = null): ApiResponse { - return new ApiResponse($this->sendRequest($this->createRequest('DELETE', $uri, $headers, $json, $version))); + return new ApiResponse($this->sendRequest($this->createApiRequest('DELETE', $uri, $headers, $json, $version))); } final protected function options(string $uri, array|null $json = null, array $headers = [], ?string $version = null): ApiResponse { - return new ApiResponse($this->sendRequest($this->createRequest('OPTIONS', $uri, $headers, $json, $version))); + return new ApiResponse($this->sendRequest($this->createApiRequest('OPTIONS', $uri, $headers, $json, $version))); } - private function createRequest(string $method, string $uri, array $headers = [], array|null $json = null, ?string $version = null): RequestInterface + private function createApiRequest(string $method, string $uri, array $headers = [], array|null $json = null, ?string $version = null): RequestInterface { $request = $this->httpFactory->createRequest($method, $uri); $request = $request->withHeader('Content-Type', $this->getApiContentType()); diff --git a/tests/WebAndApiTests.php b/tests/WebAndApiTests.php new file mode 100644 index 0000000..9ff0835 --- /dev/null +++ b/tests/WebAndApiTests.php @@ -0,0 +1,54 @@ +get($this->createUri('/get')); + + $this->assertInstanceOf(ResponseInterface::class, $response); + $this->assertStatusCode(200, $response); + + $content = json_decode($response->getBody()->getContents(), true); + $this->assertArrayNotHasKey('Content-Type', $content['headers']); + + } + + public function testJsonApi() + { + $response = $this->getApi($this->createUri('/get')); + + $this->assertInstanceOf(ApiResponse::class, $response); + $this->assertStatusCode(200, $response); + $this->assertSame('application/json', $response['headers']['Content-Type']); + } + + protected function createUri(string $uri): string + { + return 'http://127.0.0.1:8081'.$uri; + } +}