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