-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from RyanNerd/unit_testing
Added some unit tests and fixed an issue with JsonBodyParser
- Loading branch information
Showing
4 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\RequestHandlerInterface as RequestHandler; | ||
use Willow\Middleware\JsonBodyParser; | ||
|
||
final class JsonBodyParserTest extends TestCase | ||
{ | ||
public function testSanity() | ||
{ | ||
$this->assertTrue(true); | ||
} | ||
|
||
/** | ||
* JsonBodyParser should reject any Content-Type for POST other than application/json | ||
*/ | ||
public function testInvalidContentTypePost() | ||
{ | ||
$request = $this->createMock(Request::class); | ||
$request->expects($this->once())->method('getHeaderLine')->willReturn('text/bogus'); | ||
$request->expects($this->once())->method('getMethod')->willReturn('POST'); | ||
$requestHandler = $this->createMock(RequestHandler::class); | ||
$jsonBodyParser = new JsonBodyParser(); | ||
$response = $jsonBodyParser->process($request, $requestHandler); | ||
|
||
$this->assertInstanceOf(Response::class, $response); | ||
|
||
$bodyStream = $response->getBody(); | ||
$bodyStream->rewind(); | ||
$responseBody = $bodyStream->getContents(); | ||
$responseJSON = json_decode($responseBody, true); | ||
|
||
$this->assertFalse($responseJSON['authenticated']); | ||
$this->assertFalse($responseJSON['success']); | ||
$this->assertEquals(400, $responseJSON['status']); | ||
$this->assertNull($responseJSON['data']); | ||
$this->assertEquals('Invalid Content-Type: text/bogus', $responseJSON['message']); | ||
$this->assertIsNumeric($responseJSON['timestamp']); | ||
} | ||
|
||
/** | ||
* JsonBodyParser should reject any Content-Type for PATCH other than application/json | ||
*/ | ||
public function testInvalidContentTypePatch() | ||
{ | ||
$request = $this->createMock(Request::class); | ||
$request->expects($this->once())->method('getHeaderLine')->willReturn('text/bogus'); | ||
$request->expects($this->once())->method('getMethod')->willReturn('PATCH'); | ||
$requestHandler = $this->createMock(RequestHandler::class); | ||
$jsonBodyParser = new JsonBodyParser(); | ||
$response = $jsonBodyParser->process($request, $requestHandler); | ||
|
||
$this->assertInstanceOf(Response::class, $response); | ||
|
||
$bodyStream = $response->getBody(); | ||
$bodyStream->rewind(); | ||
$responseBody = $bodyStream->getContents(); | ||
$responseJSON = json_decode($responseBody, true); | ||
|
||
$this->assertFalse($responseJSON['authenticated']); | ||
$this->assertFalse($responseJSON['success']); | ||
$this->assertEquals(400, $responseJSON['status']); | ||
$this->assertNull($responseJSON['data']); | ||
$this->assertEquals('Invalid Content-Type: text/bogus', $responseJSON['message']); | ||
$this->assertIsNumeric($responseJSON['timestamp']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd" | ||
backupGlobals="true" | ||
backupStaticAttributes="false" | ||
bootstrap="../vendor/autoload.php" | ||
cacheResult="false" | ||
cacheTokens="false" | ||
colors="false" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
forceCoversAnnotation="false" | ||
printerClass="PHPUnit\TextUI\ResultPrinter"> | ||
<testsuites> | ||
<testsuite name="willow-app Tests"> | ||
<directory suffix="Test.php">tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">../app</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |