-
-
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.
this test demonstrates that scopes can be accessed in the transformers
- Loading branch information
1 parent
fe5feb4
commit 2cea8a6
Showing
8 changed files
with
224 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 |
---|---|---|
|
@@ -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() | ||
|
@@ -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', | ||
|
@@ -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]; | ||
|
||
|
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 |
---|---|---|
|
@@ -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!', | ||
], | ||
] | ||
], | ||
], | ||
] | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
}); |
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
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
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
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
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,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; | ||
} | ||
} |
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,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()); | ||
} | ||
} |