-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Testing MessageValidator::lintMessage method #44
Merged
kocsismate
merged 1 commit into
woohoolabs:master
from
Djuki:djuki-message-validator-tests/master
Oct 17, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,7 +105,7 @@ public function validateQueryParams() | |
protected function isValidMediaTypeHeader($headerName) | ||
{ | ||
$header = $this->getHeaderLine($headerName); | ||
return (strpos($header, "application/vnd.api+json") === false || $header === "application/vnd.api+json"); | ||
return strpos($header, "application/vnd.api+json") !== false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to fix this method since it was returning true for any kind of header. After this change, it returns TRUE only if the header line is |
||
} | ||
|
||
protected function setIncludedFields() | ||
|
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,124 @@ | ||
<?php | ||
namespace WoohooLabs\Yin\JsonApi\Negotiation; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use WoohooLabs\Yin\JsonApi\Request\Request; | ||
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory; | ||
|
||
class RequestValidatorTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* Test valid request without Request validation Exceptions | ||
* @test | ||
*/ | ||
public function negotiateValidRequest() | ||
{ | ||
$server = $this->getMockForAbstractClass('\Psr\Http\Message\ServerRequestInterface'); | ||
$exceptionFactory = $this->getMockForAbstractClass('\WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface'); | ||
|
||
$request = $this->createRequestMock($server, $exceptionFactory); | ||
|
||
$request->expects($this->once()) | ||
->method('validateContentTypeHeader') | ||
->will($this->returnValue(true)); | ||
; | ||
|
||
$request->expects($this->once()) | ||
->method('validateAcceptHeader') | ||
->will($this->returnValue(true)); | ||
; | ||
|
||
$validator = $this->createRequestValidator($server); | ||
|
||
$validator->negotiate($request); | ||
} | ||
|
||
|
||
/** | ||
* @test | ||
* @dataProvider getInvalidContentTypes | ||
* @expectedException \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnsupported | ||
*/ | ||
public function negotiateTrowMediaTypeUnsupported($contentType) | ||
{ | ||
// Content type is invalid Accept is valid | ||
$server = $this->createServerRequest($contentType, 'application/vnd.api+json'); | ||
|
||
$request = $this->createRequest($server, $contentType); | ||
$validator = $this->createRequestValidator($server); | ||
|
||
$validator->negotiate($request); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider getInvalidContentTypes | ||
* @expectedException \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnacceptable | ||
*/ | ||
public function negotiateThrowTypeUnacceptable($accept) | ||
{ | ||
// Content Type is valid, Accept is invalid | ||
$server = $this->createServerRequest('application/vnd.api+json', $accept); | ||
|
||
$request = $this->createRequest($server, 'application/vnd.api+json'); | ||
$validator = $this->createRequestValidator($server); | ||
|
||
$validator->negotiate($request); | ||
} | ||
|
||
public function createServerRequest($contentType, $accept = '') | ||
{ | ||
$server = $this->getMockForAbstractClass('\Psr\Http\Message\ServerRequestInterface'); | ||
|
||
$map = array( | ||
array('Content-Type', $contentType), | ||
array('Accept', $accept) | ||
); | ||
$server->expects($this->any()) | ||
->method('getHeaderLine') | ||
->will($this->returnValueMap($map)); | ||
|
||
|
||
return $server; | ||
} | ||
|
||
|
||
public function createRequest($server, $contentType) | ||
{ | ||
$exceptionInterface = new DefaultExceptionFactory($server); | ||
|
||
$request = new Request($server, $exceptionInterface); | ||
|
||
return $request; | ||
} | ||
|
||
protected function createRequestMock($server, $exceptionFactory) | ||
{ | ||
return $this->getMockForAbstractClass('\WoohooLabs\Yin\JsonApi\Request\RequestInterface', [$server, $exceptionFactory]); | ||
} | ||
|
||
|
||
private function createRequestValidator($server, $includeOriginalMessageResponse = true) | ||
{ | ||
$exceptionInterface = new DefaultExceptionFactory($server); | ||
return new RequestValidator($exceptionInterface, $includeOriginalMessageResponse); | ||
} | ||
|
||
public function getInvalidContentTypes() | ||
{ | ||
return [ | ||
['application/zip'], | ||
['application/octet-stream'], | ||
['application/ms-word'], | ||
['application/json'], | ||
['application/x-javascript'], | ||
]; | ||
} | ||
|
||
public function getValidContentTypes() | ||
{ | ||
return [ | ||
['application/vnd.api+json'] | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kocsismate MessageValidator is changed by your request. All methods are protected, and we do not need to test this class anymore.