Skip to content

Commit

Permalink
Merge 23366da into e03925f
Browse files Browse the repository at this point in the history
  • Loading branch information
milroyfraser committed Jul 9, 2019
2 parents e03925f + 23366da commit 2b50980
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 148 deletions.
22 changes: 22 additions & 0 deletions test/Dto/Book.php
@@ -0,0 +1,22 @@
<?php namespace League\Fractal\Test\Dto;

class Book
{
public $title;
public $year;
public $author;
public $meta;

public function __construct($title, $year, Person $author = null, array $meta = null)
{
$this->title = $title;
$this->year = $year;
$this->author = $author;
$this->meta = $meta;
}

public static function make($title, $year, Person $author = null, array $meta = null)
{
return new self($title, $year, $author, $meta);
}
}
18 changes: 18 additions & 0 deletions test/Dto/Person.php
@@ -0,0 +1,18 @@
<?php namespace League\Fractal\Test\Dto;

class Person
{
public $name;
public $meta;

public function __construct($name, array $meta = null)
{
$this->name = $name;
$this->meta = $meta;
}

public static function make($name, array $meta = null)
{
return new self($name, $meta);
}
}
2 changes: 1 addition & 1 deletion test/ScopeTest.php
Expand Up @@ -503,7 +503,7 @@ public function testDefaultIncludeSuccess()
$expected = [
'a' => 'b',
'author' => [
'c' => 'd',
'name' => 'Robert Cecil Martin',
],
];

Expand Down
131 changes: 63 additions & 68 deletions test/Serializer/ArraySerializerTest.php
Expand Up @@ -6,77 +6,72 @@
use League\Fractal\Resource\NullResource;
use League\Fractal\Scope;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Test\Dto\Person;
use League\Fractal\Test\Dto\Book;
use League\Fractal\Test\Stub\Transformer\GenericBookTransformer;
use Mockery;
use PHPUnit\Framework\TestCase;

class ArraySerializerTest extends TestCase
{
private $bookItemInput = [
'title' => 'Foo',
'year' => '1991',
'_author' => [
'name' => 'Dave',
],
];

private $bookCollectionInput = [
[
'title' => 'Foo',
'year' => '1991',
'_author' => [
'name' => 'Dave',
],
],
[
'title' => 'Bar',
'year' => '1997',
'_author' => [
'name' => 'Bob',
],
],
];
private function bookItemInput()
{
$author = Person::make('Miguel de Cervantes');

return Book::make('Don Quixote', '1605', $author);
}

private function bookCollectionInput()
{
$phil = Person::make('Miguel de Cervantes');
$taylor = Person::make('J. K. Rowling');

return [
Book::make('Don Quixote', '1605', $phil),
Book::make('Harry Potter', '1997', $taylor)
];
}

public function testSerializingItemResource()
{
$manager = new Manager();
$manager->parseIncludes('author');
$manager->setSerializer(new ArraySerializer());

$resource = new Item($this->bookItemInput, new GenericBookTransformer(), 'book');
$resource = new Item($this->bookItemInput(), new GenericBookTransformer(), 'book');

// Try without metadata
$scope = new Scope($manager, $resource);

$expected = [
'title' => 'Foo',
'year' => 1991,
'title' => 'Don Quixote',
'year' => 1605,
'author' => [
'name' => 'Dave',
'name' => 'Miguel de Cervantes',
],
];

$this->assertSame($expected, $scope->toArray());

//Test single field
$manager->parseFieldsets(['book' => 'title']);
$expected = ['title' => 'Foo'];
$expected = ['title' => 'Don Quixote'];
$this->assertSame($expected, $scope->toArray());

//Test multiple field
$manager->parseFieldsets(['book' => 'title,year']);
$expected = [
'title' => 'Foo',
'year' => 1991
'title' => 'Don Quixote',
'year' => 1605
];
$this->assertSame($expected, $scope->toArray());

//Test with relationship field
$manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']);
$expected = [
'title' => 'Foo',
'title' => 'Don Quixote',
'author' => [
'name' => 'Dave'
'name' => 'Miguel de Cervantes'
],
];
$this->assertSame($expected, $scope->toArray());
Expand All @@ -89,10 +84,10 @@ public function testSerializingItemResource()
$scope = new Scope($manager, $resource);

$expected = [
'title' => 'Foo',
'year' => 1991,
'title' => 'Don Quixote',
'year' => 1605,
'author' => [
'name' => 'Dave'
'name' => 'Miguel de Cervantes'
],
'meta' => [
'foo' => 'bar'
Expand All @@ -104,9 +99,9 @@ public function testSerializingItemResource()
//Test with relationship field
$manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']);
$expected = [
'title' => 'Foo',
'title' => 'Don Quixote',
'author' => [
'name' => 'Dave',
'name' => 'Miguel de Cervantes',
],
'meta' => [
'foo' => 'bar',
Expand All @@ -121,25 +116,25 @@ public function testSerializingCollectionResource()
$manager->parseIncludes('author');
$manager->setSerializer(new ArraySerializer());

$resource = new Collection($this->bookCollectionInput, new GenericBookTransformer(), 'books');
$resource = new Collection($this->bookCollectionInput(), new GenericBookTransformer(), 'books');

// Try without metadata
$scope = new Scope($manager, $resource);

$expected = [
'books' => [
[
'title' => 'Foo',
'year' => 1991,
'title' => 'Don Quixote',
'year' => 1605,
'author' => [
'name' => 'Dave',
'name' => 'Miguel de Cervantes',
],
],
[
'title' => 'Bar',
'title' => 'Harry Potter',
'year' => 1997,
'author' => [
'name' => 'Bob',
'name' => 'J. K. Rowling',
],
],
],
Expand All @@ -148,15 +143,15 @@ public function testSerializingCollectionResource()
$this->assertSame($expected, $scope->toArray());

// JSON array of JSON objects
$expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}';
$expectedJson = '{"books":[{"title":"Don Quixote","year":1605,"author":{"name":"Miguel de Cervantes"}},{"title":"Harry Potter","year":1997,"author":{"name":"J. K. Rowling"}}]}';
$this->assertSame($expectedJson, $scope->toJson());

//Test single field
$manager->parseFieldsets(['books' => 'title']);
$expected = [
'books' => [
['title' => 'Foo'],
['title' => 'Bar']
['title' => 'Don Quixote'],
['title' => 'Harry Potter']
]
];
$this->assertSame($expected, $scope->toArray());
Expand All @@ -166,11 +161,11 @@ public function testSerializingCollectionResource()
$expected = [
'books' => [
[
'title' => 'Foo',
'year' => 1991
'title' => 'Don Quixote',
'year' => 1605
],
[
'title' => 'Bar',
'title' => 'Harry Potter',
'year' => 1997
]
]
Expand All @@ -182,15 +177,15 @@ public function testSerializingCollectionResource()
$expected = [
'books' => [
[
'title' => 'Foo',
'title' => 'Don Quixote',
'author' => [
'name' => 'Dave'
'name' => 'Miguel de Cervantes'
]
],
[
'title' => 'Bar',
'title' => 'Harry Potter',
'author' => [
'name' => 'Bob'
'name' => 'J. K. Rowling'
]
]
]
Expand All @@ -208,17 +203,17 @@ public function testSerializingCollectionResource()
$expected = [
'books' => [
[
'title' => 'Foo',
'year' => 1991,
'title' => 'Don Quixote',
'year' => 1605,
'author' => [
'name' => 'Dave',
'name' => 'Miguel de Cervantes',
],
],
[
'title' => 'Bar',
'title' => 'Harry Potter',
'year' => 1997,
'author' => [
'name' => 'Bob',
'name' => 'J. K. Rowling',
],
],
],
Expand All @@ -229,22 +224,22 @@ public function testSerializingCollectionResource()

$this->assertSame($expected, $scope->toArray());

$expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}';
$expectedJson = '{"books":[{"title":"Don Quixote","year":1605,"author":{"name":"Miguel de Cervantes"}},{"title":"Harry Potter","year":1997,"author":{"name":"J. K. Rowling"}}],"meta":{"foo":"bar"}}';
$this->assertSame($expectedJson, $scope->toJson());

$manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']);
$expected = [
'books' => [
[
'title' => 'Foo',
'title' => 'Don Quixote',
'author' => [
'name' => 'Dave'
'name' => 'Miguel de Cervantes'
]
],
[
'title' => 'Bar',
'title' => 'Harry Potter',
'author' => [
'name' => 'Bob'
'name' => 'J. K. Rowling'
]
]
],
Expand All @@ -261,7 +256,7 @@ public function testSerializingNullResource()
$manager->parseIncludes('author');
$manager->setSerializer(new ArraySerializer());

$resource = new NullResource($this->bookCollectionInput, new GenericBookTransformer(), 'books');
$resource = new NullResource($this->bookCollectionInput(), new GenericBookTransformer(), 'books');

// Try without metadata
$scope = new Scope($manager, $resource);
Expand Down Expand Up @@ -313,21 +308,21 @@ public function testSerializingCollectionResourceWithoutName()
$manager->parseIncludes('author');
$manager->setSerializer(new ArraySerializer());

$resource = new Collection($this->bookCollectionInput, new GenericBookTransformer());
$resource = new Collection($this->bookCollectionInput(), new GenericBookTransformer());

// Try without metadata
$scope = new Scope($manager, $resource);

// JSON array of JSON objects
$expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}';
$expectedJson = '{"data":[{"title":"Don Quixote","year":1605,"author":{"name":"Miguel de Cervantes"}},{"title":"Harry Potter","year":1997,"author":{"name":"J. K. Rowling"}}]}';
$this->assertSame($expectedJson, $scope->toJson());

// Same again with metadata
$resource->setMetaValue('foo', 'bar');

$scope = new Scope($manager, $resource);

$expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}';
$expectedJson = '{"data":[{"title":"Don Quixote","year":1605,"author":{"name":"Miguel de Cervantes"}},{"title":"Harry Potter","year":1997,"author":{"name":"J. K. Rowling"}}],"meta":{"foo":"bar"}}';
$this->assertSame($expectedJson, $scope->toJson());
}

Expand Down

0 comments on commit 2b50980

Please sign in to comment.