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

Fixed invalid sort value type error #69

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