Skip to content

Commit

Permalink
Fix regression with top level links of Error Documents
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Nov 23, 2018
1 parent 26ad552 commit f83eea3
Show file tree
Hide file tree
Showing 19 changed files with 36 additions and 70 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ env:
matrix:
fast_finish: true
include:
- php: 7.1
- php: 7.2
env:
- EXECUTE_COVERAGE=true
Expand Down
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ ADDED:

CHANGED:

- Increased minimum PHP version requirement to 7.2
- Updated `justinrainbow/json-schema` to v5.2
- Moved classes related to documents (__BREAKING__):
- `WoohooLabs\Yin\JsonApi\Document\AbstractCollectionDocument` to `WoohooLabs\Yin\JsonApi\Schema\Document\AbstractCollectionDocument`
Expand All @@ -28,9 +27,9 @@ CHANGED:
- `WoohooLabs\Yin\JsonApi\Schema\LinkObject` to `WoohooLabs\Yin\JsonApi\Schema\Link\LinkObject`
- Links are used according to the spec (__BREAKING__):
- `AbstractSuccessfulDocument::getLinks()` returns `?DocumentLinks` instead of `?Links`
- `AbstractErrorDocument::getLinks()` returns `?ErrorLinks` instead of `?Links`
- `ErrorDocument::getLinks()` returns `?ErrorLinks` instead of `?Links`
- `ErrorDocument::setLinks()` expects a parameter of `?ErrorLinks` type instead of `?Links`
- `AbstractErrorDocument::getLinks()` returns `?DocumentLinks` instead of `?Links`
- `ErrorDocument::getLinks()` returns `?DocumentLinks` instead of `?Links`
- `ErrorDocument::setLinks()` expects a parameter of `?DocumentLinks` type instead of `?Links`
- `AbstractResource::getLinks()` returns `?ResourceLinks` instead of `Links`
- `AbstractRelationship::getLinks()` returns `?RelationshipLinks` instead of `Links`
- `AbstractRelationship::setLinks()` expects a parameter of `?RelationshipLinks` type instead of `Links`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"source": "https://github.com/woohoolabs/yin"
},
"require": {
"php": "^7.2.0",
"php": "^7.1.0",
"psr/http-message-implementation": "^1.0.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion examples/Book/JsonApi/Document/AuthorsDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getMeta(): array
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* The method returns a new DocumentLinks object if you want to provide linkage data
* for the document or null if the section should be omitted from the response.
*/
public function getLinks(): ?DocumentLinks
Expand Down
2 changes: 1 addition & 1 deletion examples/Book/JsonApi/Document/BookDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getMeta(): array
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* The method returns a new DocumentLinks object if you want to provide linkage data
* for the document or null if the section should be omitted from the response.
*/
public function getLinks(): ?DocumentLinks
Expand Down
2 changes: 1 addition & 1 deletion examples/Book/JsonApi/Document/BooksDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getMeta(): array
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* The method returns a new DocumentLinks object if you want to provide linkage data
* for the document or null if the section should be omitted from the response.
*/
public function getLinks(): ?DocumentLinks
Expand Down
2 changes: 1 addition & 1 deletion examples/User/JsonApi/Document/UserDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function getMeta(): array
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* The method returns a new DocumentLinks object if you want to provide linkage data
* for the document or null if the section should be omitted from the response.
*/
public function getLinks(): ?DocumentLinks
Expand Down
2 changes: 1 addition & 1 deletion examples/User/JsonApi/Document/UsersDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getMeta(): array
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* The method returns a new DocumentLinks object if you want to provide linkage data
* for the document or null if the section should be omitted from the response.
*/
public function getLinks(): ?DocumentLinks
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApi/Response/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function genericError(ErrorDocumentInterface $document, ?int $statusCode
private function getResponseWithLocationHeader(ResourceDocumentInterface $document, ResponseInterface $response): ResponseInterface
{
$links = $document->getLinks();
if ($links instanceof DocumentLinks && $links->getSelf() !== null) {
if ($links && $links->getSelf() !== null) {
$response = $response->withHeader("location", $links->getSelf()->getHref());
}

Expand Down
9 changes: 0 additions & 9 deletions src/JsonApi/Schema/Document/AbstractCollectionDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use WoohooLabs\Yin\JsonApi\Schema\Data\CollectionData;
use WoohooLabs\Yin\JsonApi\Schema\Data\DataInterface;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;
use WoohooLabs\Yin\JsonApi\Schema\Resource\ResourceInterface;
use WoohooLabs\Yin\JsonApi\Transformer\ResourceDocumentTransformation;
use WoohooLabs\Yin\JsonApi\Transformer\ResourceTransformation;
Expand All @@ -18,14 +17,6 @@ abstract class AbstractCollectionDocument extends AbstractResourceDocument
*/
protected $resource;

/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* for the document or null if the member should be omitted from the response.
*/
abstract public function getLinks(): ?DocumentLinks;

public function __construct(ResourceInterface $resource)
{
$this->resource = $resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,11 @@

use WoohooLabs\Yin\JsonApi\Schema\Data\DataInterface;
use WoohooLabs\Yin\JsonApi\Schema\Data\SingleResourceData;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;
use WoohooLabs\Yin\JsonApi\Transformer\ResourceDocumentTransformation;
use WoohooLabs\Yin\JsonApi\Transformer\ResourceTransformer;

abstract class AbstractSimpleResourceDocument extends AbstractResourceDocument
{
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* for the document or null if the member should be omitted from the response.
*/
abstract public function getLinks(): ?DocumentLinks;

abstract protected function getResource(): array;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use WoohooLabs\Yin\JsonApi\Schema\Data\DataInterface;
use WoohooLabs\Yin\JsonApi\Schema\Data\SingleResourceData;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;
use WoohooLabs\Yin\JsonApi\Schema\Resource\ResourceInterface;
use WoohooLabs\Yin\JsonApi\Transformer\ResourceDocumentTransformation;
use WoohooLabs\Yin\JsonApi\Transformer\ResourceTransformation;
Expand All @@ -18,14 +17,6 @@ abstract class AbstractSingleResourceDocument extends AbstractResourceDocument
*/
protected $resource;

/**
* Provides information about the "links" member of the current document.
*
* The method returns a new Links object if you want to provide linkage data
* for the document or null if the member should be omitted from the response.
*/
abstract public function getLinks(): ?DocumentLinks;

public function __construct(ResourceInterface $resource)
{
$this->resource = $resource;
Expand Down
4 changes: 1 addition & 3 deletions src/JsonApi/Schema/Document/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use WoohooLabs\Yin\JsonApi\Schema\JsonApiObject;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;
use WoohooLabs\Yin\JsonApi\Schema\Link\ErrorLinks;

interface DocumentInterface
{
Expand All @@ -30,7 +29,6 @@ public function getMeta(): array;
*
* The method returns a new Links object if you want to provide linkage data
* for the document or null if the member should be omitted from the response.
* @return DocumentLinks|ErrorLinks|null
*/
public function getLinks();
public function getLinks(): ?DocumentLinks;
}
8 changes: 4 additions & 4 deletions src/JsonApi/Schema/Document/ErrorDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use WoohooLabs\Yin\JsonApi\Schema\Error\Error;
use WoohooLabs\Yin\JsonApi\Schema\JsonApiObject;
use WoohooLabs\Yin\JsonApi\Schema\Link\ErrorLinks;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;

class ErrorDocument extends AbstractErrorDocument
{
Expand All @@ -20,7 +20,7 @@ class ErrorDocument extends AbstractErrorDocument
protected $meta = [];

/**
* @var ErrorLinks|null
* @var DocumentLinks|null
*/
protected $links;

Expand Down Expand Up @@ -58,12 +58,12 @@ public function setMeta(array $meta): ErrorDocument
return $this;
}

public function getLinks(): ?ErrorLinks
public function getLinks(): ?DocumentLinks
{
return $this->links;
}

public function setLinks(?ErrorLinks $links): ErrorDocument
public function setLinks(?DocumentLinks $links): ErrorDocument
{
$this->links = $links;

Expand Down
9 changes: 0 additions & 9 deletions src/JsonApi/Schema/Document/ErrorDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
namespace WoohooLabs\Yin\JsonApi\Schema\Document;

use WoohooLabs\Yin\JsonApi\Schema\Error\Error;
use WoohooLabs\Yin\JsonApi\Schema\Link\ErrorLinks;

interface ErrorDocumentInterface extends DocumentInterface
{
/**
* Provides information about the "links" member of the current document.
*
* The method returns a new ErrorLinks object if you want to provide linkage data
* for the document or null if the member should be omitted from the response.
*/
public function getLinks(): ?ErrorLinks;

/**
* @return Error[]
*/
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApi/Schema/Link/DocumentLinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DocumentLinks extends AbstractLinks
/**
* @var Link[]
*/
protected $profiles;
protected $profiles = [];

/**
* @param Link[] $links
Expand Down
7 changes: 4 additions & 3 deletions tests/JsonApi/Double/StubErrorDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WoohooLabs\Yin\JsonApi\Schema\Document\AbstractErrorDocument;
use WoohooLabs\Yin\JsonApi\Schema\Error\Error;
use WoohooLabs\Yin\JsonApi\Schema\JsonApiObject;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;
use WoohooLabs\Yin\JsonApi\Schema\Link\ErrorLinks;

class StubErrorDocument extends AbstractErrorDocument
Expand All @@ -21,14 +22,14 @@ class StubErrorDocument extends AbstractErrorDocument
protected $meta;

/**
* @var ErrorLinks|null
* @var DocumentLinks|null
*/
protected $links;

/**
* @param Error[] $errors
*/
public function __construct(?JsonApiObject $jsonApi = null, array $meta = [], ?ErrorLinks $links = null, array $errors = [])
public function __construct(?JsonApiObject $jsonApi = null, array $meta = [], ?DocumentLinks $links = null, array $errors = [])
{
$this->jsonApi = $jsonApi;
$this->meta = $meta;
Expand All @@ -46,7 +47,7 @@ public function getMeta(): array
return $this->meta;
}

public function getLinks(): ?ErrorLinks
public function getLinks(): ?DocumentLinks
{
return $this->links;
}
Expand Down
23 changes: 13 additions & 10 deletions tests/JsonApi/Schema/Document/ErrorDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PHPUnit\Framework\TestCase;
use WoohooLabs\Yin\JsonApi\Schema\Document\ErrorDocument;
use WoohooLabs\Yin\JsonApi\Schema\JsonApiObject;
use WoohooLabs\Yin\JsonApi\Schema\Link\ErrorLinks;
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;

class ErrorDocumentTest extends TestCase
{
Expand All @@ -15,32 +15,35 @@ class ErrorDocumentTest extends TestCase
*/
public function getJsonApi()
{
$jsonApi = new JsonApiObject("1.0");
$errorDocument = $this->createErrorDocument();

$errorDocument = $this->createErrorDocument()->setJsonApi($jsonApi);
$this->assertEquals($jsonApi, $errorDocument->getJsonApi());
$errorDocument->setJsonApi(new JsonApiObject("1.0"));

$this->assertEquals(new JsonApiObject("1.0"), $errorDocument->getJsonApi());
}

/**
* @test
*/
public function getMeta()
{
$meta = ["abc" => "def"];
$errorDocument = $this->createErrorDocument();

$errorDocument->setMeta(["abc" => "def"]);

$errorDocument = $this->createErrorDocument()->setMeta($meta);
$this->assertEquals($meta, $errorDocument->getMeta());
$this->assertEquals(["abc" => "def"], $errorDocument->getMeta());
}

/**
* @test
*/
public function getLinks()
{
$links = new ErrorLinks("https://example.com");
$errorDocument = $this->createErrorDocument();

$errorDocument->setLinks(new DocumentLinks("https://example.com"));

$errorDocument = $this->createErrorDocument()->setLinks($links);
$this->assertEquals($links, $errorDocument->getLinks());
$this->assertEquals(new DocumentLinks("https://example.com"), $errorDocument->getLinks());
}

private function createErrorDocument(): ErrorDocument
Expand Down
4 changes: 2 additions & 2 deletions tests/JsonApi/Transformer/DocumentTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public function transformErrorDocumentWithMeta()
*/
public function transformErrorDocumentWithLinks()
{
$document = $this->createErrorDocument(null, [], new ErrorLinks());
$document = $this->createErrorDocument(null, [], new DocumentLinks());

$transformedDocument = $this->toErrorDocument($document);

Expand Down Expand Up @@ -429,7 +429,7 @@ private function createDocument(
private function createErrorDocument(
?JsonApiObject $jsonApi = null,
array $meta = [],
?ErrorLinks $links = null,
?DocumentLinks $links = null,
array $errors = []
): ErrorDocumentInterface {
return new StubErrorDocument($jsonApi, $meta, $links, $errors);
Expand Down

0 comments on commit f83eea3

Please sign in to comment.