-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BREAKING] Fix empty param bag #82
Open
Mohammad-Alavi
wants to merge
3
commits into
spatie:main
Choose a base branch
from
Mohammad-Alavi:fix-empty-param-bag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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!'; | ||
} | ||
Comment on lines
+25
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you can see, we are returnning different fields based on the context that we are in. |
||
|
||
return $data; | ||
} | ||
|
||
public function includeBook(Character $character): Item | ||
{ | ||
return $this->item($character->book(), new BookTransformer()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual fix.
The rest are just tests 😬