Skip to content

Commit

Permalink
Merge pull request #79 from Mohammad-Alavi/test-nested-includes
Browse files Browse the repository at this point in the history
Test nested includes
  • Loading branch information
Nielsvanpach authored Dec 17, 2024
2 parents fdb6016 + db8f02e commit 44e7fe0
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/IncludesTest.php
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;

Expand Down Expand Up @@ -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'],
],
]);
40 changes: 40 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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];
}
22 changes: 22 additions & 0 deletions tests/TestClasses/Author.php
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;
}
}
26 changes: 26 additions & 0 deletions tests/TestClasses/AuthorTransformer.php
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());
}
}
32 changes: 32 additions & 0 deletions tests/TestClasses/Book.php
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;
}
}
32 changes: 32 additions & 0 deletions tests/TestClasses/BookTransformer.php
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());
}
}
28 changes: 28 additions & 0 deletions tests/TestClasses/Publisher.php
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;
}
}
26 changes: 26 additions & 0 deletions tests/TestClasses/PublisherTransformer.php
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());
}
}

0 comments on commit 44e7fe0

Please sign in to comment.