-
-
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.
[BREAKING] Fix empty param bag (#82)
* fix: empty param bag * test: access scope in transformer this test demonstrates that scopes can be accessed in the transformers
- Loading branch information
1 parent
4b54563
commit 6469bf6
Showing
9 changed files
with
267 additions
and
9 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
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 |
---|---|---|
@@ -1,13 +1,168 @@ | ||
<?php | ||
|
||
use Spatie\Fractalistic\Test\TestClasses\TestTransformer; | ||
use function PHPUnit\Framework\assertEquals; | ||
use League\Fractal\ParamBag; | ||
use Spatie\Fractalistic\Fractal; | ||
use Spatie\Fractalistic\Test\TestClasses\PublisherTransformer; | ||
|
||
it('uses an identifier for the scope', function () { | ||
$scope = $this->fractal | ||
->collection($this->testBooks, new TestTransformer(), 'books') | ||
->parseIncludes('characters') | ||
->createData(); | ||
it('can parse include parameters', function ($resourceName, string $include, string $includeWithParams, ParamBag $expected): void { | ||
$fractal = Fractal::create(getTestPublishers(), new PublisherTransformer()) | ||
->withResourceName($resourceName) | ||
->parseIncludes($includeWithParams); | ||
|
||
assertEquals('books', $scope->getIdentifier()); | ||
$scope = $fractal->createData(); | ||
|
||
$identifier = $scope->getIdentifier($include); | ||
$actualParams = $scope->getManager()->getIncludeParams($identifier); | ||
expect($actualParams)->toEqual($expected); | ||
})->with([ | ||
[ | ||
'resource name: string' => 'Publisher', | ||
], | ||
[ | ||
'resource name: null' => null, | ||
], | ||
])->with([ | ||
[ | ||
'include' => 'books', | ||
'include_with_params' => 'books:test(2|value)', | ||
'expected' => new ParamBag([ | ||
'test' => ['2', 'value'], | ||
]), | ||
], | ||
[ | ||
'include' => 'books', | ||
'include_with_params' => 'books:test(another_value|3):another(1|2|3)', | ||
'expected' => new ParamBag([ | ||
'test' => ['another_value', '3'], | ||
'another' => ['1', '2', '3'], | ||
]), | ||
], | ||
[ | ||
'include' => 'books.author', | ||
'include_with_params' => 'books.author:test(test|value)', | ||
'expected' => new ParamBag([ | ||
'test' => ['test', 'value'], | ||
]), | ||
], | ||
]); | ||
|
||
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()); | ||
} | ||
} |