Skip to content

Commit

Permalink
feat(changelog): add changelog, remove example, add a test for using …
Browse files Browse the repository at this point in the history
…multiple traits
  • Loading branch information
joelwurtz committed May 11, 2023
1 parent b0005b2 commit 09d6af6
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 207 deletions.
60 changes: 60 additions & 0 deletions 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
<?php

class HttpbinTest extends \Asynit\TestCase
{
/** @\Asynit\Annotation\Depend('getToken') */
public function testGet($token)
{
$response = yield $this->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

<?php

#[\Asynit\Attribute\TestCase]
class HttpbinTest
{
use \Asynit\HttpClient\HttpClientWebCaseTrait;

#[\Asynit\Attribute\Depend('getToken')]
public function testGet($token)
{
$response = $this->get('http://httpbin.org', ['Authorization' => 'Bearer {token}']);
$this->assertStatusCode(200, $response);
}

public function getToken()
{
return 'my_token';
}
}
```
33 changes: 0 additions & 33 deletions example/HttpClientOverrideTest.php

This file was deleted.

118 changes: 0 additions & 118 deletions example/HttpbinTest.php

This file was deleted.

49 changes: 0 additions & 49 deletions example/smoke.yml

This file was deleted.

14 changes: 7 additions & 7 deletions src/HttpClient/HttpClientApiCaseTrait.php
Expand Up @@ -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());
Expand Down
54 changes: 54 additions & 0 deletions tests/WebAndApiTests.php
@@ -0,0 +1,54 @@
<?php

namespace Asynit\Tests;

use Asynit\Attribute\TestCase;
use Asynit\HttpClient\ApiResponse;
use Asynit\HttpClient\HttpClientApiCaseTrait;
use Asynit\HttpClient\HttpClientWebCaseTrait;
use Psr\Http\Message\ResponseInterface;

#[TestCase]
class WebAndApiTests
{
use HttpClientWebCaseTrait, HttpClientApiCaseTrait {
HttpClientWebCaseTrait::get insteadof HttpClientApiCaseTrait;
HttpClientWebCaseTrait::post insteadof HttpClientApiCaseTrait;
HttpClientWebCaseTrait::patch insteadof HttpClientApiCaseTrait;
HttpClientWebCaseTrait::put insteadof HttpClientApiCaseTrait;
HttpClientWebCaseTrait::options insteadof HttpClientApiCaseTrait;
HttpClientWebCaseTrait::delete insteadof HttpClientApiCaseTrait;
HttpClientApiCaseTrait::get as private getApi;
HttpClientApiCaseTrait::post as private postApi;
HttpClientApiCaseTrait::patch as patchApi;
HttpClientApiCaseTrait::put as putApi;
HttpClientApiCaseTrait::options as optionsApi;
HttpClientApiCaseTrait::delete as deleteApi;
}

public function testJsonWeb()
{
$response = $this->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;
}
}

0 comments on commit 09d6af6

Please sign in to comment.