Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
n/a
0 / 0
n/a
0 / 0
CRAP
n/a
0 / 0
CreatesApieBoundedContext
n/a
0 / 0
n/a
0 / 0
22
n/a
0 / 0
 createPostUserTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createGetUserTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createGetAnimalTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createPostPrimitiveOnlyTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createPostAnimalTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createPostUploadedFileTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createPostOrderTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createPropertyOptionsTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createObjectWithRestrictionTestRequest
n/a
0 / 0
n/a
0 / 0
1
 getObjectWithRestrictionTestRequest
n/a
0 / 0
n/a
0 / 0
1
 getObjectWithRestrictionDeniedTestRequest
n/a
0 / 0
n/a
0 / 0
1
 getObjectWithRestrictionListTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createInvalidPropertyOptionsTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createMethodArgumentOptionsTestRequest
n/a
0 / 0
n/a
0 / 0
1
 createBlockUserFormSubmit
n/a
0 / 0
n/a
0 / 0
1
 createCustomActionRequest
n/a
0 / 0
n/a
0 / 0
1
 createVariadicActionRequest
n/a
0 / 0
n/a
0 / 0
1
 createSimpleConsoleInteraction
n/a
0 / 0
n/a
0 / 0
1
 createOrderLineInteraction
n/a
0 / 0
n/a
0 / 0
1
 createFileUploadInteraction
n/a
0 / 0
n/a
0 / 0
1
 createPolymorphicObjectInteraction
n/a
0 / 0
n/a
0 / 0
1
 createExampleBoundedContext
n/a
0 / 0
n/a
0 / 0
1
1<?php
2namespace Apie\IntegrationTests\Concerns;
3
4use Apie\Common\ValueObjects\EntityNamespace;
5use Apie\Core\BoundedContext\BoundedContextId;
6use Apie\CountryAndPhoneNumber\DutchPhoneNumber;
7use Apie\IntegrationTests\Apie\TypeDemo\Entities\Human;
8use Apie\IntegrationTests\Apie\TypeDemo\Entities\Ostrich;
9use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\AnimalIdentifier;
10use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\RestrictedEntityIdentifier;
11use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\UserIdentifier;
12use Apie\IntegrationTests\Apie\TypeDemo\Resources\Animal;
13use Apie\IntegrationTests\Apie\TypeDemo\Resources\Order;
14use Apie\IntegrationTests\Apie\TypeDemo\Resources\PrimitiveOnly;
15use Apie\IntegrationTests\Apie\TypeDemo\Resources\RestrictedEntity;
16use Apie\IntegrationTests\Apie\TypeDemo\Resources\UploadedFile;
17use Apie\IntegrationTests\Apie\TypeDemo\Resources\User;
18use Apie\IntegrationTests\Config\BoundedContextConfig;
19use Apie\IntegrationTests\Console\InteractiveConsoleCommand;
20use Apie\IntegrationTests\Requests\ActionMethodApiCall;
21use Apie\IntegrationTests\Requests\CmsFormSubmitRequest;
22use Apie\IntegrationTests\Requests\GetResourceApiCall;
23use Apie\IntegrationTests\Requests\GetResourceApiCallDenied;
24use Apie\IntegrationTests\Requests\GetResourceListApiCall;
25use Apie\IntegrationTests\Requests\JsonFields\GetAndSetObjectField;
26use Apie\IntegrationTests\Requests\JsonFields\GetAndSetPrimitiveField;
27use Apie\IntegrationTests\Requests\JsonFields\GetAndSetUploadedFileField;
28use Apie\IntegrationTests\Requests\JsonFields\GetPrimitiveField;
29use Apie\IntegrationTests\Requests\JsonFields\GetUuidField;
30use Apie\IntegrationTests\Requests\JsonFields\SetPrimitiveField;
31use Apie\IntegrationTests\Requests\TestRequestInterface;
32use Apie\IntegrationTests\Requests\ValidCreateResourceApiCall;
33use Apie\TextValueObjects\CompanyName;
34use Apie\TextValueObjects\FirstName;
35
36/**
37 * @codeCoverageIgnore
38 */
39trait CreatesApieBoundedContext
40{
41    /**
42     * For testing POST /user
43     */
44    public function createPostUserTestRequest(): TestRequestInterface
45    {
46        return new ValidCreateResourceApiCall(
47            new BoundedContextId('types'),
48            User::class,
49            new GetAndSetObjectField(
50                '',
51                new GetAndSetPrimitiveField('id', 'test@example.com'),
52                new GetPrimitiveField('blocked', false),
53                new GetPrimitiveField('blockedReason', null),
54                new GetAndSetPrimitiveField('phoneNumber', ' 0611223344 ', '+31611223344'),
55            ),
56        );
57    }
58
59    /**
60     * For testing GET /user/{id}
61     */
62    public function createGetUserTestRequest(): TestRequestInterface
63    {
64        // @phpstan-ignore-next-line
65        $user = (new User(UserIdentifier::fromNative('test@example.com')))->setPhoneNumber(DutchPhoneNumber::fromNative('0611223344'));
66        return new GetResourceApiCall(
67            new BoundedContextId('types'),
68            User::class,
69            'test@example.com',
70            [$user],
71            new GetAndSetObjectField(
72                '',
73                new GetPrimitiveField('id', 'test@example.com'),
74                new GetPrimitiveField('blocked', false),
75                new GetPrimitiveField('blockedReason', null),
76                new GetPrimitiveField('phoneNumber', '+31611223344'),
77            )
78        );
79    }
80
81    /**
82     * For testing GET /animal/{id}
83     */
84    public function createGetAnimalTestRequest(): TestRequestInterface
85    {
86        // @phpstan-ignore-next-line
87        $animal = new Ostrich(AnimalIdentifier::createRandom(), new FirstName('Albert'));
88        return new GetResourceApiCall(
89            new BoundedContextId('types'),
90            Animal::class,
91            $animal->getId()->toNative(),
92            [$animal],
93            new GetAndSetObjectField(
94                '',
95                new GetPrimitiveField('id', $animal->getId()->toNative()),
96                new GetPrimitiveField('name', 'ostrich'),
97                new GetPrimitiveField('animalName', 'Albert'),
98                new GetPrimitiveField('capableOfFlying', false),
99                new GetPrimitiveField('type', 'bird')
100            )
101        );
102    }
103
104    /**
105     * For testing POST/primitiveOnly
106     */
107    public function createPostPrimitiveOnlyTestRequest(): TestRequestInterface
108    {
109        return new ValidCreateResourceApiCall(
110            new BoundedContextId('types'),
111            PrimitiveOnly::class,
112            new GetAndSetObjectField(
113                '',
114                new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440000'),
115                new GetAndSetPrimitiveField('stringField', 'test'),
116                new GetAndSetPrimitiveField('integerField', '42', 42),
117                new GetAndSetPrimitiveField('floatingPoint', 1.5, 1.5),
118                new GetPrimitiveField('booleanField', null),
119            ),
120            discardRequestValidation: true //casting string to int is not documented in OpenAPI spec.
121        );
122    }
123
124    /**
125     * For testing POST /animal
126     */
127    public function createPostAnimalTestRequest(): TestRequestInterface
128    {
129        return new ValidCreateResourceApiCall(
130            new BoundedContextId('types'),
131            Animal::class,
132            new GetAndSetObjectField(
133                '',
134                new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440000'),
135                new GetAndSetPrimitiveField('animalName', 'George', 'George'),
136                new GetAndSetPrimitiveField('type', 'mammal'),
137                new GetAndSetPrimitiveField('name', 'human'),
138                new GetPrimitiveField('capableOfLayingEggs', false),
139                new GetAndSetPrimitiveField('lastName', 'Pig'),
140            ),
141        );
142    }
143
144    /**
145     * For testing POST /UploadedFile
146     */
147    public function createPostUploadedFileTestRequest(): TestRequestInterface
148    {
149        return new ValidCreateResourceApiCall(
150            new BoundedContextId('types'),
151            UploadedFile::class,
152            new GetAndSetObjectField(
153                '',
154                new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440000'),
155                new GetAndSetUploadedFileField(
156                    'file',
157                    'first order line',
158                    'test-evil.txt',
159                    '/types/UploadedFile/550e8400-e29b-41d4-a716-446655440000/download/file'
160                ),
161                new GetAndSetUploadedFileField(
162                    'imageFile',
163                    file_get_contents(__DIR__ . '/../../fixtures/apie-logo.svg'),
164                    'apie-logo.svg',
165                    '/types/UploadedFile/550e8400-e29b-41d4-a716-446655440000/download/imageFile'
166                ),
167                new GetPrimitiveField('stream', '/types/UploadedFile/550e8400-e29b-41d4-a716-446655440000/download/stream')
168            ),
169        );
170    }
171
172    /**
173     * For testing POST /Order
174     */
175    public function createPostOrderTestRequest(): TestRequestInterface
176    {
177        return new ValidCreateResourceApiCall(
178            new BoundedContextId('types'),
179            Order::class,
180            new GetAndSetObjectField(
181                '',
182                new GetPrimitiveField('id', 1),
183                new GetAndSetObjectField(
184                    'orderLineList',
185                    new GetAndSetObjectField(
186                        '0',
187                        new GetAndSetPrimitiveField('description', 'First order line'),
188                        new GetUuidField('id'),
189                    ),
190                    new GetAndSetObjectField(
191                        '1',
192                        new GetAndSetPrimitiveField('description', 'Second order line'),
193                        new GetUuidField('id'),
194                    )
195                ),
196            ),
197        );
198    }
199
200    /**
201     * Test for dropdown action for comboboxes on entity create
202     *
203     * Url POST /ObjectWithRelation/dropdown-options/userId
204     */
205    public function createPropertyOptionsTestRequest(): TestRequestInterface
206    {
207        $user = new User(UserIdentifier::fromNative('test@example.com'));
208        return new ActionMethodApiCall(
209            new BoundedContextId('types'),
210            'ObjectWithRelation/dropdown-options/userId',
211            new GetAndSetObjectField(
212                '',
213                new SetPrimitiveField('input', 'test@'),
214                new GetAndSetObjectField(
215                    '0',
216                    new GetPrimitiveField('value', 'test@example.com'),
217                    new GetPrimitiveField('displayValue', 'test@example.com'),
218                )
219            ),
220            entities: [$user],
221            discardValidationOnFaker: true
222        );
223    }
224
225    /**
226     * Test for entity with permission restrictions.
227     *
228     * POST /RestrictedEntity
229     */
230    public function createObjectWithRestrictionTestRequest(): TestRequestInterface
231    {
232        return new ValidCreateResourceApiCall(
233            new BoundedContextId('types'),
234            RestrictedEntity::class,
235            new GetAndSetObjectField(
236                '',
237                new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440000'),
238                new GetAndSetPrimitiveField('companyName', 'Company NV'),
239                new GetPrimitiveField('userId', null),
240                new GetPrimitiveField('requiredPermissions', [])
241            ),
242        );
243    }
244
245    /**
246     * Test for entity with permission restrictions.
247     *
248     * GET /RestrictedEntity
249     */
250    public function getObjectWithRestrictionTestRequest(): TestRequestInterface
251    {
252        $object = new RestrictedEntity(
253            RestrictedEntityIdentifier::fromNative('550e8400-e29b-41d4-a716-446655440000'),
254            new CompanyName('Company NV'),
255            null
256        );
257        return new GetResourceApiCall(
258            new BoundedContextId('types'),
259            RestrictedEntity::class,
260            '550e8400-e29b-41d4-a716-446655440000',
261            [$object],
262            new GetAndSetObjectField(
263                '',
264                new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440000'),
265                new GetAndSetPrimitiveField('companyName', 'Company NV'),
266                new GetPrimitiveField('userId', null),
267                new GetPrimitiveField('requiredPermissions', [])
268            ),
269            discardValidationOnFaker: true
270        );
271    }
272
273    /**
274     * Test for entity with permission restrictions.
275     *
276     * GET /RestrictedEntity/{id}
277     */
278    public function getObjectWithRestrictionDeniedTestRequest(): TestRequestInterface
279    {
280        $userId = UserIdentifier::fromNative('user@example.com');
281        $user = new User($userId);
282        $object = new RestrictedEntity(
283            RestrictedEntityIdentifier::fromNative('550e8400-e29b-41d4-a716-446655440000'),
284            new CompanyName('Company NV'),
285            $user
286        );
287        return new GetResourceApiCallDenied(
288            new BoundedContextId('types'),
289            RestrictedEntity::class,
290            '550e8400-e29b-41d4-a716-446655440000',
291            [$object, $user],
292            new GetAndSetObjectField(
293                '',
294                new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440000'),
295                new GetAndSetPrimitiveField('companyName', 'Company NV'),
296                new GetPrimitiveField('userId', 'user@example.com'),
297                new GetPrimitiveField('requiredPermissions', ['useratexampledotcom:read', 'useratexampledotcom:write'])
298            ),
299            discardValidationOnFaker: true
300        );
301    }
302
303    /**
304     * Test for entity list with permission restrictions.
305     *
306     * GET /RestrictedEntity/
307     */
308    public function getObjectWithRestrictionListTestRequest(): TestRequestInterface
309    {
310        $userId = UserIdentifier::fromNative('user@example.com');
311        $user = new User($userId);
312        $object = new RestrictedEntity(
313            RestrictedEntityIdentifier::fromNative('550e8400-e29b-41d4-a716-446655440000'),
314            new CompanyName('Company NV'),
315            $user
316        );
317        $object2 = new RestrictedEntity(
318            RestrictedEntityIdentifier::fromNative('550e8400-e29b-41d4-a716-446655440001'),
319            new CompanyName('Company NV 2'),
320            null
321        );
322        return new GetResourceListApiCall(
323            new BoundedContextId('types'),
324            RestrictedEntity::class,
325            [$object, $object2, $user],
326            new GetAndSetObjectField(
327                '',
328                new GetPrimitiveField('totalCount', 1),
329                new GetPrimitiveField('filteredCount', 1),
330                new GetPrimitiveField('first', '/types/RestrictedEntity'),
331                new GetPrimitiveField('last', '/types/RestrictedEntity'),
332                new GetAndSetObjectField(
333                    'list',
334                    new GetAndSetObjectField(
335                        '0',
336                        new GetAndSetPrimitiveField('id', '550e8400-e29b-41d4-a716-446655440001'),
337                        new GetAndSetPrimitiveField('companyName', 'Company NV 2'),
338                        new GetPrimitiveField('requiredPermissions', []),
339                        new GetPrimitiveField('userId', null),
340                    )
341                ),
342            ),
343            discardValidationOnFaker: true
344        );
345    }
346
347    /**
348     * Test for invalid property for comboboxes on entity create
349     *
350     * Url POST /ObjectWithRelation/dropdown-options/unknown
351     */
352    public function createInvalidPropertyOptionsTestRequest(): TestRequestInterface
353    {
354        return new ActionMethodApiCall(
355            new BoundedContextId('types'),
356            'ObjectWithRelation/dropdown-options/unknown',
357            new GetAndSetObjectField(
358                '',
359                new SetPrimitiveField('input', 'test@'),
360            ),
361            entities: [],
362        );
363    }
364
365    /**
366     * Test for dropdown action for comboboxes on action method call.
367     */
368    public function createMethodArgumentOptionsTestRequest(): TestRequestInterface
369    {
370        $user = new User(UserIdentifier::fromNative('test@example.com'));
371        return new ActionMethodApiCall(
372            new BoundedContextId('types'),
373            'action/Authentication/isThisMe/dropdown-options/userId',
374            new GetAndSetObjectField(
375                '',
376                new SetPrimitiveField('input', 'test@'),
377                new GetAndSetObjectField(
378                    '0',
379                    new GetPrimitiveField('value', 'test@example.com'),
380                    new GetPrimitiveField('displayValue', 'test@example.com'),
381                )
382            ),
383            entities: [$user],
384            discardValidationOnFaker: true
385        );
386    }
387
388    /**
389     * For cms action /user/{id}/block
390     */
391    public function createBlockUserFormSubmit(): CmsFormSubmitRequest
392    {
393        return new CmsFormSubmitRequest(
394            new BoundedContextId('types'),
395            User::class,
396            'test@example.com',
397            'block',
398            [
399                new User(new UserIdentifier('test@example.com'))
400            ],
401            [
402                '_csrf' => 'string',
403                'form[blockedReason]' => 'This is a test'
404            ],
405            '/cms/types/resource/User/test@example.com'
406        );
407    }
408
409    /**
410     * For testing /calc/1/plus/12
411     */
412    public function createCustomActionRequest(): TestRequestInterface
413    {
414        return new ActionMethodApiCall(
415            new BoundedContextId('types'),
416            'calc/1/plus/12',
417            new GetPrimitiveField('', 13)
418        );
419    }
420
421    /**
422     * For testing /Calculator/sum for testing variadic request.
423     */
424    public function createVariadicActionRequest(): TestRequestInterface
425    {
426        return new ActionMethodApiCall(
427            new BoundedContextId('types'),
428            'Calculator/sum',
429            new GetAndSetPrimitiveField('', ['numbers' => [1,2,3]], 6)
430        );
431    }
432
433    public function createSimpleConsoleInteraction(): InteractiveConsoleCommand
434    {
435        return new InteractiveConsoleCommand(
436            'apie:types:primitive-only:create',
437            PrimitiveOnly::class,
438            [
439                'stringField' => [0, 'string'],
440                'integerField' => [0, 42],
441                'floatingPoint' => [0, 1.5],
442                'booleanField' => [0, 'yes'],
443                'id' => ['075433c9-ca1f-435c-be81-61bae3009521']
444            ]
445        );
446    }
447
448    public function createOrderLineInteraction(): InteractiveConsoleCommand
449    {
450        return new InteractiveConsoleCommand(
451            'apie:types:order:create',
452            Order::class,
453            [
454                'orderLineList' => ['yes', 'my order line description', 'no'],
455            ]
456        );
457    }
458
459    public function createFileUploadInteraction(): InteractiveConsoleCommand
460    {
461        return new InteractiveConsoleCommand(
462            'apie:types:uploaded-file:create',
463            UploadedFile::class,
464            [
465                'id' => ['075433c9-ca1f-435c-be81-61bae3009521'],
466                'file' => [__FILE__],
467                'imageFile' => ['1'],
468            ]
469        );
470    }
471
472    public function createPolymorphicObjectInteraction(): InteractiveConsoleCommand
473    {
474        return new InteractiveConsoleCommand(
475            'apie:types:animal:create',
476            Animal::class,
477            [
478                'id' => ['075433c9-ca1f-435c-be81-61bae3009521'],
479                'animalName' => ['Donald'],
480                'lastName' => ['Duck'],
481            ],
482            Human::class
483        );
484    }
485
486    public function createExampleBoundedContext(): BoundedContextConfig
487    {
488        $result = new BoundedContextConfig();
489        $result->addEntityNamespace(
490            new BoundedContextId('types'),
491            __DIR__ . '/../Apie/TypeDemo',
492            new EntityNamespace('Apie\\IntegrationTests\\Apie\\TypeDemo\\')
493        );
494
495        return $result;
496    }
497}