From 4d16fde6c8d3a63cd1a0c00cdba56e13a70144d4 Mon Sep 17 00:00:00 2001 From: Moln Date: Mon, 13 Jan 2020 12:59:17 +0800 Subject: [PATCH 1/2] Update README.md --- README.md | 88 +++++++++++++++++++++++++++-- src/Helper/MakeHttpRequestTrait.php | 23 +++++--- 2 files changed, 100 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f872a3d..1fad8e6 100644 --- a/README.md +++ b/README.md @@ -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( @@ -44,8 +45,7 @@ class HomePageTest extend AbstractActionTestCase { } public function testPassMiddlewareOrMockService() { - - + $this->container->setService('some middleware', new PassMiddleware()); $mock = $this->createMock(SomeService::class); @@ -63,4 +63,84 @@ class HomePageTest extend AbstractActionTestCase { $this->assertInstanceOf(ResponseInterface::class, $response); } } -``` \ No newline at end of file +``` + +### 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` diff --git a/src/Helper/MakeHttpRequestTrait.php b/src/Helper/MakeHttpRequestTrait.php index 47f3d96..9628741 100644 --- a/src/Helper/MakeHttpRequestTrait.php +++ b/src/Helper/MakeHttpRequestTrait.php @@ -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 */ @@ -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, From 4a464bfe71b420e3f73d9982f39407474429b887 Mon Sep 17 00:00:00 2001 From: Moln Date: Mon, 13 Jan 2020 13:12:55 +0800 Subject: [PATCH 2/2] Add tests. --- test/AbstractActionTestCaseTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/AbstractActionTestCaseTest.php b/test/AbstractActionTestCaseTest.php index ed56dd4..923c8e9 100644 --- a/test/AbstractActionTestCaseTest.php +++ b/test/AbstractActionTestCaseTest.php @@ -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(); + } }