Skip to content

Commit

Permalink
Relationship with no "data" member. Fixed #92 (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath authored Feb 25, 2019
1 parent 668ec0c commit 5f47c15
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: php
php:
- '7.1'
- '7.2'
- '7.3'

before_script:
- composer install
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [2.1.0] - 2019-02-25
### Fixed
- Relationship without data property (#92)

## [2.0.1] - 2018-12-31
### Changed
- Downgraded min required php version to 7.1
Expand All @@ -14,5 +18,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added
- v2 initial release

[Unreleased]: https://github.com/json-api-php/json-api/compare/2.0.1...HEAD
[Unreleased]: https://github.com/json-api-php/json-api/compare/2.1.0...HEAD
[2.1.0]: https://github.com/json-api-php/json-api/compare/2.0.1...2.1.0
[2.0.1]: https://github.com/json-api-php/json-api/compare/2.0.0...2.0.1
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
bootstrap="vendor/autoload.php"
stopOnFailure="false"
verbose="true"
colors="true"
syntaxCheck="true">
colors="true">
<testsuites>
<testsuite name="Main">
<directory>./test</directory>
Expand Down
31 changes: 31 additions & 0 deletions src/EmptyRelationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace JsonApiPhp\JsonApi;

use JsonApiPhp\JsonApi\Internal\RelationshipMember;
use JsonApiPhp\JsonApi\Internal\ResourceField;
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;

/**
* A relationship with no data
*/
class EmptyRelationship implements ResourceField
{
use ResourceFieldTrait;

private $obj;

public function __construct(string $name, RelationshipMember $member, RelationshipMember ...$members)
{
$this->name = $name;
$this->obj = combine($member, ...$members);
}

/**
* @param object $o
*/
public function attachTo($o): void
{
child($o, 'relationships')->{$this->name} = $this->obj;
}
}
2 changes: 1 addition & 1 deletion src/Error/SourceParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace JsonApiPhp\JsonApi\Error;

use JsonApiPhp\JsonApi\Internal\ErrorMember;
use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\ErrorMember;

final class SourceParameter implements ErrorMember
{
Expand Down
2 changes: 1 addition & 1 deletion src/Error/SourcePointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace JsonApiPhp\JsonApi\Error;

use JsonApiPhp\JsonApi\Internal\ErrorMember;
use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\ErrorMember;

final class SourcePointer implements ErrorMember
{
Expand Down
10 changes: 10 additions & 0 deletions src/Internal/RelationshipMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace JsonApiPhp\JsonApi\Internal;

/**
* @internal
*/
interface RelationshipMember extends ToOneMember, ToManyMember
{
}
5 changes: 4 additions & 1 deletion src/Internal/ToManyMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace JsonApiPhp\JsonApi\Internal;

interface ToManyMember extends ToOneMember
/**
* @internal
*/
interface ToManyMember extends Attachable
{
}
2 changes: 1 addition & 1 deletion src/Link/AboutLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JsonApiPhp\JsonApi\Link;

use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\ErrorMember;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use function JsonApiPhp\JsonApi\child;

final class AboutLink implements ErrorMember
{
Expand Down
2 changes: 1 addition & 1 deletion src/Link/FirstLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JsonApiPhp\JsonApi\Link;

use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\PaginationLink;
use function JsonApiPhp\JsonApi\child;

final class FirstLink implements PaginationLink
{
Expand Down
2 changes: 1 addition & 1 deletion src/Link/LastLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JsonApiPhp\JsonApi\Link;

use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\PaginationLink;
use function JsonApiPhp\JsonApi\child;

final class LastLink implements PaginationLink
{
Expand Down
2 changes: 1 addition & 1 deletion src/Link/NextLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JsonApiPhp\JsonApi\Link;

use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\PaginationLink;
use function JsonApiPhp\JsonApi\child;

final class NextLink implements PaginationLink
{
Expand Down
2 changes: 1 addition & 1 deletion src/Link/PrevLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace JsonApiPhp\JsonApi\Link;

use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\PaginationLink;
use function JsonApiPhp\JsonApi\child;

final class PrevLink implements PaginationLink
{
Expand Down
6 changes: 3 additions & 3 deletions src/Link/RelatedLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace JsonApiPhp\JsonApi\Link;

use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\ToOneMember;
use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\RelationshipMember;

final class RelatedLink implements ToOneMember
final class RelatedLink implements RelationshipMember
{
use LinkTrait;

Expand Down
6 changes: 3 additions & 3 deletions src/Link/SelfLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace JsonApiPhp\JsonApi\Link;

use function JsonApiPhp\JsonApi\child;
use JsonApiPhp\JsonApi\Internal\DataDocumentMember;
use JsonApiPhp\JsonApi\Internal\LinkTrait;
use JsonApiPhp\JsonApi\Internal\RelationshipMember;
use JsonApiPhp\JsonApi\Internal\ResourceMember;
use JsonApiPhp\JsonApi\Internal\ToOneMember;
use function JsonApiPhp\JsonApi\child;

final class SelfLink implements DataDocumentMember, ResourceMember, ToOneMember
final class SelfLink implements DataDocumentMember, ResourceMember, RelationshipMember
{
use LinkTrait;

Expand Down
4 changes: 2 additions & 2 deletions src/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use JsonApiPhp\JsonApi\Internal\ErrorDocumentMember;
use JsonApiPhp\JsonApi\Internal\ErrorMember;
use JsonApiPhp\JsonApi\Internal\MetaDocumentMember;
use JsonApiPhp\JsonApi\Internal\RelationshipMember;
use JsonApiPhp\JsonApi\Internal\ResourceMember;
use JsonApiPhp\JsonApi\Internal\ToOneMember;

final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, ToOneMember
final class Meta implements ErrorMember, ErrorDocumentMember, MetaDocumentMember, DataDocumentMember, ResourceMember, RelationshipMember
{
/**
* @var string
Expand Down
6 changes: 3 additions & 3 deletions src/ToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
use JsonApiPhp\JsonApi\Internal\Identifier;
use JsonApiPhp\JsonApi\Internal\ResourceField;
use JsonApiPhp\JsonApi\Internal\ResourceFieldTrait;
use JsonApiPhp\JsonApi\Internal\ToOneMember;
use JsonApiPhp\JsonApi\Internal\ToManyMember;

final class ToMany implements Identifier, ResourceField
{
use ResourceFieldTrait;
/**
* @var ToOneMember[]
* @var ToManyMember[]
*/
private $members;
/**
* @var ResourceIdentifierCollection
*/
private $collection;

public function __construct(string $name, ResourceIdentifierCollection $collection, ToOneMember ...$members)
public function __construct(string $name, ResourceIdentifierCollection $collection, ToManyMember ...$members)
{
$this->validateFieldName($name);
$this->name = $name;
Expand Down
4 changes: 2 additions & 2 deletions test/CompoundDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ public function testIncludedResourceMayBeIdentifiedByAnotherLinkedResource()

/**
* A compound document MUST NOT include more than one resource object for each type and id pair.
* @expectedException \LogicException
* @expectedExceptionMessage Resource apples:1 is already included
*/
public function testCanNotBeManyIncludedResourcesWithEqualIdentifiers()
{
$this->expectException('LogicException');
$this->expectExceptionMessage('Resource apples:1 is already included');
$apple = new ResourceObject('apples', '1');
new CompoundDocument($apple->identifier(), new Included($apple, $apple));
}
Expand Down
58 changes: 58 additions & 0 deletions test/ResourceObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use JsonApiPhp\JsonApi\Attribute;
use JsonApiPhp\JsonApi\DataDocument;
use JsonApiPhp\JsonApi\EmptyRelationship;
use JsonApiPhp\JsonApi\Link\RelatedLink;
use JsonApiPhp\JsonApi\Link\SelfLink;
use JsonApiPhp\JsonApi\Meta;
Expand Down Expand Up @@ -152,6 +153,63 @@ public function testRelationshipWithEmptyMultiIdLinkage()
);
}

public function testRelationshipWithNoData()
{
$this->assertEncodesTo(
'
{
"data": {
"type": "basket",
"id": "1",
"relationships": {
"empty": {
"links": {
"related": "/foo"
}
}
}
}
}
',
new DataDocument(
new ResourceObject(
'basket',
'1',
new EmptyRelationship('empty', new RelatedLink('/foo'))
)
)
);

$this->assertEncodesTo(
'
{
"data": {
"type": "basket",
"id": "1",
"relationships": {
"empty": {
"links": {
"related": "/foo",
"self": "/bar"
},
"meta": {
"foo": "bar"
}
}
}
}
}
',
new DataDocument(
new ResourceObject(
'basket',
'1',
new EmptyRelationship('empty', new RelatedLink('/foo'), new SelfLink('/bar'), new Meta('foo', 'bar'))
)
)
);
}

public function testCanNotCreateIdAttribute()
{
$this->expectException(\DomainException::class);
Expand Down

0 comments on commit 5f47c15

Please sign in to comment.