Skip to content

Commit

Permalink
Merge pull request #44 from Djuki/djuki-message-validator-tests/master
Browse files Browse the repository at this point in the history
Testing MessageValidator::lintMessage method
  • Loading branch information
kocsismate authored Oct 17, 2016
2 parents 6d6441b + 0a111bb commit 88e6c80
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 3 deletions.
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 "";
}

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;
}

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']
];
}
}

0 comments on commit 88e6c80

Please sign in to comment.