Skip to content

Commit 6469bf6

Browse files
[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
1 parent 4b54563 commit 6469bf6

File tree

9 files changed

+267
-9
lines changed

9 files changed

+267
-9
lines changed

src/Fractal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function createData()
406406
$this->manager->parseFieldsets($this->fieldsets);
407407
}
408408

409-
return $this->manager->createData($this->getResource(), $this->resourceName);
409+
return $this->manager->createData($this->getResource());
410410
}
411411

412412
/**

tests/Pest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Spatie\Fractalistic\Fractal;
1010
use Spatie\Fractalistic\Test\TestClasses\Author;
1111
use Spatie\Fractalistic\Test\TestClasses\Book;
12+
use Spatie\Fractalistic\Test\TestClasses\Character;
1213
use Spatie\Fractalistic\Test\TestClasses\Publisher;
1314

1415
uses()
@@ -56,6 +57,11 @@ function getTestPublishers(): array
5657
$authorA = new Author('Philip K Dick', 'philip@example.org');
5758
$authorB = new Author('George R. R. Satan', 'george@example.org');
5859

60+
$charA = new Character('Death');
61+
$charB = new Character('Hex');
62+
$charC = new Character('Ned Stark');
63+
$charD = new Character('Tywin Lannister');
64+
5965
$bookA = new Book(
6066
'1',
6167
'Hogfather',
@@ -78,11 +84,19 @@ function getTestPublishers(): array
7884

7985
$bookA->author = $authorA;
8086
$bookA->publisher = $publisherA;
87+
$bookA->characters = [$charA, $charB];
88+
$authorA->characters = [$charA, $charB];
89+
$charA->book = $bookA;
90+
$charB->book = $bookA;
8191
$publisherA->books = [$bookA];
8292
$authorA->books = [$bookA];
8393

8494
$bookB->author = $authorB;
8595
$bookB->publisher = $publisherB;
96+
$bookB->characters = [$charC, $charD];
97+
$authorB->characters = [$charC, $charD];
98+
$charC->book = $bookB;
99+
$charD->book = $bookB;
86100
$publisherB->books = [$bookB];
87101
$authorB->books = [$bookB];
88102

tests/ScopeTest.php

Lines changed: 163 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,168 @@
11
<?php
22

3-
use Spatie\Fractalistic\Test\TestClasses\TestTransformer;
4-
use function PHPUnit\Framework\assertEquals;
3+
use League\Fractal\ParamBag;
4+
use Spatie\Fractalistic\Fractal;
5+
use Spatie\Fractalistic\Test\TestClasses\PublisherTransformer;
56

6-
it('uses an identifier for the scope', function () {
7-
$scope = $this->fractal
8-
->collection($this->testBooks, new TestTransformer(), 'books')
9-
->parseIncludes('characters')
10-
->createData();
7+
it('can parse include parameters', function ($resourceName, string $include, string $includeWithParams, ParamBag $expected): void {
8+
$fractal = Fractal::create(getTestPublishers(), new PublisherTransformer())
9+
->withResourceName($resourceName)
10+
->parseIncludes($includeWithParams);
1111

12-
assertEquals('books', $scope->getIdentifier());
12+
$scope = $fractal->createData();
13+
14+
$identifier = $scope->getIdentifier($include);
15+
$actualParams = $scope->getManager()->getIncludeParams($identifier);
16+
expect($actualParams)->toEqual($expected);
17+
})->with([
18+
[
19+
'resource name: string' => 'Publisher',
20+
],
21+
[
22+
'resource name: null' => null,
23+
],
24+
])->with([
25+
[
26+
'include' => 'books',
27+
'include_with_params' => 'books:test(2|value)',
28+
'expected' => new ParamBag([
29+
'test' => ['2', 'value'],
30+
]),
31+
],
32+
[
33+
'include' => 'books',
34+
'include_with_params' => 'books:test(another_value|3):another(1|2|3)',
35+
'expected' => new ParamBag([
36+
'test' => ['another_value', '3'],
37+
'another' => ['1', '2', '3'],
38+
]),
39+
],
40+
[
41+
'include' => 'books.author',
42+
'include_with_params' => 'books.author:test(test|value)',
43+
'expected' => new ParamBag([
44+
'test' => ['test', 'value'],
45+
]),
46+
],
47+
]);
48+
49+
it('can access scope in transformer', function (): void {
50+
$fractal = Fractal::create(getTestPublishers(), new PublisherTransformer())
51+
->parseIncludes('books.characters,books.author.characters');
52+
53+
$result = $fractal->toArray();
54+
55+
expect($result)->toEqual([
56+
'data' => [
57+
[
58+
'name' => 'Elephant books',
59+
'address' => 'Amazon rainforests, near the river',
60+
'books' =>
61+
[
62+
'data' => [
63+
[
64+
'id' => 1,
65+
'title' => 'Hogfather',
66+
'characters' => [
67+
'data' => [
68+
[
69+
'name' => 'Death',
70+
'current_scope' => 'characters',
71+
'parent_scope' => 'books',
72+
'scope_identifier' => 'books.characters',
73+
'called_by_book' => 'yes!',
74+
],
75+
[
76+
'name' => 'Hex',
77+
'current_scope' => 'characters',
78+
'parent_scope' => 'books',
79+
'scope_identifier' => 'books.characters',
80+
'called_by_book' => 'yes!',
81+
],
82+
]
83+
],
84+
'author' => [
85+
'data' => [
86+
'name' => 'Philip K Dick',
87+
'email' => 'philip@example.org',
88+
'characters' => [
89+
'data' => [
90+
[
91+
'name' => 'Death',
92+
'current_scope' => 'characters',
93+
'parent_scope' => 'author',
94+
'scope_identifier' => 'books.author.characters',
95+
'called_by_author' => 'indeed!',
96+
],
97+
[
98+
'name' => 'Hex',
99+
'current_scope' => 'characters',
100+
'parent_scope' => 'author',
101+
'scope_identifier' => 'books.author.characters',
102+
'called_by_author' => 'indeed!',
103+
],
104+
]
105+
],
106+
]
107+
],
108+
],
109+
],
110+
],
111+
],
112+
[
113+
'name' => 'Bloody Fantasy inc.',
114+
'address' => 'Diagon Alley, before the bank, to the left',
115+
'books' => [
116+
'data' => [
117+
[
118+
'id' => 2,
119+
'title' => 'Game Of Kill Everyone',
120+
'characters' => [
121+
'data' => [
122+
[
123+
'name' => 'Ned Stark',
124+
'current_scope' => 'characters',
125+
'parent_scope' => 'books',
126+
'scope_identifier' => 'books.characters',
127+
'called_by_book' => 'yes!',
128+
],
129+
[
130+
'name' => 'Tywin Lannister',
131+
'current_scope' => 'characters',
132+
'parent_scope' => 'books',
133+
'scope_identifier' => 'books.characters',
134+
'called_by_book' => 'yes!',
135+
],
136+
]
137+
],
138+
'author' => [
139+
'data' => [
140+
'name' => 'George R. R. Satan',
141+
'email' => 'george@example.org',
142+
'characters' => [
143+
'data' => [
144+
[
145+
'name' => 'Ned Stark',
146+
'current_scope' => 'characters',
147+
'parent_scope' => 'author',
148+
'scope_identifier' => 'books.author.characters',
149+
'called_by_author' => 'indeed!',
150+
],
151+
[
152+
'name' => 'Tywin Lannister',
153+
'current_scope' => 'characters',
154+
'parent_scope' => 'author',
155+
'scope_identifier' => 'books.author.characters',
156+
'called_by_author' => 'indeed!',
157+
],
158+
]
159+
],
160+
],
161+
]
162+
],
163+
],
164+
],
165+
],
166+
],
167+
]);
13168
});

tests/TestClasses/Author.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Author
88
public string $email;
99
/** @var Book[] */
1010
public ?array $books = [];
11+
/** @var Character[] */
12+
public array $characters = [];
1113

1214
public function __construct(string $name, string $email)
1315
{
@@ -19,4 +21,9 @@ public function books(): array
1921
{
2022
return $this->books;
2123
}
24+
25+
public function characters(): array
26+
{
27+
return $this->characters;
28+
}
2229
}

tests/TestClasses/AuthorTransformer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class AuthorTransformer extends TransformerAbstract
99
{
1010
protected array $availableIncludes = [
1111
'books',
12+
'characters'
1213
];
1314

1415
public function transform(Author $author): array
@@ -23,4 +24,9 @@ public function includeBooks(Author $author): Collection
2324
{
2425
return $this->collection($author->books(), new BookTransformer());
2526
}
27+
28+
public function includeCharacters(Author $author): Collection
29+
{
30+
return $this->collection($author->characters(), new CharacterTransformer());
31+
}
2632
}

tests/TestClasses/Book.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class Book
77
public string $id;
88
public string $title;
99
public string $yr;
10+
/** @var Character[] */
11+
public array $characters = [];
1012
public ?Publisher $publisher = null;
1113
public ?Author $author = null;
1214

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

30+
/**
31+
* @return Character[]
32+
*/
33+
public function characters(): array
34+
{
35+
return $this->characters;
36+
}
37+
2838
public function author(): ?Author
2939
{
3040
return $this->author;

tests/TestClasses/BookTransformer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Spatie\Fractalistic\Test\TestClasses;
44

5+
use League\Fractal\Resource\Collection;
56
use League\Fractal\Resource\Item;
67
use League\Fractal\TransformerAbstract;
78

89
class BookTransformer extends TransformerAbstract
910
{
1011
protected array $availableIncludes = [
1112
'publisher',
13+
'characters',
1214
'author',
1315
];
1416

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

30+
public function includeCharacters(Book $book): Collection
31+
{
32+
return $this->collection($book->characters(), new CharacterTransformer());
33+
}
34+
2835
public function includeAuthor(Book $book): Item
2936
{
3037
return $this->item($book->author(), new AuthorTransformer());

tests/TestClasses/Character.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Spatie\Fractalistic\Test\TestClasses;
4+
5+
class Character
6+
{
7+
public string $name;
8+
public ?Book $book = null;
9+
10+
public function __construct(string $name)
11+
{
12+
$this->name = $name;
13+
}
14+
15+
public function book(): ?Book
16+
{
17+
return $this->book;
18+
}
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Spatie\Fractalistic\Test\TestClasses;
4+
5+
use League\Fractal\Resource\Item;
6+
use League\Fractal\TransformerAbstract;
7+
8+
final class CharacterTransformer extends TransformerAbstract
9+
{
10+
protected array $availableIncludes = [
11+
'book',
12+
'author',
13+
];
14+
15+
public function transform(Character $character): array
16+
{
17+
$parentScope = last($this->getCurrentScope()->getParentScopes());
18+
$data = [
19+
'name' => $character->name,
20+
'current_scope' => $this->getCurrentScope()->getScopeIdentifier(),
21+
'parent_scope' => $parentScope,
22+
'scope_identifier' => $this->getCurrentScope()->getIdentifier(),
23+
];
24+
25+
if ($parentScope === 'author') {
26+
$data['called_by_author'] = 'indeed!';
27+
}
28+
29+
if ($parentScope === 'books') {
30+
$data['called_by_book'] = 'yes!';
31+
}
32+
33+
return $data;
34+
}
35+
36+
public function includeBook(Character $character): Item
37+
{
38+
return $this->item($character->book(), new BookTransformer());
39+
}
40+
}

0 commit comments

Comments
 (0)