Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 147
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
GraphqlTestHelper
0.00% covered (danger)
0.00%
0 / 147
0.00% covered (danger)
0.00%
0 / 10
156
0.00% covered (danger)
0.00%
0 / 1
 createEmptyCall
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 createSchemaCall
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 createTypeInspectionCall
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 createIntrospectionCall
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 createEntityList
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 createExpectedListResponse
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 createQueryCall
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 createFileQuery
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 1
2
 createMutationCall
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
2
 createFileuploadCall
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\IntegrationTests\Graphql;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\FileStorage\ImageFile;
6use Apie\Core\FileStorage\StoredFile;
7use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\PrimitiveOnlyIdentifier;
8use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\UploadedFileIdentifier;
9use Apie\IntegrationTests\Apie\TypeDemo\Resources\PrimitiveOnly;
10use Apie\IntegrationTests\Apie\TypeDemo\Resources\UploadedFile;
11use Apie\IntegrationTests\IntegrationTestHelper;
12
13class GraphqlTestHelper extends IntegrationTestHelper
14{
15    public function createEmptyCall(): GraphqlProvider
16    {
17        return new GraphqlProvider(
18            new BoundedContextId('types'),
19            [
20                'query' => '{ apie_version }'
21            ],
22            [
23                "data" => [
24                    "apie_version" => \Apie\Core\ApieLib::VERSION,
25                ],
26            ]
27        );
28    }
29
30    public function createSchemaCall(): GraphqlProvider
31    {
32        return new SchemaGraphqlProvider(
33            new BoundedContextId('types'),
34            [
35                'query' => '{
36  __schema {
37    queryType {
38      fields {
39        name
40        description
41      }
42    }
43  }
44}'
45            ],
46            [
47            ]
48        );
49    }
50
51    public function createTypeInspectionCall(): GraphqlProvider
52    {
53        return new SchemaGraphqlProvider(
54            new BoundedContextId('types'),
55            [
56                'query' => 'query IntrospectionQuery {
57  __schema {
58    queryType {
59      name
60    }
61    mutationType {
62      name
63    }
64    subscriptionType {
65      name
66    }
67    types {
68      ...FullType
69    }
70    directives {
71      name
72      description
73      locations
74      args {
75        ...InputValue
76      }
77    }
78  }
79}
80
81fragment FullType on __Type {
82  kind
83  name
84  description
85  fields(includeDeprecated: true) {
86    name
87    description
88    args {
89      ...InputValue
90    }
91    type {
92      ...TypeRef
93    }
94    isDeprecated
95    deprecationReason
96  }
97  inputFields {
98    ...InputValue
99  }
100  interfaces {
101    ...TypeRef
102  }
103  enumValues(includeDeprecated: true) {
104    name
105    description
106    isDeprecated
107    deprecationReason
108  }
109  possibleTypes {
110    ...TypeRef
111  }
112}
113
114fragment InputValue on __InputValue {
115  name
116  description
117  type {
118    ...TypeRef
119  }
120  defaultValue
121}
122
123fragment TypeRef on __Type {
124  kind
125  name
126  ofType {
127    kind
128    name
129    ofType {
130      kind
131      name
132      ofType {
133        kind
134        name
135        ofType {
136          kind
137          name
138          ofType {
139            kind
140            name
141            ofType {
142              kind
143              name
144              ofType {
145                kind
146                name
147              }
148            }
149          }
150        }
151      }
152    }
153  }
154}
155'
156            ],
157            []
158        );
159    }
160
161    public function createIntrospectionCall(): GraphqlProvider
162    {
163        return new SchemaGraphqlProvider(
164            new BoundedContextId('types'),
165            [
166                'query' => '{
167  __type(name: "findPrimitiveOnly") {
168    name
169    description
170    fields {
171      name
172      description
173    }
174  }
175}'
176            ],
177            [
178            ]
179        );
180    }
181
182    /**
183     * @return array<int, PrimitiveOnly>
184     */
185    private function createEntityList(int $count): array
186    {
187        $entities = [];
188        for ($i = 0; $i < $count; $i++) {
189            $entity = new PrimitiveOnly(PrimitiveOnlyIdentifier::generateFromInteger($i));
190            $entity->stringField = 'String ' . $i;
191            $entity->integerField = $i * 10;
192            $entity->floatingPoint = $i * 1.5;
193            $entity->booleanField = $i % 2 === 0;
194            $entities[] = $entity;
195        }
196        return $entities;
197    }
198
199    /**
200     * @return array<int, array<string, mixed>>
201     */
202    private function createExpectedListResponse(int $start, int $end): array
203    {
204        $list = [];
205        for ($i = $start; $i <= $end; $i++) {
206            $list[] = [
207                'id' => (string) PrimitiveOnlyIdentifier::generateFromInteger($i),
208                'stringField' => 'String ' . $i,
209                'integerField' => $i * 10,
210                'floatingPoint' => $i * 1.5,
211                'booleanField' => $i % 2 === 0,
212            ];
213        }
214        return $list;
215    }
216
217    public function createQueryCall(): GraphqlProvider
218    {
219        $entities = $this->createEntityList(30);
220        return new GraphqlProvider(
221            new BoundedContextId('types'),
222            [
223                  'query' => '{ findPrimitiveOnly(filter: { orderBy: "+id" }) { totalCount, list { id, stringField, integerField, floatingPoint, booleanField } } }'
224              ],
225            [
226                'data' => [
227                    'findPrimitiveOnly' => [
228                        'list' => $this->createExpectedListResponse(0, 19),
229                        'totalCount' => 30,
230                    ]
231                ]
232              ],
233            $entities
234        );
235    }
236
237    public function createFileQuery(): GraphqlProvider
238    {
239        $path = __DIR__ . '/../../fixtures/apie-logo.svg';
240        $entities = [
241          new UploadedFile(
242              UploadedFileIdentifier::fromNative('821c5aca-2853-4f7a-b3bc-045a81798e24'),
243              StoredFile::createFromLocalFile($path),
244              ImageFile::createFromLocalFile($path),
245          )
246        ];
247        return new GraphqlProvider(
248            new BoundedContextId('types'),
249            [
250                  'query' => '{ findUploadedFile(id: "821c5aca-2853-4f7a-b3bc-045a81798e24") { list { id, imageFile, file } } }'
251              ],
252            [
253                'data' => [
254                    'findUploadedFile' => [
255                        'list' => [
256                          [
257                            'id' => '821c5aca-2853-4f7a-b3bc-045a81798e24',
258                            'imageFile' => '/types/UploadedFile/821c5aca-2853-4f7a-b3bc-045a81798e24/download/imageFile',
259                            'file' => '/types/UploadedFile/821c5aca-2853-4f7a-b3bc-045a81798e24/download/file',
260                          ]
261                        ]
262                    ]
263                ]
264            ],
265            $entities
266        );
267    }
268
269    public function createMutationCall(): GraphqlProvider
270    {
271        $entities = $this->createEntityList(30);
272        return new GraphqlProvider(
273            new BoundedContextId('types'),
274            [
275              'query' => <<<GQL
276mutation CreatePrimitiveOnly(\$id: String!, \$booleanField: Boolean!) {
277  createPrimitiveOnly(input: {
278    id: \$id,
279    booleanField: \$booleanField
280  }) { id, stringField, integerField, booleanField }
281}
282GQL,
283                'variables' => [
284                    'id' => '821c5aca-2853-4f7a-b3bc-045a81798e24',
285                    'booleanField' => false,
286                ],
287            ],
288            [
289                'data' => [
290                    'createPrimitiveOnly' => [
291                        'id' => '821c5aca-2853-4f7a-b3bc-045a81798e24',
292                        'stringField' => null,
293                        'integerField' => null,
294                        'booleanField' => false,
295                    ]
296                ]
297              ],
298            $entities
299        );
300    }
301
302    public function createFileuploadCall(): GraphqlProvider
303    {
304        return new GraphqlWithFileUpload(
305            new BoundedContextId('types'),
306            [
307              'query' => <<<GQL
308mutation CreateUploadedFile(\$id: String!, \$file: UploadedFileInterface_create!) {
309  createUploadedFile(input: {
310    id: \$id,
311    file: \$file
312  }) { id, file, stream, imageFile }
313}
314GQL,
315                'variables' => [
316                    'id' => '821c5aca-2853-4f7a-b3bc-045a81798e24',
317                ],
318            ],
319            [
320                'data' => [
321                    'createUploadedFile' => [
322                        'id' => '821c5aca-2853-4f7a-b3bc-045a81798e24',
323                        'imageFile' => null,
324                        'stream' => '/types/UploadedFile/821c5aca-2853-4f7a-b3bc-045a81798e24/download/stream',
325                        'file' => '/types/UploadedFile/821c5aca-2853-4f7a-b3bc-045a81798e24/download/file',
326                    ]
327                ]
328              ],
329            [],
330            [
331                'variables.file' => StoredFile::createFromString('test', 'test.txt'),
332              ]
333        );
334    }
335
336}