Skip to content

Commit

Permalink
Merge pull request #69 from piotrbelina/feature/fix-invalid-sort-value
Browse files Browse the repository at this point in the history
Fixed invalid sort value type error
  • Loading branch information
kocsismate authored Feb 6, 2018
2 parents efb8703 + 823f1fb commit ec09233
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/JsonApi/Exception/DefaultExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ public function createQueryParamUnrecognizedException(
return new QueryParamUnrecognized($queryParamName);
}

public function createQueryParamMalformedException(
RequestInterface $request,
string $queryParamName,
$queryParamValue
): QueryParamMalformed {
return new QueryParamMalformed($queryParamName, $queryParamValue);
}

public function createRelationshipNotExistsException(string $relationship): RelationshipNotExists
{
return new RelationshipNotExists($relationship);
Expand Down
5 changes: 5 additions & 0 deletions src/JsonApi/Exception/ExceptionFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function createMediaTypeUnacceptableException(RequestInterface $request,
*/
public function createQueryParamUnrecognizedException(RequestInterface $request, string $queryParamName);

/**
* @return JsonApiExceptionInterface|Exception
*/
public function createQueryParamMalformedException(RequestInterface $request, string $queryParamName, $queryParamValue);

/**
* @return JsonApiExceptionInterface|Exception
*/
Expand Down
54 changes: 54 additions & 0 deletions src/JsonApi/Exception/QueryParamMalformed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);

namespace WoohooLabs\Yin\JsonApi\Exception;

use WoohooLabs\Yin\JsonApi\Schema\Error;
use WoohooLabs\Yin\JsonApi\Schema\ErrorSource;

class QueryParamMalformed extends JsonApiException
{
/**
* @var string
*/
protected $malformedQueryParam;

/**
* @var mixed
*/
protected $malformedQueryParamValue;

/**
* QueryParamMalformed constructor.
* @param string $malformedQueryParam
* @param mixed $malformedQueryParamValue
*/
public function __construct(string $malformedQueryParam, $malformedQueryParamValue)
{
parent::__construct("Query parameter '$malformedQueryParam' is malformed!");
$this->malformedQueryParam = $malformedQueryParam;
$this->malformedQueryParamValue = $malformedQueryParamValue;
}

protected function getErrors(): array
{
return [
Error::create()
->setStatus("400")
->setCode("QUERY_PARAM_MALFORMED")
->setTitle("Query parameter is malformed")
->setDetail("Query parameter '$this->malformedQueryParam' is malformed!")
->setSource(ErrorSource::fromParameter($this->malformedQueryParam))
];
}

public function getMalformedQueryParam(): string
{
return $this->malformedQueryParam;
}

public function getMalformedQueryParamValue()
{
return $this->malformedQueryParamValue;
}
}
5 changes: 5 additions & 0 deletions src/JsonApi/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface;
use WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnacceptable;
use WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnsupported;
use WoohooLabs\Yin\JsonApi\Exception\QueryParamMalformed;
use WoohooLabs\Yin\JsonApi\Exception\QueryParamUnrecognized;
use WoohooLabs\Yin\JsonApi\Hydrator\Relationship\ToManyRelationship;
use WoohooLabs\Yin\JsonApi\Hydrator\Relationship\ToOneRelationship;
Expand Down Expand Up @@ -278,8 +279,12 @@ public function getSorting(): array
protected function setSorting()
{
$sortingQueryParam = $this->getQueryParam("sort", "");
if (!is_string($sortingQueryParam)) {
throw $this->exceptionFactory->createQueryParamMalformedException($this, "sort", $sortingQueryParam);
}
if ($sortingQueryParam === "") {
$this->sorting = [];

return;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/JsonApi/Exception/QueryParamMalformedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace WoohooLabs\Yin\Tests\JsonApi\Exception;

use PHPUnit\Framework\TestCase;
use WoohooLabs\Yin\JsonApi\Exception\QueryParamMalformed;

class QueryParamMalformedTest extends TestCase
{
/**
* @test
*/
public function getQueryParam()
{
$queryParam = "sort";
$queryParamValue = ["field" => "asc"];

$exception = $this->createException($queryParam, $queryParamValue);
$this->assertEquals($queryParam, $exception->getMalformedQueryParam());
$this->assertEquals($queryParamValue, $exception->getMalformedQueryParamValue());
}

private function createException($queryParam, $queryParamValue): QueryParamMalformed
{
return new QueryParamMalformed($queryParam, $queryParamValue);
}
}
14 changes: 14 additions & 0 deletions tests/JsonApi/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;
use WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnacceptable;
use WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnsupported;
use WoohooLabs\Yin\JsonApi\Exception\QueryParamMalformed;
use WoohooLabs\Yin\JsonApi\Exception\QueryParamUnrecognized;
use WoohooLabs\Yin\JsonApi\Request\Pagination\CursorBasedPagination;
use WoohooLabs\Yin\JsonApi\Request\Pagination\FixedPageBasedPagination;
Expand Down Expand Up @@ -417,6 +418,19 @@ public function getSortingWhenNotEmpty()
$this->assertEquals($sorting, $request->getSorting());
}

/**
* @test
*/
public function getSortingWhenMalformed()
{
$this->expectException(QueryParamMalformed::class);

$queryParams = ["sort" => ["name" => "asc"]];

$request = $this->createRequestWithQueryParams($queryParams);
$request->getSorting();
}

/**
* @test
*/
Expand Down

0 comments on commit ec09233

Please sign in to comment.