From 4f8d6b363ac33c019648e60016bf2f956da8da64 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 5 Aug 2020 10:36:35 +0200 Subject: [PATCH 1/4] Handle absent relation data different then empty relation data If the relation does not have data (i.e. only meta or links), we should not init the relation as if the data is empty (i.e. null or an empty array). --- src/Concerns/HasRelations.php | 17 +++++----- src/Interfaces/ItemInterface.php | 10 +++--- src/Parsers/ItemParser.php | 9 ++++-- src/Relations/AbstractManyRelation.php | 10 +----- src/Relations/AbstractOneRelation.php | 12 ++----- src/Relations/AbstractRelation.php | 12 +++++-- tests/Concerns/HasRelationsTest.php | 33 ++++++++++++++++++++ tests/Parsers/ItemParserTest.php | 24 ++++++++++++++ tests/Relations/AbstractManyRelationTest.php | 5 +-- 9 files changed, 92 insertions(+), 40 deletions(-) diff --git a/src/Concerns/HasRelations.php b/src/Concerns/HasRelations.php index a5dd81c..7c690fb 100644 --- a/src/Concerns/HasRelations.php +++ b/src/Concerns/HasRelations.php @@ -141,14 +141,14 @@ public function getRelationValue(string $name): ?DataInterface /** * Set the specific relationship on the model. * - * @param string $name - * @param \Swis\JsonApi\Client\Interfaces\DataInterface|null $data - * @param \Swis\JsonApi\Client\Links|null $links - * @param \Swis\JsonApi\Client\Meta|null $meta + * @param string $name + * @param \Swis\JsonApi\Client\Interfaces\DataInterface|false|null $data + * @param \Swis\JsonApi\Client\Links|null $links + * @param \Swis\JsonApi\Client\Meta|null $meta * * @return static */ - public function setRelation(string $name, DataInterface $data = null, Links $links = null, Meta $meta = null) + public function setRelation(string $name, $data = false, Links $links = null, Meta $meta = null) { $method = Str::camel($name); if (method_exists($this, $method)) { @@ -160,8 +160,11 @@ public function setRelation(string $name, DataInterface $data = null, Links $lin $relationObject = $this->morphTo($name); } - if ($data !== null) { - $relationObject->associate($data); + if ($data !== false) { + $relationObject->dissociate(); + if ($data !== null) { + $relationObject->associate($data); + } } $relationObject->setLinks($links); $relationObject->setMeta($meta); diff --git a/src/Interfaces/ItemInterface.php b/src/Interfaces/ItemInterface.php index 1acf886..46decef 100644 --- a/src/Interfaces/ItemInterface.php +++ b/src/Interfaces/ItemInterface.php @@ -127,14 +127,14 @@ public function getAvailableRelations(): array; /** * Set the specific relationship in the model. * - * @param string $relation - * @param \Swis\JsonApi\Client\Interfaces\DataInterface|null $value - * @param \Swis\JsonApi\Client\Links|null $links - * @param \Swis\JsonApi\Client\Meta|null $meta + * @param string $relation + * @param \Swis\JsonApi\Client\Interfaces\DataInterface|false|null $value + * @param \Swis\JsonApi\Client\Links|null $links + * @param \Swis\JsonApi\Client\Meta|null $meta * * @return static */ - public function setRelation(string $relation, DataInterface $value = null, Links $links = null, Meta $meta = null); + public function setRelation(string $relation, $value = false, Links $links = null, Meta $meta = null); /** * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface[]|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface[] diff --git a/src/Parsers/ItemParser.php b/src/Parsers/ItemParser.php index d272045..0ea56d1 100644 --- a/src/Parsers/ItemParser.php +++ b/src/Parsers/ItemParser.php @@ -135,9 +135,12 @@ private function setRelations(ItemInterface $item, $data): void throw new ValidationException('Relationship object MUST contain at least one of the following properties: `links`, `data`, `meta`.'); } - $value = null; - if (property_exists($relationship, 'data') && $relationship->data !== null) { - $value = $this->parseRelationshipData($relationship->data); + $value = false; + if (property_exists($relationship, 'data')) { + $value = null; + if ($relationship->data !== null) { + $value = $this->parseRelationshipData($relationship->data); + } } $links = null; diff --git a/src/Relations/AbstractManyRelation.php b/src/Relations/AbstractManyRelation.php index 6be8c17..405dfc0 100644 --- a/src/Relations/AbstractManyRelation.php +++ b/src/Relations/AbstractManyRelation.php @@ -6,7 +6,7 @@ use Swis\JsonApi\Client\Interfaces\ManyRelationInterface; /** - * @property \Swis\JsonApi\Client\Collection|null $included + * @property \Swis\JsonApi\Client\Collection|false|null $included */ abstract class AbstractManyRelation extends AbstractRelation implements ManyRelationInterface { @@ -22,14 +22,6 @@ public function associate(Collection $included) return $this; } - /** - * @return bool - */ - public function hasIncluded(): bool - { - return $this->getIncluded()->isNotEmpty(); - } - /** * @return \Swis\JsonApi\Client\Collection */ diff --git a/src/Relations/AbstractOneRelation.php b/src/Relations/AbstractOneRelation.php index 7a9478b..5152e88 100644 --- a/src/Relations/AbstractOneRelation.php +++ b/src/Relations/AbstractOneRelation.php @@ -6,7 +6,7 @@ use Swis\JsonApi\Client\Interfaces\OneRelationInterface; /** - * @property \Swis\JsonApi\Client\Interfaces\ItemInterface|null $included + * @property \Swis\JsonApi\Client\Interfaces\ItemInterface|false|null $included */ abstract class AbstractOneRelation extends AbstractRelation implements OneRelationInterface { @@ -27,14 +27,6 @@ public function associate(ItemInterface $included) */ public function getIncluded(): ? ItemInterface { - return $this->included; - } - - /** - * @return bool - */ - public function hasIncluded(): bool - { - return null !== $this->getIncluded(); + return $this->included ?: null; } } diff --git a/src/Relations/AbstractRelation.php b/src/Relations/AbstractRelation.php index 1f6e5b8..590eafd 100644 --- a/src/Relations/AbstractRelation.php +++ b/src/Relations/AbstractRelation.php @@ -11,9 +11,9 @@ abstract class AbstractRelation use HasMeta; /** - * @var \Swis\JsonApi\Client\Interfaces\DataInterface|null + * @var \Swis\JsonApi\Client\Interfaces\DataInterface|false|null */ - protected $included; + protected $included = false; /** * @var bool @@ -30,6 +30,14 @@ public function dissociate() return $this; } + /** + * @return bool + */ + public function hasIncluded(): bool + { + return $this->included !== false; + } + /** * @param bool $omitIncluded * diff --git a/tests/Concerns/HasRelationsTest.php b/tests/Concerns/HasRelationsTest.php index 04742ed..c17ff18 100644 --- a/tests/Concerns/HasRelationsTest.php +++ b/tests/Concerns/HasRelationsTest.php @@ -29,6 +29,7 @@ public function it_can_get_and_set_an_item_as_relation() $relation = $mock->getRelation('foo'); $this->assertInstanceOf(MorphToRelation::class, $relation); + $this->assertTrue($relation->hasIncluded()); $this->assertSame($data, $relation->getIncluded()); } @@ -45,9 +46,41 @@ public function it_can_get_and_set_a_collection_as_relation() $relation = $mock->getRelation('foo'); $this->assertInstanceOf(MorphToManyRelation::class, $relation); + $this->assertTrue($relation->hasIncluded()); $this->assertSame($data, $relation->getIncluded()); } + /** + * @test + */ + public function it_can_get_and_set_null_as_relation() + { + /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ + $mock = $this->getMockForTrait(HasRelations::class); + + $mock->setRelation('foo', null); + + $relation = $mock->getRelation('foo'); + $this->assertInstanceOf(MorphToRelation::class, $relation); + $this->assertTrue($relation->hasIncluded()); + $this->assertNull($relation->getIncluded()); + } + + /** + * @test + */ + public function it_does_not_set_false_as_relation() + { + /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ + $mock = $this->getMockForTrait(HasRelations::class); + + $mock->setRelation('foo', false); + + $relation = $mock->getRelation('foo'); + $this->assertInstanceOf(MorphToRelation::class, $relation); + $this->assertFalse($relation->hasIncluded()); + } + /** * @test */ diff --git a/tests/Parsers/ItemParserTest.php b/tests/Parsers/ItemParserTest.php index 0669f2c..431ddaf 100644 --- a/tests/Parsers/ItemParserTest.php +++ b/tests/Parsers/ItemParserTest.php @@ -490,6 +490,7 @@ public function it_parses_an_empty_has_one_relationship() static::assertInstanceOf(HasOneRelation::class, $item->getRelation('empty')); static::assertNull($item->getRelation('empty')->getLinks()); static::assertNull($item->getRelation('empty')->getMeta()); + static::assertTrue($item->getRelation('empty')->hasIncluded()); static::assertNull($item->getRelation('empty')->getIncluded()); } @@ -601,6 +602,21 @@ public function it_parses_an_unknown_plural_relation_as_morph_to_many() static::assertCount(3, $item->getRelation('morphmany')->getIncluded()); } + /** + * @test + */ + public function it_does_not_set_data_when_there_is_no_data_present() + { + $parser = $this->getItemParser(); + + $item = $parser->parse($this->getJsonApiItemMock('master', '1')); + + static::assertInstanceOf(MorphToRelation::class, $item->getRelation('nodata')); + static::assertInstanceOf(Links::class, $item->getRelation('nodata')->getLinks()); + static::assertInstanceOf(Meta::class, $item->getRelation('nodata')->getMeta()); + static::assertFalse($item->getRelation('nodata')->hasIncluded()); + } + /** * @test */ @@ -753,6 +769,14 @@ private function getJsonApiItemMock($type, $id) 'empty' => [ 'data' => null, ], + 'nodata' => [ + 'links' => [ + 'self' => 'http://example.com/'.$type.'/'.$id.'/relationships/nodata', + ], + 'meta' => [ + 'foo' => 'bar', + ], + ], ], 'links' => [ 'self' => 'http://example.com/master/1', diff --git a/tests/Relations/AbstractManyRelationTest.php b/tests/Relations/AbstractManyRelationTest.php index bbb2c0a..b880038 100644 --- a/tests/Relations/AbstractManyRelationTest.php +++ b/tests/Relations/AbstractManyRelationTest.php @@ -78,11 +78,8 @@ public function it_can_sort_the_included() $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Collection $collectionMock */ $collectionMock = $this->getMockBuilder(Collection::class) - ->setMethods(['isNotEmpty', 'sortBy']) + ->onlyMethods(['sortBy']) ->getMock(); - $collectionMock->expects($this->once()) - ->method('isNotEmpty') - ->willReturn(true); $collectionMock->expects($this->once()) ->method('sortBy') ->with('foo', SORT_NATURAL, true); From e251ad7a17b21db6ec14d6a989090a05b66cc968 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 5 Aug 2020 10:39:45 +0200 Subject: [PATCH 2/4] Do not add relations without data to JSON API array If the relation does not have any data, we should not serialize it as having empty data (i.e. null or empty array). --- src/Item.php | 23 ++++++++------- tests/ItemTest.php | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 11 deletions(-) diff --git a/src/Item.php b/src/Item.php index 7b6fcbb..eace119 100644 --- a/src/Item.php +++ b/src/Item.php @@ -130,26 +130,27 @@ public function getRelationships(): array $relationships = []; foreach ($this->getRelations() as $name => $relation) { + if (!$relation->hasIncluded()) { + continue; + } + if ($relation instanceof OneRelationInterface) { - $relationships[$name] = ['data' => null]; + $relationships[$name]['data'] = null; if ($relation->getIncluded() !== null) { - $relationships[$name] = [ - 'data' => [ - 'type' => $relation->getIncluded()->getType(), - 'id' => $relation->getIncluded()->getId(), - ], + $relationships[$name]['data'] = [ + 'type' => $relation->getIncluded()->getType(), + 'id' => $relation->getIncluded()->getId(), ]; } } elseif ($relation instanceof ManyRelationInterface) { $relationships[$name]['data'] = []; foreach ($relation->getIncluded() as $item) { - $relationships[$name]['data'][] = - [ - 'type' => $item->getType(), - 'id' => $item->getId(), - ]; + $relationships[$name]['data'][] = [ + 'type' => $item->getType(), + 'id' => $item->getId(), + ]; } } } diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 7a69859..3c64648 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -119,6 +119,24 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array() ); } + /** + * @test + */ + public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array() + { + $item = new WithRelationshipItem(); + $item->setId('1234'); + $item->hasoneRelation(); + + $this->assertSame( + [ + 'type' => 'item-with-relationship', + 'id' => '1234', + ], + $item->toJsonApiArray() + ); + } + /** * @test */ @@ -170,6 +188,24 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array() ); } + /** + * @test + */ + public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array() + { + $item = new WithRelationshipItem(); + $item->setId('1234'); + $item->hasmanyRelation(); + + $this->assertSame( + [ + 'type' => 'item-with-relationship', + 'id' => '1234', + ], + $item->toJsonApiArray() + ); + } + /** * @test */ @@ -219,6 +255,24 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array() ); } + /** + * @test + */ + public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array() + { + $item = new WithRelationshipItem(); + $item->setId('1234'); + $item->morphtoRelation(); + + $this->assertSame( + [ + 'type' => 'item-with-relationship', + 'id' => '1234', + ], + $item->toJsonApiArray() + ); + } + /** * @test */ @@ -270,6 +324,24 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array() ); } + /** + * @test + */ + public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array() + { + $item = new WithRelationshipItem(); + $item->setId('1234'); + $item->morphtomanyRelation(); + + $this->assertSame( + [ + 'type' => 'item-with-relationship', + 'id' => '1234', + ], + $item->toJsonApiArray() + ); + } + /** * @test */ From c9e68136aa9ff16504767fc13fdc9ab08a87ef12 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 5 Aug 2020 10:41:37 +0200 Subject: [PATCH 3/4] Add relation links and meta to JSON API array --- src/Item.php | 46 ++++++++++++++++++++++++++------------------ tests/ItemTest.php | 48 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 23 deletions(-) diff --git a/src/Item.php b/src/Item.php index eace119..f58a56a 100644 --- a/src/Item.php +++ b/src/Item.php @@ -130,28 +130,36 @@ public function getRelationships(): array $relationships = []; foreach ($this->getRelations() as $name => $relation) { - if (!$relation->hasIncluded()) { - continue; + if ($relation->hasIncluded()) { + if ($relation instanceof OneRelationInterface) { + $relationships[$name]['data'] = null; + + if ($relation->getIncluded() !== null) { + $relationships[$name]['data'] = [ + 'type' => $relation->getIncluded()->getType(), + 'id' => $relation->getIncluded()->getId(), + ]; + } + } elseif ($relation instanceof ManyRelationInterface) { + $relationships[$name]['data'] = []; + + foreach ($relation->getIncluded() as $item) { + $relationships[$name]['data'][] = [ + 'type' => $item->getType(), + 'id' => $item->getId(), + ]; + } + } } - if ($relation instanceof OneRelationInterface) { - $relationships[$name]['data'] = null; + $links = $relation->getLinks(); + if ($links !== null) { + $relationships[$name]['links'] = $links->toArray(); + } - if ($relation->getIncluded() !== null) { - $relationships[$name]['data'] = [ - 'type' => $relation->getIncluded()->getType(), - 'id' => $relation->getIncluded()->getId(), - ]; - } - } elseif ($relation instanceof ManyRelationInterface) { - $relationships[$name]['data'] = []; - - foreach ($relation->getIncluded() as $item) { - $relationships[$name]['data'][] = [ - 'type' => $item->getType(), - 'id' => $item->getId(), - ]; - } + $meta = $relation->getMeta(); + if ($meta !== null) { + $relationships[$name]['meta'] = $meta->toArray(); } } diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 3c64648..dbf5dae 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -78,6 +78,8 @@ public function is_adds_hasone_relation_in_to_json_api_array() $item = new WithRelationshipItem(); $item->setId('1234'); $item->hasoneRelation()->associate((new RelatedItem())->setId('5678')); + $item->hasoneRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); + $item->hasoneRelation()->setMeta(new Meta(['foo' => 'bar'])); $this->assertSame( [ @@ -89,6 +91,14 @@ public function is_adds_hasone_relation_in_to_json_api_array() 'type' => 'related-item', 'id' => '5678', ], + 'links' => [ + 'self' => [ + 'href' => 'http://example.com/articles', + ], + ], + 'meta' => [ + 'foo' => 'bar', + ], ], ], ], @@ -122,7 +132,7 @@ public function is_adds_empty_hasone_relation_in_to_json_api_array() /** * @test */ - public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array() + public function is_does_not_add_hasone_relation_without_data_links_and_meta_in_to_json_api_array() { $item = new WithRelationshipItem(); $item->setId('1234'); @@ -145,6 +155,8 @@ public function is_adds_hasmany_relation_in_to_json_api_array() $item = new WithRelationshipItem(); $item->setId('1234'); $item->hasmanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')])); + $item->hasmanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); + $item->hasmanyRelation()->setMeta(new Meta(['foo' => 'bar'])); $this->assertSame( [ @@ -158,6 +170,14 @@ public function is_adds_hasmany_relation_in_to_json_api_array() 'id' => '5678', ], ], + 'links' => [ + 'self' => [ + 'href' => 'http://example.com/articles', + ], + ], + 'meta' => [ + 'foo' => 'bar', + ], ], ], ], @@ -191,7 +211,7 @@ public function is_adds_empty_hasmany_relation_in_to_json_api_array() /** * @test */ - public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array() + public function is_does_not_add_hasmany_relation_without_data_links_and_meta_in_to_json_api_array() { $item = new WithRelationshipItem(); $item->setId('1234'); @@ -214,6 +234,8 @@ public function is_adds_morphto_relation_in_to_json_api_array() $item = new WithRelationshipItem(); $item->setId('1234'); $item->morphtoRelation()->associate((new RelatedItem())->setId('5678')); + $item->morphtoRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); + $item->morphtoRelation()->setMeta(new Meta(['foo' => 'bar'])); $this->assertSame( [ @@ -225,6 +247,14 @@ public function is_adds_morphto_relation_in_to_json_api_array() 'type' => 'related-item', 'id' => '5678', ], + 'links' => [ + 'self' => [ + 'href' => 'http://example.com/articles', + ], + ], + 'meta' => [ + 'foo' => 'bar', + ], ], ], ], @@ -258,7 +288,7 @@ public function is_adds_empty_morphto_relation_in_to_json_api_array() /** * @test */ - public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array() + public function is_does_not_add_morphto_relation_without_data_links_and_meta_in_to_json_api_array() { $item = new WithRelationshipItem(); $item->setId('1234'); @@ -281,6 +311,8 @@ public function is_adds_morphtomany_relation_in_to_json_api_array() $item = new WithRelationshipItem(); $item->setId('1234'); $item->morphtomanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')])); + $item->morphtomanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); + $item->morphtomanyRelation()->setMeta(new Meta(['foo' => 'bar'])); $this->assertSame( [ @@ -294,6 +326,14 @@ public function is_adds_morphtomany_relation_in_to_json_api_array() 'id' => '5678', ], ], + 'links' => [ + 'self' => [ + 'href' => 'http://example.com/articles', + ], + ], + 'meta' => [ + 'foo' => 'bar', + ], ], ], ], @@ -327,7 +367,7 @@ public function is_adds_empty_morphtomany_relation_in_to_json_api_array() /** * @test */ - public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array() + public function is_does_not_add_morphtomany_relation_without_data_links_and_meta_in_to_json_api_array() { $item = new WithRelationshipItem(); $item->setId('1234'); From 89c59fa6bcc7db4b9fa4feccd89afaf11dea9465 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 5 Aug 2020 12:22:32 +0200 Subject: [PATCH 4/4] Use createMock instead of getMockBuilder --- tests/DocumentClientTest.php | 20 ++++++++++---------- tests/Relations/AbstractManyRelationTest.php | 5 ++--- tests/RepositoryTest.php | 16 ++++++++-------- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/DocumentClientTest.php b/tests/DocumentClientTest.php index 44aa289..1d3f32a 100644 --- a/tests/DocumentClientTest.php +++ b/tests/DocumentClientTest.php @@ -18,7 +18,7 @@ class DocumentClientTest extends AbstractTest public function the_base_url_can_be_changed_after_instantiation() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ - $client = $this->getMockBuilder(ClientInterface::class)->getMock(); + $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('getBaseUri') @@ -29,7 +29,7 @@ public function the_base_url_can_be_changed_after_instantiation() ->with('http://www.test-changed.com'); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser */ - $parser = $this->getMockBuilder(ResponseParserInterface::class)->getMock(); + $parser = $this->createMock(ResponseParserInterface::class); $documentClient = new DocumentClient($client, $parser); @@ -46,7 +46,7 @@ public function it_builds_a_get_request() $document = new Document(); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ - $client = $this->getMockBuilder(ClientInterface::class)->getMock(); + $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('get') @@ -54,7 +54,7 @@ public function it_builds_a_get_request() ->willReturn($response); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser */ - $parser = $this->getMockBuilder(ResponseParserInterface::class)->getMock(); + $parser = $this->createMock(ResponseParserInterface::class); $parser->expects($this->once()) ->method('parse') @@ -77,7 +77,7 @@ public function it_builds_a_delete_request() $document = new Document(); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ - $client = $this->getMockBuilder(ClientInterface::class)->getMock(); + $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('delete') @@ -85,7 +85,7 @@ public function it_builds_a_delete_request() ->willReturn($response); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser */ - $parser = $this->getMockBuilder(ResponseParserInterface::class)->getMock(); + $parser = $this->createMock(ResponseParserInterface::class); $parser->expects($this->once()) ->method('parse') @@ -110,7 +110,7 @@ public function it_builds_a_patch_request() $itemDocument->setData((new Item())->setType('test')->setId('1')); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ - $client = $this->getMockBuilder(ClientInterface::class)->getMock(); + $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('patch') @@ -118,7 +118,7 @@ public function it_builds_a_patch_request() ->willReturn($response); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser */ - $parser = $this->getMockBuilder(ResponseParserInterface::class)->getMock(); + $parser = $this->createMock(ResponseParserInterface::class); $parser->expects($this->once()) ->method('parse') @@ -143,7 +143,7 @@ public function it_builds_a_post_request() $itemDocument->setData((new Item())->setType('test')); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ - $client = $this->getMockBuilder(ClientInterface::class)->getMock(); + $client = $this->createMock(ClientInterface::class); $client->expects($this->once()) ->method('post') @@ -151,7 +151,7 @@ public function it_builds_a_post_request() ->willReturn($response); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ResponseParserInterface $parser */ - $parser = $this->getMockBuilder(ResponseParserInterface::class)->getMock(); + $parser = $this->createMock(ResponseParserInterface::class); $parser->expects($this->once()) ->method('parse') diff --git a/tests/Relations/AbstractManyRelationTest.php b/tests/Relations/AbstractManyRelationTest.php index b880038..415cbe0 100644 --- a/tests/Relations/AbstractManyRelationTest.php +++ b/tests/Relations/AbstractManyRelationTest.php @@ -77,9 +77,8 @@ public function it_can_sort_the_included() /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractManyRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Collection $collectionMock */ - $collectionMock = $this->getMockBuilder(Collection::class) - ->onlyMethods(['sortBy']) - ->getMock(); + $collectionMock = $this->createMock(Collection::class); + $collectionMock->expects($this->once()) ->method('sortBy') ->with('foo', SORT_NATURAL, true); diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index 6f8e649..9b2ca93 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -18,7 +18,7 @@ class RepositoryTest extends TestCase public function it_can_get_the_client() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame($client, $repository->getClient()); @@ -30,7 +30,7 @@ public function it_can_get_the_client() public function it_can_get_the_endpoint() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $repository = new MockRepository($client, new DocumentFactory()); $this->assertSame('mocks', $repository->getEndpoint()); @@ -44,7 +44,7 @@ public function it_can_get_all() $document = new Document(); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('get') @@ -64,7 +64,7 @@ public function it_can_take_one() $document = new Document(); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('get') @@ -84,7 +84,7 @@ public function it_can_find_one() $document = new Document(); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('get') @@ -105,7 +105,7 @@ public function it_can_save_new() $document->setData(new Item()); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('post') @@ -126,7 +126,7 @@ public function it_can_save_existing() $document->setData((new Item())->setId(1)); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('patch') @@ -146,7 +146,7 @@ public function it_can_delete() $document = new Document(); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ - $client = $this->getMockBuilder(DocumentClientInterface::class)->getMock(); + $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('delete')