|
1 | 1 | <?php |
2 | 2 |
|
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; |
5 | 6 |
|
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); |
11 | 11 |
|
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 | + ]); |
13 | 168 | }); |
0 commit comments