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 collection transformers
- Loading branch information
Coen Zimmerman
committed
Apr 15, 2017
1 parent
573d6c7
commit c9c4257
Showing
4 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
tests/Encoder/Transformers/ModelCollectionTransformerTest.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,190 @@ | ||
<?php | ||
namespace Czim\JsonApi\Test\Encoder\Transformers; | ||
|
||
use Czim\JsonApi\Contracts\Encoder\EncoderInterface; | ||
use Czim\JsonApi\Encoder\Transformers\ModelCollectionTransformer; | ||
use Czim\JsonApi\Test\Helpers\Models\TestAlternativeModel; | ||
use Czim\JsonApi\Test\Helpers\Models\TestSimpleModel; | ||
use Czim\JsonApi\Test\Helpers\Resources\TestAlternativeModelResource; | ||
use Czim\JsonApi\Test\Helpers\Resources\TestSimpleModelResource; | ||
use Czim\JsonApi\Test\TestCase; | ||
use Illuminate\Support\Collection; | ||
use Mockery; | ||
|
||
/** | ||
* Class ModelCollectionTransformerTest | ||
* | ||
* @group encoding | ||
*/ | ||
class ModelCollectionTransformerTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_a_collection_of_simple_models_using_a_resource() | ||
{ | ||
// Prepare the collection | ||
$collection = new Collection; | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 13; | ||
$model->unique_field = 'test123'; | ||
$model->second_field = 'test'; | ||
$model->name = 'Testing!'; | ||
$model->active = false; | ||
|
||
$collection->push($model); | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 14; | ||
$model->unique_field = 'test124'; | ||
$model->second_field = 'test2'; | ||
$model->name = 'Testing?'; | ||
$model->active = true; | ||
|
||
$collection->push($model); | ||
|
||
// Prepare dependencies & mocks | ||
$encoderMock = $this->getMockEncoder(); | ||
$encoderMock->shouldReceive('getResourceForModel')->with(Mockery::type(TestSimpleModel::class)) | ||
->andReturn(new TestSimpleModelResource); | ||
|
||
$transformer = new ModelCollectionTransformer; | ||
$transformer->setEncoder($encoderMock); | ||
$transformer->setIsVariable(false); | ||
|
||
static::assertEquals( | ||
[ | ||
'data' => [ | ||
[ | ||
'id' => '13', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test123', | ||
'second-field' => 'test', | ||
'name' => 'Testing!', | ||
'active' => false, | ||
], | ||
], | ||
[ | ||
'id' => '14', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test124', | ||
'second-field' => 'test2', | ||
'name' => 'Testing?', | ||
'active' => true, | ||
], | ||
], | ||
], | ||
], | ||
$transformer->transform($collection) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_an_empty_collection() | ||
{ | ||
// Prepare the collection | ||
$collection = new Collection; | ||
|
||
// Prepare dependencies & mocks | ||
$encoderMock = $this->getMockEncoder(); | ||
|
||
$transformer = new ModelCollectionTransformer; | ||
$transformer->setEncoder($encoderMock); | ||
|
||
static::assertEquals( | ||
[ | ||
'data' => [], | ||
], | ||
$transformer->transform($collection) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_a_collection_of_varying_models_using_their_relevant_resources() | ||
{ | ||
// Prepare the collection | ||
$collection = new Collection; | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 13; | ||
$model->unique_field = 'test123'; | ||
$model->second_field = 'test'; | ||
$model->name = 'Testing!'; | ||
$model->active = false; | ||
|
||
$collection->push($model); | ||
|
||
$model = new TestAlternativeModel; | ||
$model->id = 8; | ||
$model->slug = 'test-slug'; | ||
$model->value = 3.5; | ||
|
||
$collection->push($model); | ||
|
||
// Prepare dependencies & mocks | ||
$encoderMock = $this->getMockEncoder(); | ||
$encoderMock->shouldReceive('getResourceForModel')->with(Mockery::type(TestSimpleModel::class)) | ||
->andReturn(new TestSimpleModelResource); | ||
$encoderMock->shouldReceive('getResourceForModel')->with(Mockery::type(TestAlternativeModel::class)) | ||
->andReturn(new TestAlternativeModelResource); | ||
|
||
$transformer = new ModelCollectionTransformer; | ||
$transformer->setEncoder($encoderMock); | ||
$transformer->setIsVariable(true); | ||
|
||
static::assertEquals( | ||
[ | ||
'data' => [ | ||
[ | ||
'id' => '13', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test123', | ||
'second-field' => 'test', | ||
'name' => 'Testing!', | ||
'active' => false, | ||
], | ||
], | ||
[ | ||
'id' => '8', | ||
'type' => 'test-alternative-models', | ||
'attributes' => [ | ||
'slug' => 'test-slug', | ||
'value' => 3.5, | ||
], | ||
], | ||
], | ||
], | ||
$transformer->transform($collection) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
function it_throws_an_exception_if_data_is_not_a_model_collection() | ||
{ | ||
$transformer = new ModelCollectionTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
$transformer->transform($this); | ||
} | ||
|
||
/** | ||
* @return EncoderInterface|Mockery\MockInterface | ||
*/ | ||
protected function getMockEncoder() | ||
{ | ||
return Mockery::mock(EncoderInterface::class); | ||
} | ||
|
||
} |
198 changes: 198 additions & 0 deletions
198
tests/Encoder/Transformers/PaginatedModelsTransformerTest.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,198 @@ | ||
<?php | ||
namespace Czim\JsonApi\Test\Encoder\Transformers; | ||
|
||
use Czim\JsonApi\Contracts\Encoder\EncoderInterface; | ||
use Czim\JsonApi\Encoder\Transformers\PaginatedModelsTransformer; | ||
use Czim\JsonApi\Test\Helpers\Models\TestSimpleModel; | ||
use Czim\JsonApi\Test\Helpers\Resources\TestSimpleModelResource; | ||
use Czim\JsonApi\Test\TestCase; | ||
use Illuminate\Pagination\LengthAwarePaginator; | ||
use Illuminate\Support\Collection; | ||
use Mockery; | ||
|
||
/** | ||
* Class PaginatedModelsTransformerTest | ||
* | ||
* @group encoding | ||
*/ | ||
class PaginatedModelsTransformerTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_transforms_a_paginated_collection_of_simple_models() | ||
{ | ||
// Prepare the collection | ||
$collection = new Collection; | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 13; | ||
$model->unique_field = 'test123'; | ||
$model->second_field = 'test'; | ||
$model->name = 'Testing!'; | ||
$model->active = false; | ||
|
||
$collection->push($model); | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 14; | ||
$model->unique_field = 'test124'; | ||
$model->second_field = 'test2'; | ||
$model->name = 'Testing?'; | ||
$model->active = true; | ||
|
||
$collection->push($model); | ||
|
||
$paginator = new LengthAwarePaginator($collection, 8, 2, 2); | ||
|
||
// Prepare dependencies & mocks | ||
$encoderMock = $this->getMockEncoder(); | ||
$encoderMock->shouldReceive('getTopResourceUrl')->andReturn('http://localhost/models'); | ||
$encoderMock->shouldReceive('getResourceForModel')->with(Mockery::type(TestSimpleModel::class)) | ||
->andReturn(new TestSimpleModelResource); | ||
|
||
$encoderMock->shouldReceive('setLink')->with('first', 'http://localhost/models?page[number]=1') | ||
->once()->andReturnSelf(); | ||
$encoderMock->shouldReceive('setLink')->with('next', 'http://localhost/models?page[number]=3') | ||
->once()->andReturnSelf(); | ||
$encoderMock->shouldReceive('setLink')->with('prev', 'http://localhost/models?page[number]=1') | ||
->once()->andReturnSelf(); | ||
$encoderMock->shouldReceive('setLink')->with('last', 'http://localhost/models?page[number]=4') | ||
->once()->andReturnSelf(); | ||
|
||
|
||
$transformer = new PaginatedModelsTransformer; | ||
$transformer->setEncoder($encoderMock); | ||
$transformer->setIsVariable(false); | ||
|
||
static::assertEquals( | ||
[ | ||
'data' => [ | ||
[ | ||
'id' => '13', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test123', | ||
'second-field' => 'test', | ||
'name' => 'Testing!', | ||
'active' => false, | ||
], | ||
], | ||
[ | ||
'id' => '14', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test124', | ||
'second-field' => 'test2', | ||
'name' => 'Testing?', | ||
'active' => true, | ||
], | ||
], | ||
], | ||
], | ||
$transformer->transform($paginator) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
function it_defaults_to_using_paginator_defined_links_if_resource_url_is_not_defined_in_encoder() | ||
{ | ||
// Prepare the collection | ||
$collection = new Collection; | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 13; | ||
$model->unique_field = 'test123'; | ||
$model->second_field = 'test'; | ||
$model->name = 'Testing!'; | ||
$model->active = false; | ||
|
||
$collection->push($model); | ||
|
||
$model = new TestSimpleModel; | ||
$model->id = 14; | ||
$model->unique_field = 'test124'; | ||
$model->second_field = 'test2'; | ||
$model->name = 'Testing?'; | ||
$model->active = true; | ||
|
||
$collection->push($model); | ||
|
||
$paginator = new LengthAwarePaginator($collection, 8, 2, 2, [ | ||
'path' => '/testing/path', | ||
'pageName' => 'pg_test', | ||
]); | ||
|
||
// Prepare dependencies & mocks | ||
$encoderMock = $this->getMockEncoder(); | ||
$encoderMock->shouldReceive('getTopResourceUrl')->andReturn(null); | ||
$encoderMock->shouldReceive('getResourceForModel')->with(Mockery::type(TestSimpleModel::class)) | ||
->andReturn(new TestSimpleModelResource); | ||
|
||
$encoderMock->shouldReceive('setLink')->with('first', '/testing/path?pg_test=1') | ||
->once()->andReturnSelf(); | ||
$encoderMock->shouldReceive('setLink')->with('next', '/testing/path?pg_test=3') | ||
->once()->andReturnSelf(); | ||
$encoderMock->shouldReceive('setLink')->with('prev', '/testing/path?pg_test=1') | ||
->once()->andReturnSelf(); | ||
$encoderMock->shouldReceive('setLink')->with('last', '/testing/path?pg_test=4') | ||
->once()->andReturnSelf(); | ||
|
||
|
||
$transformer = new PaginatedModelsTransformer; | ||
$transformer->setEncoder($encoderMock); | ||
$transformer->setIsVariable(false); | ||
|
||
static::assertEquals( | ||
[ | ||
'data' => [ | ||
[ | ||
'id' => '13', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test123', | ||
'second-field' => 'test', | ||
'name' => 'Testing!', | ||
'active' => false, | ||
], | ||
], | ||
[ | ||
'id' => '14', | ||
'type' => 'test-simple-models', | ||
'attributes' => [ | ||
'unique-field' => 'test124', | ||
'second-field' => 'test2', | ||
'name' => 'Testing?', | ||
'active' => true, | ||
], | ||
], | ||
], | ||
], | ||
$transformer->transform($paginator) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* @expectedException \InvalidArgumentException | ||
*/ | ||
function it_throws_an_exception_if_data_is_not_a_paginator() | ||
{ | ||
$transformer = new PaginatedModelsTransformer; | ||
$transformer->setEncoder($this->getMockEncoder()); | ||
|
||
$transformer->transform($this); | ||
} | ||
|
||
/** | ||
* @return EncoderInterface|Mockery\MockInterface | ||
*/ | ||
protected function getMockEncoder() | ||
{ | ||
return Mockery::mock(EncoderInterface::class); | ||
} | ||
|
||
} |
Oops, something went wrong.