From ab5bd9137fb4fcb84ea220dd6ba7aa134459a4d3 Mon Sep 17 00:00:00 2001 From: koriym Date: Mon, 22 Jun 2020 00:12:46 +0900 Subject: [PATCH] more tests for WebRouter --- tests/Provide/Router/WebRouterTest.php | 88 ++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/Provide/Router/WebRouterTest.php b/tests/Provide/Router/WebRouterTest.php index 41d473e2..159cc4a3 100644 --- a/tests/Provide/Router/WebRouterTest.php +++ b/tests/Provide/Router/WebRouterTest.php @@ -4,6 +4,7 @@ namespace BEAR\Sunday\Provide\Router; +use BEAR\Sunday\Exception\BadRequestJsonException; use PHPUnit\Framework\TestCase; class WebRouterTest extends TestCase @@ -53,6 +54,93 @@ public function testMatchWithQuery() : void $this->assertSame(['id' => '1'], $request->query); } + public function testPost() : void + { + $global = [ + '_GET' => [], + '_POST' => ['solstice' => 'eclipse'] + ]; + $server = [ + 'REQUEST_METHOD' => 'POST', + 'REQUEST_URI' => '/' + ]; + $request = $this->router->match($global, $server); + $this->assertSame('post', $request->method); + $this->assertSame('page://self/', $request->path); + $this->assertSame(['solstice' => 'eclipse'], $request->query); + } + + public function testPutFormUrlEncoded() : void + { + $global = [ + '_GET' => [], + '_POST' => [] + ]; + $server = [ + 'REQUEST_METHOD' => 'PUT', + 'REQUEST_URI' => '/', + 'CONTENT_TYPE' => 'application/x-www-form-urlencoded', + 'HTTP_RAW_POST_DATA' => 'solstice=eclipse' + ]; + $request = $this->router->match($global, $server); + $this->assertSame('put', $request->method); + $this->assertSame('page://self/', $request->path); + $this->assertSame(['solstice' => 'eclipse'], $request->query); + } + + public function testPutApplicationJson() : void + { + $global = [ + '_GET' => [], + '_POST' => [] + ]; + $server = [ + 'REQUEST_METHOD' => 'PUT', + 'REQUEST_URI' => '/', + 'CONTENT_TYPE' => 'application/json', + 'HTTP_RAW_POST_DATA' => '{"solstice":"eclipse"}' + ]; + $request = $this->router->match($global, $server); + $this->assertSame('put', $request->method); + $this->assertSame('page://self/', $request->path); + $this->assertSame(['solstice' => 'eclipse'], $request->query); + } + + public function testPutUnknowMediaType() : void + { + $global = [ + '_GET' => [], + '_POST' => [] + ]; + $server = [ + 'REQUEST_METHOD' => 'PUT', + 'REQUEST_URI' => '/', + 'CONTENT_TYPE' => 'application/__unknown__', + 'HTTP_RAW_POST_DATA' => '{"solstice":"eclipse"}' + ]; + $request = $this->router->match($global, $server); + $this->assertSame('put', $request->method); + $this->assertSame('page://self/', $request->path); + $this->assertSame([], $request->query); + } + + public function testPutInvalidJson() : void + { + $this->expectException(BadRequestJsonException::class); + $this->expectExceptionMessage('Syntax error'); + $global = [ + '_GET' => [], + '_POST' => [] + ]; + $server = [ + 'REQUEST_METHOD' => 'PUT', + 'REQUEST_URI' => '/', + 'CONTENT_TYPE' => 'application/json', + 'HTTP_RAW_POST_DATA' => '{"solstice"}' + ]; + $request = $this->router->match($global, $server); + } + public function testGenerate() : void { $actual = (bool) $this->router->generate('', []);