diff --git a/test/Dto/Book.php b/test/Dto/Book.php new file mode 100644 index 00000000..ae18d3a3 --- /dev/null +++ b/test/Dto/Book.php @@ -0,0 +1,22 @@ +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); + } +} diff --git a/test/Dto/Person.php b/test/Dto/Person.php new file mode 100644 index 00000000..b06dddee --- /dev/null +++ b/test/Dto/Person.php @@ -0,0 +1,18 @@ +name = $name; + $this->meta = $meta; + } + + public static function make($name, array $meta = null) + { + return new self($name, $meta); + } +} diff --git a/test/ScopeTest.php b/test/ScopeTest.php index 3a3cd227..35151de1 100755 --- a/test/ScopeTest.php +++ b/test/ScopeTest.php @@ -503,7 +503,7 @@ public function testDefaultIncludeSuccess() $expected = [ 'a' => 'b', 'author' => [ - 'c' => 'd', + 'name' => 'Robert Cecil Martin', ], ]; diff --git a/test/Serializer/ArraySerializerTest.php b/test/Serializer/ArraySerializerTest.php index 72d8994b..b6629504 100644 --- a/test/Serializer/ArraySerializerTest.php +++ b/test/Serializer/ArraySerializerTest.php @@ -6,36 +6,31 @@ 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() { @@ -43,16 +38,16 @@ public function testSerializingItemResource() $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', ], ]; @@ -60,23 +55,23 @@ public function testSerializingItemResource() //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()); @@ -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' @@ -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', @@ -121,7 +116,7 @@ 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); @@ -129,17 +124,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', ], ], ], @@ -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()); @@ -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 ] ] @@ -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' ] ] ] @@ -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', ], ], ], @@ -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' ] ] ], @@ -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); @@ -313,13 +308,13 @@ 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 @@ -327,7 +322,7 @@ public function testSerializingCollectionResourceWithoutName() $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()); } diff --git a/test/Serializer/DataArraySerializerTest.php b/test/Serializer/DataArraySerializerTest.php index 948aa40f..7037671a 100644 --- a/test/Serializer/DataArraySerializerTest.php +++ b/test/Serializer/DataArraySerializerTest.php @@ -6,6 +6,8 @@ use League\Fractal\Resource\NullResource; use League\Fractal\Scope; use League\Fractal\Serializer\DataArraySerializer; +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; @@ -18,13 +20,8 @@ public function testSerializingItemResource() $manager->parseIncludes('author'); $manager->setSerializer(new DataArraySerializer()); - $bookData = [ - 'title' => 'Foo', - 'year' => '1991', - '_author' => [ - 'name' => 'Dave', - ], - ]; + $phil = Person::make('Miguel de Cervantes'); + $bookData = Book::make('Don Quixote', '1605', $phil); // Try without metadata $resource = new Item($bookData, new GenericBookTransformer(), 'book'); @@ -32,11 +29,11 @@ public function testSerializingItemResource() $expected = [ 'data' => [ - 'title' => 'Foo', - 'year' => 1991, + 'title' => 'Don Quixote', + 'year' => 1605, 'author' => [ 'data' => [ - 'name' => 'Dave', + 'name' => 'Miguel de Cervantes', ], ], ], @@ -48,7 +45,7 @@ public function testSerializingItemResource() $manager->parseFieldsets(['book' => 'title']); $expected = [ 'data' => [ - 'title' => 'Foo', + 'title' => 'Don Quixote', ] ]; $this->assertSame($expected, $scope->toArray()); @@ -57,8 +54,8 @@ public function testSerializingItemResource() $manager->parseFieldsets(['book' => 'title,year']); $expected = [ 'data' => [ - 'title' => 'Foo', - 'year' => 1991 + 'title' => 'Don Quixote', + 'year' => 1605 ] ]; $this->assertSame($expected, $scope->toArray()); @@ -67,10 +64,10 @@ public function testSerializingItemResource() $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); $expected = [ 'data' => [ - 'title' => 'Foo', + 'title' => 'Don Quixote', 'author' => [ 'data' => [ - 'name' => 'Dave' + 'name' => 'Miguel de Cervantes' ] ] ] @@ -88,12 +85,11 @@ public function testSerializingItemResource() $expected = [ 'data' => [ - 'title' => 'Foo', - 'year' => 1991, + 'title' => 'Don Quixote', + 'year' => 1605, 'author' => [ 'data' => [ - 'name' => 'Dave', - + 'name' => 'Miguel de Cervantes', ], ], ], @@ -108,11 +104,10 @@ public function testSerializingItemResource() $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']); $expected = [ 'data' => [ - 'title' => 'Foo', + 'title' => 'Don Quixote', 'author' => [ 'data' => [ - 'name' => 'Dave' - + 'name' => 'Miguel de Cervantes', ] ] ], @@ -129,21 +124,15 @@ public function testSerializingCollectionResource() $manager->parseIncludes('author'); $manager->setSerializer(new DataArraySerializer()); + $phil = Person::make('Miguel de Cervantes'); + $bookFoo = Book::make('Don Quixote', '1605', $phil); + + $taylor = Person::make('J. K. Rowling'); + $bookBar = Book::make('Harry Potter', '1997', $taylor); + $booksData = [ - [ - 'title' => 'Foo', - 'year' => '1991', - '_author' => [ - 'name' => 'Dave', - ], - ], - [ - 'title' => 'Bar', - 'year' => '1997', - '_author' => [ - 'name' => 'Bob', - ], - ], + $bookFoo, + $bookBar, ]; // Try without metadata @@ -154,20 +143,20 @@ public function testSerializingCollectionResource() $expected = [ 'data' => [ [ - 'title' => 'Foo', - 'year' => 1991, + 'title' => 'Don Quixote', + 'year' => 1605, 'author' => [ 'data' => [ - 'name' => 'Dave', + 'name' => 'Miguel de Cervantes', ], ], ], [ - 'title' => 'Bar', + 'title' => 'Harry Potter', 'year' => 1997, 'author' => [ 'data' => [ - 'name' => 'Bob', + 'name' => 'J. K. Rowling', ], ], ], @@ -176,15 +165,15 @@ public function testSerializingCollectionResource() $this->assertSame($expected, $scope->toArray()); - $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}]}'; + $expectedJson = '{"data":[{"title":"Don Quixote","year":1605,"author":{"data":{"name":"Miguel de Cervantes"}}},{"title":"Harry Potter","year":1997,"author":{"data":{"name":"J. K. Rowling"}}}]}'; $this->assertSame($expectedJson, $scope->toJson()); //Test single field $manager->parseFieldsets(['books' => 'title']); $expected = [ 'data' => [ - ['title' => 'Foo'], - ['title' => 'Bar'] + ['title' => 'Don Quixote'], + ['title' => 'Harry Potter'], ], ]; $this->assertSame($expected, $scope->toArray()); @@ -194,11 +183,11 @@ public function testSerializingCollectionResource() $expected = [ 'data' => [ [ - 'title' => 'Foo', - 'year' => 1991 + 'title' => 'Don Quixote', + 'year' => 1605 ], [ - 'title' => 'Bar', + 'title' => 'Harry Potter', 'year' => 1997 ] ], @@ -210,18 +199,18 @@ public function testSerializingCollectionResource() $expected = [ 'data' => [ [ - 'title' => 'Foo', + 'title' => 'Don Quixote', 'author' => [ 'data' => [ - 'name' => 'Dave' + 'name' => 'Miguel de Cervantes', ] ] ], [ - 'title' => 'Bar', + 'title' => 'Harry Potter', 'author' => [ 'data' => [ - 'name' => 'Bob' + 'name' => 'J. K. Rowling', ] ] ] @@ -242,20 +231,20 @@ public function testSerializingCollectionResource() $expected = [ 'data' => [ [ - 'title' => 'Foo', - 'year' => 1991, + 'title' => 'Don Quixote', + 'year' => 1605, 'author' => [ 'data' => [ - 'name' => 'Dave', + 'name' => 'Miguel de Cervantes', ], ], ], [ - 'title' => 'Bar', + 'title' => 'Harry Potter', 'year' => 1997, 'author' => [ 'data' => [ - 'name' => 'Bob', + 'name' => 'J. K. Rowling', ], ], ], @@ -267,31 +256,31 @@ public function testSerializingCollectionResource() $this->assertSame($expected, $scope->toArray()); - $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}],"meta":{"foo":"bar"}}'; + $expectedJson = '{"data":[{"title":"Don Quixote","year":1605,"author":{"data":{"name":"Miguel de Cervantes"}}},{"title":"Harry Potter","year":1997,"author":{"data":{"name":"J. K. Rowling"}}}],"meta":{"foo":"bar"}}'; $this->assertSame($expectedJson, $scope->toJson()); $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']); $expected = [ 'data' => [ [ - 'title' => 'Foo', + 'title' => 'Don Quixote', 'author' => [ 'data' => [ - 'name' => 'Dave' + 'name' => 'Miguel de Cervantes', ] ] ], [ - 'title' => 'Bar', + 'title' => 'Harry Potter', 'author' => [ 'data' => [ - 'name' => 'Bob' + 'name' => 'J. K. Rowling', ] ] ] ], 'meta' => [ - 'foo' => 'bar' + 'foo' => 'bar', ] ]; @@ -304,13 +293,8 @@ public function testSerializingNullResource() $manager->parseIncludes('author'); $manager->setSerializer(new DataArraySerializer()); - $bookData = [ - 'title' => 'Foo', - 'year' => '1991', - '_author' => [ - 'name' => 'Dave', - ], - ]; + $phil = Person::make('Miguel de Cervantes'); + $bookData = Book::make('Don Quixote', '1605', $phil); // Try without metadata $resource = new NullResource($bookData, new GenericBookTransformer(), 'book'); diff --git a/test/Stub/Transformer/DefaultIncludeBookTransformer.php b/test/Stub/Transformer/DefaultIncludeBookTransformer.php index be85b8f6..97a60410 100644 --- a/test/Stub/Transformer/DefaultIncludeBookTransformer.php +++ b/test/Stub/Transformer/DefaultIncludeBookTransformer.php @@ -1,5 +1,6 @@ item(['c' => 'd'], new GenericAuthorTransformer()); + return $this->item(Person::make('Robert Cecil Martin'), new GenericAuthorTransformer()); } } diff --git a/test/Stub/Transformer/GenericAuthorTransformer.php b/test/Stub/Transformer/GenericAuthorTransformer.php index 1885f8c9..faa8ba47 100644 --- a/test/Stub/Transformer/GenericAuthorTransformer.php +++ b/test/Stub/Transformer/GenericAuthorTransformer.php @@ -1,11 +1,14 @@ $author->name, + ]; } } diff --git a/test/Stub/Transformer/GenericBookTransformer.php b/test/Stub/Transformer/GenericBookTransformer.php index 633ed508..5d92aab0 100644 --- a/test/Stub/Transformer/GenericBookTransformer.php +++ b/test/Stub/Transformer/GenericBookTransformer.php @@ -1,5 +1,6 @@ $book->title, + 'year' => (int) $book->year, + ]; - return $book; + if (! is_null($book->meta)) { + $data['meta'] = $book->meta; + } + + return $data; } - public function includeAuthor(array $book) + public function includeAuthor(Book $book) { - if (! isset($book['_author'])) { + if (is_null($book->author)) { return; } - return $this->item($book['_author'], new GenericAuthorTransformer(), 'author'); + return $this->item($book->author, new GenericAuthorTransformer(), 'author'); } }