This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for model relationship transformer
- Loading branch information
Coen Zimmerman
committed
May 28, 2017
1 parent
e1fc5b7
commit 8127468
Showing
1 changed file
with
175 additions
and
0 deletions.
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
tests/Encoder/Transformers/ModelRelationshipTransformerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
<?php | ||
namespace Czim\JsonApi\Test\Encoder\Transformers; | ||
|
||
use Czim\JsonApi\Contracts\Encoder\EncoderInterface; | ||
use Czim\JsonApi\Contracts\Resource\EloquentResourceInterface; | ||
use Czim\JsonApi\Encoder\Transformers\ModelRelationshipTransformer; | ||
use Czim\JsonApi\Support\Resource\RelationshipTransformData; | ||
use Czim\JsonApi\Test\TestCase; | ||
use Mockery; | ||
|
||
/** | ||
* Class ModelRelationshipTransformerTest | ||
* | ||
* The happy path with sideloaded relation data is tested by integration tests. | ||
* | ||
* @group encoding | ||
*/ | ||
class ModelRelationshipTransformerTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_relation_data_for_a_model_resource_without_references() | ||
{ | ||
/** @var Mockery\Mock|EloquentResourceInterface $resource */ | ||
$resource = Mockery::mock(EloquentResourceInterface::class); | ||
$resource->shouldReceive('availableIncludes')->andReturn(['test']); | ||
$resource->shouldReceive('url')->andReturn('http://test.url'); | ||
$resource->shouldReceive('id')->andReturn('1'); | ||
$resource->shouldReceive('type')->andReturn('tests'); | ||
|
||
$data = new RelationshipTransformData([ | ||
'resource' => $resource, | ||
'include' => 'test', | ||
'references' => false, | ||
'sideload' => false, | ||
]); | ||
|
||
$transformer = new ModelRelationshipTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
static::assertEquals( | ||
[ | ||
'links' => [ | ||
'self' => 'http://test.url/1/relationships/test', | ||
'related' => 'http://test.url/1/related/test', | ||
] | ||
], | ||
$transformer->transform($data) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_relation_data_for_a_model_resource_without_links_if_configured() | ||
{ | ||
/** @var Mockery\Mock|EloquentResourceInterface $resource */ | ||
$resource = Mockery::mock(EloquentResourceInterface::class); | ||
$resource->shouldReceive('availableIncludes')->andReturn(['test']); | ||
|
||
$this->app['config']->set('jsonapi.transform.links.relationships', false); | ||
$this->app['config']->set('jsonapi.transform.links.related', false); | ||
|
||
$data = new RelationshipTransformData([ | ||
'resource' => $resource, | ||
'include' => 'test', | ||
'references' => false, | ||
'sideload' => false, | ||
]); | ||
|
||
$transformer = new ModelRelationshipTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
static::assertEquals([], $transformer->transform($data)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_relation_data_for_a_model_resource_with_references() | ||
{ | ||
/** @var Mockery\Mock|EloquentResourceInterface $resource */ | ||
$resource = Mockery::mock(EloquentResourceInterface::class); | ||
$resource->shouldReceive('availableIncludes')->andReturn(['test']); | ||
$resource->shouldReceive('url')->andReturn('http://test.url'); | ||
$resource->shouldReceive('id')->andReturn('1'); | ||
$resource->shouldReceive('type')->andReturn('tests'); | ||
$resource->shouldReceive('relationshipReferences')->andReturn(['type' => 'tests', 'id' => '2']); | ||
|
||
$data = new RelationshipTransformData([ | ||
'resource' => $resource, | ||
'include' => 'test', | ||
'references' => true, | ||
'sideload' => false, | ||
]); | ||
|
||
$transformer = new ModelRelationshipTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
static::assertEquals( | ||
[ | ||
'links' => [ | ||
'self' => 'http://test.url/1/relationships/test', | ||
'related' => 'http://test.url/1/related/test', | ||
], | ||
'data' => [ | ||
'type' => 'tests', | ||
'id' => '2', | ||
], | ||
], | ||
$transformer->transform($data) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Czim\JsonApi\Exceptions\EncodingException | ||
*/ | ||
function it_throws_an_exception_if_data_does_not_indicate_a_valid_include() | ||
{ | ||
/** @var Mockery\Mock|EloquentResourceInterface $resource */ | ||
$resource = Mockery::mock(EloquentResourceInterface::class); | ||
$resource->shouldReceive('availableIncludes')->andReturn([]); | ||
$resource->shouldReceive('url')->andReturn('http://test.url'); | ||
$resource->shouldReceive('id')->andReturn('1'); | ||
$resource->shouldReceive('type')->andReturn('tests'); | ||
|
||
$data = new RelationshipTransformData([ | ||
'resource' => $resource, | ||
'include' => 'test', | ||
'references' => false, | ||
'sideload' => true, | ||
]); | ||
|
||
$transformer = new ModelRelationshipTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
$transformer->transform($data); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
function it_throws_an_exception_if_data_is_not_a_relationship_data_instance() | ||
{ | ||
$transformer = new ModelRelationshipTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
$transformer->transform($this); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \Czim\JsonApi\Exceptions\EncodingException | ||
*/ | ||
function it_throws_an_exception_if_no_resource_is_set_in_data() | ||
{ | ||
$transformer = new ModelRelationshipTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
$transformer->transform(new RelationshipTransformData([])); | ||
} | ||
|
||
/** | ||
* @return EncoderInterface|Mockery\MockInterface | ||
*/ | ||
protected function getMockEncoder() | ||
{ | ||
return Mockery::mock(EncoderInterface::class); | ||
} | ||
|
||
} |