Skip to content

Commit

Permalink
more tests for WebRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Jun 21, 2020
1 parent 4ef4fa0 commit ab5bd91
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions tests/Provide/Router/WebRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BEAR\Sunday\Provide\Router;

use BEAR\Sunday\Exception\BadRequestJsonException;
use PHPUnit\Framework\TestCase;

class WebRouterTest extends TestCase
Expand Down Expand Up @@ -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('', []);
Expand Down

0 comments on commit ab5bd91

Please sign in to comment.