Skip to content

Commit

Permalink
Added Array and DataArray Serializer tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Sturgeon committed May 27, 2014
1 parent 4f486da commit 2991425
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/Serializer/ArraySerializerTest.php
@@ -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();
}

}
104 changes: 104 additions & 0 deletions test/Serializer/DataArraySerializerTest.php
@@ -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();
}

}

0 comments on commit 2991425

Please sign in to comment.