Skip to content
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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/JsonApi/Negotiation/MessageValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function __construct(ExceptionFactoryInterface $exceptionFactory, $includ
* @param string $message
* @return string
*/
public function lintMessage($message)
protected function lintMessage($message)
{
if (empty($message) === true) {
return null;
return "";
}
Copy link
Contributor Author

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.


try {
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApi/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 application/vnd.api+json. If this is how it should behave than my change is correct.

}

protected function setIncludedFields()
Expand Down
124 changes: 124 additions & 0 deletions tests/JsonApi/Negotiation/RequestValidatorTest.php
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']
];
}
}