Skip to content

Commit

Permalink
test: access scope in transformer
Browse files Browse the repository at this point in the history
this test demonstrates that scopes can be accessed in the transformers
  • Loading branch information
Mohammad-Alavi committed Dec 6, 2024
1 parent fe5feb4 commit 2cea8a6
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Spatie\Fractalistic\Fractal;
use Spatie\Fractalistic\Test\TestClasses\Author;
use Spatie\Fractalistic\Test\TestClasses\Book;
use Spatie\Fractalistic\Test\TestClasses\Character;
use Spatie\Fractalistic\Test\TestClasses\Publisher;

uses()
Expand Down Expand Up @@ -56,6 +57,11 @@ function getTestPublishers(): array
$authorA = new Author('Philip K Dick', '[email protected]');
$authorB = new Author('George R. R. Satan', '[email protected]');

$charA = new Character('Death');
$charB = new Character('Hex');
$charC = new Character('Ned Stark');
$charD = new Character('Tywin Lannister');

$bookA = new Book(
'1',
'Hogfather',
Expand All @@ -78,11 +84,19 @@ function getTestPublishers(): array

$bookA->author = $authorA;
$bookA->publisher = $publisherA;
$bookA->characters = [$charA, $charB];
$authorA->characters = [$charA, $charB];
$charA->book = $bookA;
$charB->book = $bookA;
$publisherA->books = [$bookA];
$authorA->books = [$bookA];

$bookB->author = $authorB;
$bookB->publisher = $publisherB;
$bookB->characters = [$charC, $charD];
$authorB->characters = [$charC, $charD];
$charC->book = $bookB;
$charD->book = $bookB;
$publisherB->books = [$bookB];
$authorB->books = [$bookB];

Expand Down
121 changes: 121 additions & 0 deletions tests/ScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,124 @@
]),
],
]);

it('can access scope in transformer', function (): void {
$fractal = Fractal::create(getTestPublishers(), new PublisherTransformer())
->parseIncludes('books.characters,books.author.characters');

$result = $fractal->toArray();

expect($result)->toEqual([
'data' => [
[
'name' => 'Elephant books',
'address' => 'Amazon rainforests, near the river',
'books' =>
[
'data' => [
[
'id' => 1,
'title' => 'Hogfather',
'characters' => [
'data' => [
[
'name' => 'Death',
'current_scope' => 'characters',
'parent_scope' => 'books',
'scope_identifier' => 'books.characters',
'called_by_book' => 'yes!',
],
[
'name' => 'Hex',
'current_scope' => 'characters',
'parent_scope' => 'books',
'scope_identifier' => 'books.characters',
'called_by_book' => 'yes!',
],
]
],
'author' => [
'data' => [
'name' => 'Philip K Dick',
'email' => '[email protected]',
'characters' => [
'data' => [
[
'name' => 'Death',
'current_scope' => 'characters',
'parent_scope' => 'author',
'scope_identifier' => 'books.author.characters',
'called_by_author' => 'indeed!',
],
[
'name' => 'Hex',
'current_scope' => 'characters',
'parent_scope' => 'author',
'scope_identifier' => 'books.author.characters',
'called_by_author' => 'indeed!',
],
]
],
]
],
],
],
],
],
[
'name' => 'Bloody Fantasy inc.',
'address' => 'Diagon Alley, before the bank, to the left',
'books' => [
'data' => [
[
'id' => 2,
'title' => 'Game Of Kill Everyone',
'characters' => [
'data' => [
[
'name' => 'Ned Stark',
'current_scope' => 'characters',
'parent_scope' => 'books',
'scope_identifier' => 'books.characters',
'called_by_book' => 'yes!',
],
[
'name' => 'Tywin Lannister',
'current_scope' => 'characters',
'parent_scope' => 'books',
'scope_identifier' => 'books.characters',
'called_by_book' => 'yes!',
],
]
],
'author' => [
'data' => [
'name' => 'George R. R. Satan',
'email' => '[email protected]',
'characters' => [
'data' => [
[
'name' => 'Ned Stark',
'current_scope' => 'characters',
'parent_scope' => 'author',
'scope_identifier' => 'books.author.characters',
'called_by_author' => 'indeed!',
],
[
'name' => 'Tywin Lannister',
'current_scope' => 'characters',
'parent_scope' => 'author',
'scope_identifier' => 'books.author.characters',
'called_by_author' => 'indeed!',
],
]
],
],
]
],
],
],
],
],
]);
});
7 changes: 7 additions & 0 deletions tests/TestClasses/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Author
public string $email;
/** @var Book[] */
public ?array $books = [];
/** @var Character[] */
public array $characters = [];

public function __construct(string $name, string $email)
{
Expand All @@ -19,4 +21,9 @@ public function books(): array
{
return $this->books;
}

public function characters(): array
{
return $this->characters;
}
}
6 changes: 6 additions & 0 deletions tests/TestClasses/AuthorTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class AuthorTransformer extends TransformerAbstract
{
protected array $availableIncludes = [
'books',
'characters'
];

public function transform(Author $author): array
Expand All @@ -23,4 +24,9 @@ public function includeBooks(Author $author): Collection
{
return $this->collection($author->books(), new BookTransformer());
}

public function includeCharacters(Author $author): Collection
{
return $this->collection($author->characters(), new CharacterTransformer());
}
}
10 changes: 10 additions & 0 deletions tests/TestClasses/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Book
public string $id;
public string $title;
public string $yr;
/** @var Character[] */
public array $characters = [];
public ?Publisher $publisher = null;
public ?Author $author = null;

Expand All @@ -25,6 +27,14 @@ public function publisher(): ?Publisher
return $this->publisher;
}

/**
* @return Character[]
*/
public function characters(): array
{
return $this->characters;
}

public function author(): ?Author
{
return $this->author;
Expand Down
7 changes: 7 additions & 0 deletions tests/TestClasses/BookTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Spatie\Fractalistic\Test\TestClasses;

use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;

class BookTransformer extends TransformerAbstract
{
protected array $availableIncludes = [
'publisher',
'characters',
'author',
];

Expand All @@ -25,6 +27,11 @@ public function includePublisher(Book $book): Item
return $this->item($book->publisher(), new PublisherTransformer());
}

public function includeCharacters(Book $book): Collection
{
return $this->collection($book->characters(), new CharacterTransformer());
}

public function includeAuthor(Book $book): Item
{
return $this->item($book->author(), new AuthorTransformer());
Expand Down
19 changes: 19 additions & 0 deletions tests/TestClasses/Character.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\Fractalistic\Test\TestClasses;

class Character
{
public string $name;
public ?Book $book = null;

public function __construct(string $name)
{
$this->name = $name;
}

public function book(): ?Book
{
return $this->book;
}
}
40 changes: 40 additions & 0 deletions tests/TestClasses/CharacterTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Spatie\Fractalistic\Test\TestClasses;

use League\Fractal\Resource\Item;
use League\Fractal\TransformerAbstract;

final class CharacterTransformer extends TransformerAbstract
{
protected array $availableIncludes = [
'book',
'author',
];

public function transform(Character $character): array
{
$parentScope = last($this->getCurrentScope()->getParentScopes());
$data = [
'name' => $character->name,
'current_scope' => $this->getCurrentScope()->getScopeIdentifier(),
'parent_scope' => $parentScope,
'scope_identifier' => $this->getCurrentScope()->getIdentifier(),
];

if ($parentScope === 'author') {
$data['called_by_author'] = 'indeed!';
}

if ($parentScope === 'books') {
$data['called_by_book'] = 'yes!';
}

return $data;
}

public function includeBook(Character $character): Item
{
return $this->item($character->book(), new BookTransformer());
}
}

0 comments on commit 2cea8a6

Please sign in to comment.