Skip to content

Commit

Permalink
Merge pull request #6 from zfegg/develop-zendframework
Browse files Browse the repository at this point in the history
Fix `call("GET", .., $paramters)` &&  Update README.md
  • Loading branch information
Moln authored Jan 13, 2020
2 parents c6eddf2 + 4a464bf commit fb65376
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 11 deletions.
88 changes: 84 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ composer require zfegg/expressive-test --dev
Usage / 使用
--------------

### `runApp(...)`

```php

use Psr\Http\Message\ResponseInterface;
use Zfegg\ExpressiveTest\AbstractActionTestCase;

class HomePageTest extend AbstractActionTestCase {
class HomePageTest extends AbstractActionTestCase {

public function testHome() {
$response = $this->runApp(
Expand All @@ -44,8 +45,7 @@ class HomePageTest extend AbstractActionTestCase {
}

public function testPassMiddlewareOrMockService() {



$this->container->setService('some middleware', new PassMiddleware());

$mock = $this->createMock(SomeService::class);
Expand All @@ -63,4 +63,84 @@ class HomePageTest extend AbstractActionTestCase {
$this->assertInstanceOf(ResponseInterface::class, $response);
}
}
```
```

### Simple test methods like Laravel

```php

use Psr\Http\Message\ResponseInterface;
use Zfegg\ExpressiveTest\AbstractActionTestCase;

class HomePageTest extends AbstractActionTestCase {

public function testHome() {
/*
$this->get($uri, $headers = []);
$this->getJson($uri, $headers = []);
$this->post($uri, $data = [], $headers = []);
$this->postJson($uri, $data = [], $headers = []);
$this->put($uri, $data = [], $headers = []);
$this->putJson($uri, $data = [], $headers = []);
$this->patch($uri, $data = [], $headers = []);
$this->patchJson($uri, $data = [], $headers = []);
$this->delete($uri, $data = [], $headers = []);
$this->json($method, $uri, $data = [], $headers = []);
$this->call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null);
*/
$response = $this->getJson('/?test=1');
$response->assertOk();
$response->assertSuccessful();
}

public function testRedirectLogin() {
$response = $this->getJson('/redirect');
$response->assertRedirect('/login');
}
}
```

### Test support methods

- `get($uri, $headers = [])`
- `getJson($uri, $headers = [])`
- `post($uri, $data = [], $headers = [])`
- `postJson($uri, $data = [], $headers = [])`
- `put($uri, $data = [], $headers = [])`
- `putJson($uri, $data = [], $headers = [])`
- `patch($uri, $data = [], $headers = [])`
- `patchJson($uri, $data = [], $headers = [])`
- `delete($uri, $data = [], $headers = [])`
- `json($method, $uri, $data = [], $headers = [])`
- `call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)`

### Assert methods

- `assertCookie`
- `assertCookieExpired`
- `assertCookieMissing`
- `assertCookieNotExpired`
- `assertCreated`
- `assertDontSee`
- `assertDontSeeText`
- `assertExactJson`
- `assertForbidden`
- `assertHeader`
- `assertHeaderMissing`
- `assertJson`
- `assertJsonCount`
- `assertJsonMessage`
- `assertJsonMissing`
- `assertJsonMissingExact`
- `assertJsonPath`
- `assertJsonStructure`
- `assertLocation`
- `assertNoContent`
- `assertNotFound`
- `assertOk`
- `assertRedirect`
- `assertSee`
- `assertSeeText`
- `assertStatus`
- `assertSuccessful`
- `assertUnauthorized`
23 changes: 16 additions & 7 deletions src/Helper/MakeHttpRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,13 @@ public function json(string $method, string $uri, array $data = [], array $heade
/**
* Call the given URI and return the Response.
*
* @param string $method
* @param string $uri
* @param array $parameters
* @param array $cookies
* @param array $files
* @param array $server
* @param string|null $content
* @param string $uri The URI
* @param string $method The HTTP method
* @param array $parameters The query (GET) or request (POST) parameters
* @param array $cookies The request cookies ($_COOKIE)
* @param array $files The request files ($_FILES)
* @param array $server The server parameters ($_SERVER)
* @param string|null $content
*
* @return TestResponse
*/
Expand All @@ -358,6 +358,15 @@ public function call(
array $server = [],
?string $content = null
): TestResponse {
if (strtoupper($method) == 'GET' && $parameters) {
if (strpos($uri, '?') !== false) {
$uri .= '&' . http_build_query($parameters);
} else {
$uri .= '?' . http_build_query($parameters);
}
$parameters = [];
}

$response = $this->runApp(
$method,
$uri,
Expand Down
20 changes: 20 additions & 0 deletions test/AbstractActionTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,24 @@ public function testJsonMethods($callMethod, ...$args)
$response->assertOk()
->assertJson(['a' => 123]);
}

public function testCallGetParameters()
{
/** @var Application $app */
$app = $this->container->get(Application::class);
$app->get('/', function (ServerRequestInterface $request) {
$this->assertEquals(['test' => '123'], $request->getQueryParams());
return new Response\EmptyResponse();
});
$app->get('/test2', function (ServerRequestInterface $request) {
$this->assertEquals(['test' => '123', 'key2' => 'val2'], $request->getQueryParams());
return new Response\EmptyResponse();
});

$response = $this->call('GET', '/', ['test' => '123']);
$response->assertNoContent();

$response = $this->call('GET', '/test2?key2=val2', ['test' => '123']);
$response->assertNoContent();
}
}

0 comments on commit fb65376

Please sign in to comment.