Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Array and DataArray Serializer tests.
- Loading branch information
Phil Sturgeon
committed
May 27, 2014
1 parent
4f486da
commit 2991425
Showing
2 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
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,94 @@ | ||
<?php | ||
|
||
use League\Fractal\Serializer\ArraySerializer; | ||
use League\Fractal\Resource\Collection; | ||
use League\Fractal\Resource\Item; | ||
use League\Fractal\Manager; | ||
use League\Fractal\Scope; | ||
use League\Fractal\Test\Stub\Transformer\GenericBookTransformer; | ||
|
||
class ArraySerializerTest extends PHPUnit_Framework_TestCase { | ||
|
||
public function testSerializingItemResource() | ||
{ | ||
$manager = new Manager(); | ||
$manager->parseIncludes('author'); | ||
$manager->setSerializer(new ArraySerializer()); | ||
|
||
$bookData = array( | ||
'title' => 'Foo', | ||
'year' => '1991', | ||
'_author' => array( | ||
'name' => 'Dave', | ||
), | ||
); | ||
|
||
$resource = new Item($bookData, new GenericBookTransformer(), 'book'); | ||
$scope = new Scope($manager, $resource); | ||
|
||
$expected = array( | ||
'title' => 'Foo', | ||
'year' => 1991, | ||
'author' => array( | ||
'name' => 'Dave', | ||
), | ||
'includes' => array('author'), | ||
); | ||
|
||
$this->assertEquals($expected, $scope->toArray()); | ||
} | ||
|
||
public function testSerializingCollectionResource() | ||
{ | ||
$manager = new Manager(); | ||
$manager->parseIncludes('author'); | ||
$manager->setSerializer(new ArraySerializer()); | ||
|
||
$booksData = array( | ||
array( | ||
'title' => 'Foo', | ||
'year' => '1991', | ||
'_author' => array( | ||
'name' => 'Dave', | ||
), | ||
), | ||
array( | ||
'title' => 'Bar', | ||
'year' => '1997', | ||
'_author' => array( | ||
'name' => 'Bob', | ||
), | ||
), | ||
); | ||
|
||
$resource = new Collection($booksData, new GenericBookTransformer(), 'book'); | ||
$scope = new Scope($manager, $resource); | ||
|
||
$expected = array( | ||
array( | ||
'title' => 'Foo', | ||
'year' => 1991, | ||
'author' => array( | ||
'name' => 'Dave', | ||
), | ||
), | ||
array( | ||
'title' => 'Bar', | ||
'year' => 1997, | ||
'author' => array( | ||
'name' => 'Bob', | ||
), | ||
), | ||
'includes' => array('author'), | ||
); | ||
|
||
$this->assertEquals($expected, $scope->toArray()); | ||
} | ||
|
||
|
||
public function tearDown() | ||
{ | ||
Mockery::close(); | ||
} | ||
|
||
} |
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,104 @@ | ||
<?php | ||
|
||
use League\Fractal\Serializer\DataArraySerializer; | ||
use League\Fractal\Resource\Collection; | ||
use League\Fractal\Resource\Item; | ||
use League\Fractal\Manager; | ||
use League\Fractal\Scope; | ||
use League\Fractal\Test\Stub\Transformer\GenericBookTransformer; | ||
|
||
class DataArraySerializerTest extends PHPUnit_Framework_TestCase { | ||
|
||
public function testSerializingItemResource() | ||
{ | ||
$manager = new Manager(); | ||
$manager->parseIncludes('author'); | ||
$manager->setSerializer(new DataArraySerializer()); | ||
|
||
$bookData = array( | ||
'title' => 'Foo', | ||
'year' => '1991', | ||
'_author' => array( | ||
'name' => 'Dave', | ||
), | ||
); | ||
|
||
$resource = new Item($bookData, new GenericBookTransformer(), 'book'); | ||
$scope = new Scope($manager, $resource); | ||
|
||
$expected = array( | ||
'data' => array( | ||
'title' => 'Foo', | ||
'year' => 1991, | ||
'author' => array( | ||
'data' => array( | ||
'name' => 'Dave', | ||
), | ||
), | ||
), | ||
'includes' => array('author'), | ||
); | ||
|
||
$this->assertEquals($expected, $scope->toArray()); | ||
} | ||
|
||
public function testSerializingCollectionResource() | ||
{ | ||
$manager = new Manager(); | ||
$manager->parseIncludes('author'); | ||
$manager->setSerializer(new DataArraySerializer()); | ||
|
||
$booksData = array( | ||
array( | ||
'title' => 'Foo', | ||
'year' => '1991', | ||
'_author' => array( | ||
'name' => 'Dave', | ||
), | ||
), | ||
array( | ||
'title' => 'Bar', | ||
'year' => '1997', | ||
'_author' => array( | ||
'name' => 'Bob', | ||
), | ||
), | ||
); | ||
|
||
$resource = new Collection($booksData, new GenericBookTransformer(), 'book'); | ||
$scope = new Scope($manager, $resource); | ||
|
||
$expected = array( | ||
'data' => array( | ||
array( | ||
'title' => 'Foo', | ||
'year' => 1991, | ||
'author' => array( | ||
'data' => array( | ||
'name' => 'Dave', | ||
), | ||
), | ||
), | ||
array( | ||
'title' => 'Bar', | ||
'year' => 1997, | ||
'author' => array( | ||
'data' => array( | ||
'name' => 'Bob', | ||
), | ||
), | ||
), | ||
), | ||
'includes' => array('author'), | ||
); | ||
|
||
$this->assertEquals($expected, $scope->toArray()); | ||
} | ||
|
||
|
||
public function tearDown() | ||
{ | ||
Mockery::close(); | ||
} | ||
|
||
} |