-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from Mohammad-Alavi/test-nested-includes
Test nested includes
- Loading branch information
Showing
8 changed files
with
270 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<?php | ||
|
||
use Spatie\Fractalistic\Fractal; | ||
use Spatie\Fractalistic\Test\TestClasses\PublisherTransformer; | ||
use Spatie\Fractalistic\Test\TestClasses\TestTransformer; | ||
use function PHPUnit\Framework\assertEquals; | ||
|
||
|
@@ -86,3 +87,66 @@ | |
->toArray(); | ||
assertEquals($expectedArray, $array); | ||
}); | ||
|
||
it('can parse nested includes', function ($includes): void { | ||
$fractal = Fractal::create(getTestPublishers(), new PublisherTransformer()) | ||
->parseIncludes($includes); | ||
|
||
$result = $fractal->toArray(); | ||
|
||
$expectedArray = [ | ||
'data' => [ | ||
[ | ||
'name' => 'Elephant books', | ||
'address' => 'Amazon rainforests, near the river', | ||
'books' => [ | ||
'data' => [ | ||
[ | ||
'id' => 1, | ||
'title' => 'Hogfather', | ||
'author' => [ | ||
'data' => [ | ||
'name' => 'Philip K Dick', | ||
'email' => '[email protected]', | ||
], | ||
], | ||
] | ||
], | ||
], | ||
], | ||
[ | ||
'name' => 'Bloody Fantasy inc.', | ||
'address' => 'Diagon Alley, before the bank, to the left', | ||
'books' => [ | ||
'data' => [ | ||
[ | ||
'id' => 2, | ||
'title' => 'Game Of Kill Everyone', | ||
'author' => [ | ||
'data' => [ | ||
'name' => 'George R. R. Satan', | ||
'email' => '[email protected]', | ||
], | ||
], | ||
] | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
expect($result)->toBe($expectedArray); | ||
})->with([ | ||
[ | ||
'string' => 'books,books.author', | ||
], | ||
[ | ||
'array' => ['books', 'books.author'], | ||
], | ||
[ | ||
'string: auto-loads parent' => 'books.author', | ||
], | ||
[ | ||
'array: auto-loads parent' => ['books.author'], | ||
], | ||
]); |
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 |
---|---|---|
|
@@ -7,6 +7,9 @@ | |
*/ | ||
|
||
use Spatie\Fractalistic\Fractal; | ||
use Spatie\Fractalistic\Test\TestClasses\Author; | ||
use Spatie\Fractalistic\Test\TestClasses\Book; | ||
use Spatie\Fractalistic\Test\TestClasses\Publisher; | ||
|
||
uses() | ||
->beforeEach(function () { | ||
|
@@ -48,3 +51,40 @@ | |
| Functions | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
function getTestPublishers(): array | ||
{ | ||
$authorA = new Author('Philip K Dick', '[email protected]'); | ||
$authorB = new Author('George R. R. Satan', '[email protected]'); | ||
|
||
$bookA = new Book( | ||
'1', | ||
'Hogfather', | ||
'1998', | ||
); | ||
$bookB = new Book( | ||
'2', | ||
'Game Of Kill Everyone', | ||
'2014', | ||
); | ||
|
||
$publisherA = new Publisher( | ||
'Elephant books', | ||
'Amazon rainforests, near the river', | ||
); | ||
$publisherB = new Publisher( | ||
'Bloody Fantasy inc.', | ||
'Diagon Alley, before the bank, to the left', | ||
); | ||
|
||
$bookA->author = $authorA; | ||
$bookA->publisher = $publisherA; | ||
$publisherA->books = [$bookA]; | ||
$authorA->books = [$bookA]; | ||
|
||
$bookB->author = $authorB; | ||
$bookB->publisher = $publisherB; | ||
$publisherB->books = [$bookB]; | ||
$authorB->books = [$bookB]; | ||
|
||
return [$publisherA, $publisherB]; | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace Spatie\Fractalistic\Test\TestClasses; | ||
|
||
class Author | ||
{ | ||
public string $name; | ||
public string $email; | ||
/** @var Book[] */ | ||
public ?array $books = []; | ||
|
||
public function __construct(string $name, string $email) | ||
{ | ||
$this->name = $name; | ||
$this->email = $email; | ||
} | ||
|
||
public function books(): array | ||
{ | ||
return $this->books; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Spatie\Fractalistic\Test\TestClasses; | ||
|
||
use League\Fractal\Resource\Collection; | ||
use League\Fractal\TransformerAbstract; | ||
|
||
class AuthorTransformer extends TransformerAbstract | ||
{ | ||
protected array $availableIncludes = [ | ||
'books', | ||
]; | ||
|
||
public function transform(Author $author): array | ||
{ | ||
return [ | ||
'name' => $author->name, | ||
'email' => $author->email, | ||
]; | ||
} | ||
|
||
public function includeBooks(Author $author): Collection | ||
{ | ||
return $this->collection($author->books(), new BookTransformer()); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Spatie\Fractalistic\Test\TestClasses; | ||
|
||
class Book | ||
{ | ||
public string $id; | ||
public string $title; | ||
public string $yr; | ||
public ?Publisher $publisher = null; | ||
public ?Author $author = null; | ||
|
||
public function __construct( | ||
string $id, | ||
string $title, | ||
string $yr | ||
) { | ||
$this->id = $id; | ||
$this->title = $title; | ||
$this->yr = $yr; | ||
} | ||
|
||
public function publisher(): ?Publisher | ||
{ | ||
return $this->publisher; | ||
} | ||
|
||
public function author(): ?Author | ||
{ | ||
return $this->author; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Spatie\Fractalistic\Test\TestClasses; | ||
|
||
use League\Fractal\Resource\Item; | ||
use League\Fractal\TransformerAbstract; | ||
|
||
class BookTransformer extends TransformerAbstract | ||
{ | ||
protected array $availableIncludes = [ | ||
'publisher', | ||
'author', | ||
]; | ||
|
||
public function transform(Book $book): array | ||
{ | ||
return [ | ||
'id' => (int)$book->id, | ||
'title' => $book->title, | ||
]; | ||
} | ||
|
||
public function includePublisher(Book $book): Item | ||
{ | ||
return $this->item($book->publisher(), new PublisherTransformer()); | ||
} | ||
|
||
public function includeAuthor(Book $book): Item | ||
{ | ||
return $this->item($book->author(), new AuthorTransformer()); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Spatie\Fractalistic\Test\TestClasses; | ||
|
||
class Publisher | ||
{ | ||
public string $name; | ||
public string $address; | ||
/** @var Book[] */ | ||
public array $books = []; | ||
|
||
/** | ||
* @param string $name | ||
* @param string $address | ||
*/ | ||
public function __construct( | ||
string $name, | ||
string $address | ||
) { | ||
$this->name = $name; | ||
$this->address = $address; | ||
} | ||
|
||
public function books(): array | ||
{ | ||
return $this->books; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace Spatie\Fractalistic\Test\TestClasses; | ||
|
||
use League\Fractal\Resource\Collection; | ||
use League\Fractal\TransformerAbstract; | ||
|
||
class PublisherTransformer extends TransformerAbstract | ||
{ | ||
protected array $availableIncludes = [ | ||
'books', | ||
]; | ||
|
||
public function transform(Publisher $publisher): array | ||
{ | ||
return [ | ||
'name' => $publisher->name, | ||
'address' => $publisher->address, | ||
]; | ||
} | ||
|
||
public function includeBooks(Publisher $publisher): Collection | ||
{ | ||
return $this->collection($publisher->books(), new BookTransformer()); | ||
} | ||
} |