• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15775135891

20 Jun 2025 08:42AM UTC coverage: 22.065% (+0.2%) from 21.876%
15775135891

push

github

soyuka
Merge 4.1

13 of 103 new or added lines in 10 files covered. (12.62%)

868 existing lines in 35 files now uncovered.

11487 of 52060 relevant lines covered (22.06%)

21.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Laravel/Tests/EloquentTest.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Laravel\Tests;
15

16
use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
17
use ApiPlatform\Laravel\workbench\app\Enums\BookStatus;
18
use Illuminate\Foundation\Testing\RefreshDatabase;
19
use Illuminate\Support\Str;
20
use Orchestra\Testbench\Concerns\WithWorkbench;
21
use Orchestra\Testbench\TestCase;
22
use Workbench\App\Models\PostWithMorphMany;
23
use Workbench\Database\Factories\AuthorFactory;
24
use Workbench\Database\Factories\BookFactory;
25
use Workbench\Database\Factories\CommentMorphFactory;
26
use Workbench\Database\Factories\GrandSonFactory;
27
use Workbench\Database\Factories\PostWithMorphManyFactory;
28
use Workbench\Database\Factories\WithAccessorFactory;
29

30
class EloquentTest extends TestCase
31
{
32
    use ApiTestAssertionsTrait;
33
    use RefreshDatabase;
34
    use WithWorkbench;
35

36
    public function testBackedEnumsNormalization(): void
37
    {
38
        BookFactory::new([
×
39
            'status' => BookStatus::DRAFT,
×
40
        ])->has(AuthorFactory::new())->count(10)->create();
×
41

42
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
43
        $book = $response->json()['member'][0];
×
44

45
        $this->assertArrayHasKey('status', $book);
×
46
        $this->assertSame('DRAFT', $book['status']);
×
47
    }
48

49
    public function testSearchFilter(): void
50
    {
51
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
52

53
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
54
        $book = $response->json()['member'][0];
×
55

56
        $response = $this->get('/api/books?isbn='.$book['isbn'], ['Accept' => ['application/ld+json']]);
×
57
        $this->assertSame($response->json()['member'][0], $book);
×
58
    }
59

60
    public function testValidateSearchFilter(): void
61
    {
62
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
63

64
        $response = $this->get('/api/books?isbn=a', ['Accept' => ['application/ld+json']]);
×
65
        $this->assertSame($response->json()['detail'], 'The isbn field must be at least 2 characters.');
×
66
    }
67

68
    public function testSearchFilterRelation(): void
69
    {
70
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
71

72
        $response = $this->get('/api/books?author=1', ['Accept' => ['application/ld+json']]);
×
73
        $this->assertSame($response->json()['member'][0]['author'], '/api/authors/1');
×
74
    }
75

76
    public function testPropertyFilter(): void
77
    {
78
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
79

80
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
81
        $book = $response->json()['member'][0];
×
82

83
        $response = $this->get(\sprintf('%s.jsonld?properties[]=author', $book['@id']));
×
84
        $book = $response->json();
×
85

86
        $this->assertArrayHasKey('@id', $book);
×
87
        $this->assertArrayHasKey('author', $book);
×
88
        $this->assertArrayNotHasKey('name', $book);
×
89
    }
90

91
    public function testPartialSearchFilter(): void
92
    {
93
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
94

95
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
96
        $book = $response->json()['member'][0];
×
97

98
        if (!isset($book['name'])) {
×
99
            throw new \UnexpectedValueException();
×
100
        }
101

102
        $end = strpos($book['name'], ' ') ?: 3;
×
103
        $name = substr($book['name'], 0, $end);
×
104

105
        $response = $this->get('/api/books?name='.$name, ['Accept' => ['application/ld+json']]);
×
106
        $this->assertSame($response->json()['member'][0], $book);
×
107
    }
108

109
    public function testDateFilterEqual(): void
110
    {
111
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
112

113
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
114
        $book = $response->json()['member'][0];
×
115
        $updated = $this->patchJson(
×
116
            $book['@id'],
×
117
            ['publicationDate' => '2024-02-18 00:00:00'],
×
118
            [
×
119
                'Accept' => ['application/ld+json'],
×
120
                'Content-Type' => ['application/merge-patch+json'],
×
121
            ]
×
122
        );
×
123

124
        $response = $this->get('/api/books?publicationDate[eq]='.$updated['publicationDate'], ['Accept' => ['application/ld+json']]);
×
125
        $this->assertSame($response->json()['member'][0]['@id'], $book['@id']);
×
126
    }
127

128
    public function testDateFilterIncludeNull(): void
129
    {
130
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
131

132
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
133
        $book = $response->json()['member'][0];
×
134
        $updated = $this->patchJson(
×
135
            $book['@id'],
×
136
            ['publicationDate' => null],
×
137
            [
×
138
                'Accept' => ['application/ld+json'],
×
139
                'Content-Type' => ['application/merge-patch+json'],
×
140
            ]
×
141
        );
×
142

143
        $response = $this->get('/api/books?publicationWithNulls[gt]=9999-12-31', ['Accept' => ['application/ld+json']]);
×
144
        $this->assertGreaterThan(0, $response->json()['totalItems']);
×
145
    }
146

147
    public function testDateFilterExcludeNull(): void
148
    {
149
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
150

151
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
152
        $book = $response->json()['member'][0];
×
153
        $updated = $this->patchJson(
×
154
            $book['@id'],
×
155
            ['publicationDate' => null],
×
156
            [
×
157
                'Accept' => ['application/ld+json'],
×
158
                'Content-Type' => ['application/merge-patch+json'],
×
159
            ]
×
160
        );
×
161

162
        $response = $this->get('/api/books?publicationDate[gt]=9999-12-31', ['Accept' => ['application/ld+json']]);
×
163
        $this->assertSame(0, $response->json()['totalItems']);
×
164
    }
165

166
    public function testDateFilterGreaterThan(): void
167
    {
168
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
169

170
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
171
        $bookBefore = $response->json()['member'][0];
×
172
        $updated = $this->patchJson(
×
173
            $bookBefore['@id'],
×
174
            ['publicationDate' => '9998-02-18 00:00:00'],
×
175
            [
×
176
                'Accept' => ['application/ld+json'],
×
177
                'Content-Type' => ['application/merge-patch+json'],
×
178
            ]
×
179
        );
×
180

181
        $bookAfter = $response->json()['member'][1];
×
182
        $this->patchJson(
×
183
            $bookAfter['@id'],
×
184
            ['publicationDate' => '9999-02-18 00:00:00'],
×
185
            [
×
186
                'Accept' => ['application/ld+json'],
×
187
                'Content-Type' => ['application/merge-patch+json'],
×
188
            ]
×
189
        );
×
190

191
        $response = $this->get('/api/books?publicationDate[gt]='.$updated['publicationDate'], ['Accept' => ['application/ld+json']]);
×
192
        $this->assertSame($response->json()['member'][0]['@id'], $bookAfter['@id']);
×
193
        $this->assertSame($response->json()['totalItems'], 1);
×
194
    }
195

196
    public function testDateFilterLowerThanEqual(): void
197
    {
198
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
199
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
200
        $bookBefore = $response->json()['member'][0];
×
201
        $this->patchJson(
×
202
            $bookBefore['@id'],
×
203
            ['publicationDate' => '0001-02-18 00:00:00'],
×
204
            [
×
205
                'Accept' => ['application/ld+json'],
×
206
                'Content-Type' => ['application/merge-patch+json'],
×
207
            ]
×
208
        );
×
209

210
        $bookAfter = $response->json()['member'][1];
×
211
        $this->patchJson(
×
212
            $bookAfter['@id'],
×
213
            ['publicationDate' => '0002-02-18 00:00:00'],
×
214
            [
×
215
                'Accept' => ['application/ld+json'],
×
216
                'Content-Type' => ['application/merge-patch+json'],
×
217
            ]
×
218
        );
×
219

220
        $response = $this->get('/api/books?publicationDate[lte]=0002-02-18', ['Accept' => ['application/ld+json']]);
×
221
        $this->assertSame($response->json()['member'][0]['@id'], $bookBefore['@id']);
×
222
        $this->assertSame($response->json()['member'][1]['@id'], $bookAfter['@id']);
×
223
        $this->assertSame($response->json()['totalItems'], 2);
×
224
    }
225

226
    public function testDateFilterBetween(): void
227
    {
228
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
229
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
230
        $book = $response->json()['member'][0];
×
231
        $updated = $this->patchJson(
×
232
            $book['@id'],
×
233
            ['publicationDate' => '0001-02-18 00:00:00'],
×
234
            [
×
235
                'Accept' => ['application/ld+json'],
×
236
                'Content-Type' => ['application/merge-patch+json'],
×
237
            ]
×
238
        );
×
239

240
        $book2 = $response->json()['member'][1];
×
241
        $this->patchJson(
×
242
            $book2['@id'],
×
243
            ['publicationDate' => '0002-02-18 00:00:00'],
×
244
            [
×
245
                'Accept' => ['application/ld+json'],
×
246
                'Content-Type' => ['application/merge-patch+json'],
×
247
            ]
×
248
        );
×
249

250
        $book3 = $response->json()['member'][2];
×
251
        $updated3 = $this->patchJson(
×
252
            $book3['@id'],
×
253
            ['publicationDate' => '0003-02-18 00:00:00'],
×
254
            [
×
255
                'Accept' => ['application/ld+json'],
×
256
                'Content-Type' => ['application/merge-patch+json'],
×
257
            ]
×
258
        );
×
259

260
        $response = $this->get('/api/books?publicationDate[gte]='.substr($updated['publicationDate'], 0, 10).'&publicationDate[lt]='.substr($updated3['publicationDate'], 0, 10), ['Accept' => ['application/ld+json']]);
×
261
        $this->assertSame($response->json()['member'][0]['@id'], $book['@id']);
×
262
        $this->assertSame($response->json()['member'][1]['@id'], $book2['@id']);
×
263
        $this->assertSame($response->json()['totalItems'], 2);
×
264
    }
265

266
    public function testSearchFilterWithPropertyPlaceholder(): void
267
    {
268
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
269
        $response = $this->get('/api/authors', ['Accept' => ['application/ld+json']])->json();
×
270
        $author = $response['member'][0];
×
271

272
        $test = $this->get('/api/authors?name='.explode(' ', $author['name'])[0], ['Accept' => ['application/ld+json']])->json();
×
273
        $this->assertSame($test['member'][0]['id'], $author['id']);
×
274

275
        $test = $this->get('/api/authors?id='.$author['id'], ['Accept' => ['application/ld+json']])->json();
×
276
        $this->assertSame($test['member'][0]['id'], $author['id']);
×
277
    }
278

279
    public function testOrderFilterWithPropertyPlaceholder(): void
280
    {
281
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
282
        $res = $this->get('/api/authors?order[id]=desc', ['Accept' => ['application/ld+json']])->json();
×
283
        $this->assertSame($res['member'][0]['id'], 10);
×
284
    }
285

286
    public function testOrFilter(): void
287
    {
288
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
289
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']])->json()['member'];
×
290
        $book = $response[0];
×
291
        $book2 = $response[1];
×
292

293
        $res = $this->get(\sprintf('/api/books?name2[]=%s&name2[]=%s', $book['name'], $book2['name']), ['Accept' => ['application/ld+json']])->json();
×
294
        $this->assertSame($res['totalItems'], 2);
×
295
    }
296

297
    public function testRangeLowerThanFilter(): void
298
    {
299
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
300
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
301
        $bookBefore = $response->json()['member'][0];
×
302
        $this->patchJson(
×
303
            $bookBefore['@id'],
×
304
            ['isbn' => '12'],
×
305
            [
×
306
                'Accept' => ['application/ld+json'],
×
307
                'Content-Type' => ['application/merge-patch+json'],
×
308
            ]
×
309
        );
×
310

311
        $bookAfter = $response->json()['member'][1];
×
312
        $updated = $this->patchJson(
×
313
            $bookAfter['@id'],
×
314
            ['isbn' => '15'],
×
315
            [
×
316
                'Accept' => ['application/ld+json'],
×
317
                'Content-Type' => ['application/merge-patch+json'],
×
318
            ]
×
319
        );
×
320

321
        $response = $this->get('api/books?isbn_range[lt]='.$updated['isbn'], ['Accept' => ['application/ld+json']]);
×
322
        $this->assertSame($response->json()['member'][0]['@id'], $bookBefore['@id']);
×
323
        $this->assertSame($response->json()['totalItems'], 1);
×
324
    }
325

326
    public function testRangeLowerThanEqualFilter(): void
327
    {
328
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
329
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
330
        $bookBefore = $response->json()['member'][0];
×
331
        $this->patchJson(
×
332
            $bookBefore['@id'],
×
333
            ['isbn' => '12'],
×
334
            [
×
335
                'Accept' => ['application/ld+json'],
×
336
                'Content-Type' => ['application/merge-patch+json'],
×
337
            ]
×
338
        );
×
339

340
        $bookAfter = $response->json()['member'][1];
×
341
        $updated = $this->patchJson(
×
342
            $bookAfter['@id'],
×
343
            ['isbn' => '15'],
×
344
            [
×
345
                'Accept' => ['application/ld+json'],
×
346
                'Content-Type' => ['application/merge-patch+json'],
×
347
            ]
×
348
        );
×
349

350
        $response = $this->get('api/books?isbn_range[lte]='.$updated['isbn'], ['Accept' => ['application/ld+json']]);
×
351
        $this->assertSame($response->json()['member'][0]['@id'], $bookBefore['@id']);
×
352
        $this->assertSame($response->json()['member'][1]['@id'], $bookAfter['@id']);
×
353
        $this->assertSame($response->json()['totalItems'], 2);
×
354
    }
355

356
    public function testRangeGreaterThanFilter(): void
357
    {
358
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
359
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
360
        $bookBefore = $response->json()['member'][0];
×
361
        $updated = $this->patchJson(
×
362
            $bookBefore['@id'],
×
363
            ['isbn' => '999999999999998'],
×
364
            [
×
365
                'Accept' => ['application/ld+json'],
×
366
                'Content-Type' => ['application/merge-patch+json'],
×
367
            ]
×
368
        );
×
369

370
        $bookAfter = $response->json()['member'][1];
×
371
        $this->patchJson(
×
372
            $bookAfter['@id'],
×
373
            ['isbn' => '999999999999999'],
×
374
            [
×
375
                'Accept' => ['application/ld+json'],
×
376
                'Content-Type' => ['application/merge-patch+json'],
×
377
            ]
×
378
        );
×
379

380
        $response = $this->get('api/books?isbn_range[gt]='.$updated['isbn'], ['Accept' => ['application/ld+json']]);
×
381
        $this->assertSame($response->json()['member'][0]['@id'], $bookAfter['@id']);
×
382
        $this->assertSame($response->json()['totalItems'], 1);
×
383
    }
384

385
    public function testRangeGreaterThanEqualFilter(): void
386
    {
387
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
388
        $response = $this->get('/api/books', ['Accept' => ['application/ld+json']]);
×
389
        $bookBefore = $response->json()['member'][0];
×
390
        $updated = $this->patchJson(
×
391
            $bookBefore['@id'],
×
392
            ['isbn' => '999999999999998'],
×
393
            [
×
394
                'Accept' => ['application/ld+json'],
×
395
                'Content-Type' => ['application/merge-patch+json'],
×
396
            ]
×
397
        );
×
398

399
        $bookAfter = $response->json()['member'][1];
×
400
        $this->patchJson(
×
401
            $bookAfter['@id'],
×
402
            ['isbn' => '999999999999999'],
×
403
            [
×
404
                'Accept' => ['application/ld+json'],
×
405
                'Content-Type' => ['application/merge-patch+json'],
×
406
            ]
×
407
        );
×
408
        $response = $this->get('api/books?isbn_range[gte]='.$updated['isbn'], ['Accept' => ['application/ld+json']]);
×
409
        $json = $response->json();
×
410
        $this->assertSame($json['member'][0]['@id'], $bookBefore['@id']);
×
411
        $this->assertSame($json['member'][1]['@id'], $bookAfter['@id']);
×
412
        $this->assertSame($json['totalItems'], 2);
×
413
    }
414

415
    public function testWrongOrderFilter(): void
416
    {
417
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
418
        $res = $this->get('/api/authors?order[name]=something', ['Accept' => ['application/ld+json']]);
×
419
        $this->assertEquals($res->getStatusCode(), 422);
×
420
    }
421

422
    public function testWithAccessor(): void
423
    {
424
        WithAccessorFactory::new()->create();
×
425
        $res = $this->get('/api/with_accessors/1', ['Accept' => ['application/ld+json']]);
×
426
        $this->assertArraySubset(['name' => 'test'], $res->json());
×
427
    }
428

429
    public function testBooleanFilter(): void
430
    {
431
        BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
×
432
        $res = $this->get('/api/books?published=notabool', ['Accept' => ['application/ld+json']]);
×
433
        $this->assertEquals($res->getStatusCode(), 422);
×
434

435
        $res = $this->get('/api/books?published=0', ['Accept' => ['application/ld+json']]);
×
436
        $this->assertEquals($res->getStatusCode(), 200);
×
437
        $this->assertEquals($res->json()['totalItems'], 0);
×
438
    }
439

440
    public function testBelongsTo(): void
441
    {
442
        GrandSonFactory::new()->count(1)->create();
×
443

444
        $res = $this->get('/api/grand_sons/1/grand_father', ['Accept' => ['application/ld+json']]);
×
445
        $json = $res->json();
×
446
        $this->assertEquals($json['@id'], '/api/grand_sons/1/grand_father');
×
447
        $this->assertEquals($json['sons'][0], '/api/grand_sons/1');
×
448
    }
449

450
    public function testHasMany(): void
451
    {
NEW
452
        GrandSonFactory::new()->count(1)->create();
×
453

NEW
454
        $res = $this->get('/api/grand_fathers/1/grand_sons', ['Accept' => ['application/ld+json']]);
×
NEW
455
        $json = $res->json();
×
NEW
456
        $this->assertEquals($json['@id'], '/api/grand_fathers/1/grand_sons');
×
NEW
457
        $this->assertEquals($json['totalItems'], 1);
×
NEW
458
        $this->assertEquals($json['member'][0]['@id'], '/api/grand_sons/1');
×
459
    }
460

461
    public function testRelationIsHandledOnCreateWithNestedData(): void
462
    {
463
        $cartData = [
×
464
            'productSku' => 'SKU_TEST_001',
×
465
            'quantity' => 2,
×
466
            'priceAtAddition' => '19.99',
×
467
            'shoppingCart' => [
×
468
                'userIdentifier' => 'user-'.Str::uuid()->toString(),
×
469
                'status' => 'active',
×
470
            ],
×
471
        ];
×
472

473
        $response = $this->postJson('/api/cart_items', $cartData, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
×
474
        $response->assertStatus(201);
×
475

476
        $response
×
477
            ->assertJson([
×
478
                '@context' => '/api/contexts/CartItem',
×
479
                '@id' => '/api/cart_items/1',
×
480
                '@type' => 'CartItem',
×
481
                'id' => 1,
×
482
                'productSku' => 'SKU_TEST_001',
×
483
                'quantity' => 2,
×
484
                'priceAtAddition' => 19.99,
×
485
                'shoppingCart' => [
×
486
                    '@id' => '/api/shopping_carts/1',
×
487
                    '@type' => 'ShoppingCart',
×
488
                    'userIdentifier' => $cartData['shoppingCart']['userIdentifier'],
×
489
                    'status' => 'active',
×
490
                ],
×
491
            ]);
×
492
    }
493

494
    public function testRelationIsHandledOnCreateWithNestedDataToMany(): void
495
    {
496
        $cartData = [
×
497
            'userIdentifier' => 'user-'.Str::uuid()->toString(),
×
498
            'status' => 'active',
×
499
            'cartItems' => [
×
500
                [
×
501
                    'productSku' => 'SKU_TEST_001',
×
502
                    'quantity' => 2,
×
503
                    'priceAtAddition' => '19.99',
×
504
                ],
×
505
                [
×
506
                    'productSku' => 'SKU_TEST_002',
×
507
                    'quantity' => 1,
×
508
                    'priceAtAddition' => '25.50',
×
509
                ],
×
510
            ],
×
511
        ];
×
512

513
        $response = $this->postJson('/api/shopping_carts', $cartData, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
×
514
        $response->assertStatus(201);
×
515
        $response->assertJson([
×
516
            '@context' => '/api/contexts/ShoppingCart',
×
517
            '@id' => '/api/shopping_carts/1',
×
518
            '@type' => 'ShoppingCart',
×
519
            'id' => 1,
×
520
            'userIdentifier' => $cartData['userIdentifier'],
×
521
            'status' => 'active',
×
522
            'cartItems' => [
×
523
                [
×
524
                    '@id' => '/api/cart_items/1',
×
525
                    '@type' => 'CartItem',
×
526
                    'productSku' => 'SKU_TEST_001',
×
527
                    'quantity' => 2,
×
528
                    'priceAtAddition' => '19.99',
×
529
                ],
×
530
                [
×
531
                    '@id' => '/api/cart_items/2',
×
532
                    '@type' => 'CartItem',
×
533
                    'productSku' => 'SKU_TEST_002',
×
534
                    'quantity' => 1,
×
535
                    'priceAtAddition' => '25.50',
×
536
                ],
×
537
            ],
×
538
        ]);
×
539
    }
540

541
    public function testPostWithEmptyMorphMany(): void
542
    {
UNCOV
543
        $response = $this->postJson('/api/post_with_morph_manies', [
×
UNCOV
544
            'title' => 'My first post',
×
UNCOV
545
            'content' => 'This is the content of my first post.',
×
UNCOV
546
            'comments' => [['content' => 'hello']],
×
UNCOV
547
        ], ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']);
×
UNCOV
548
        $response->assertStatus(201);
×
UNCOV
549
        $response->assertJson([
×
UNCOV
550
            'title' => 'My first post',
×
UNCOV
551
            'content' => 'This is the content of my first post.',
×
UNCOV
552
            'comments' => [['content' => 'hello']],
×
UNCOV
553
        ]);
×
554
    }
555

556
    public function testPostCommentsCollectionFromMorphMany(): void
557
    {
NEW
558
        PostWithMorphManyFactory::new()->create();
×
559

NEW
560
        CommentMorphFactory::new()->count(5)->create([
×
NEW
561
            'commentable_id' => 1,
×
NEW
562
            'commentable_type' => PostWithMorphMany::class,
×
NEW
563
        ]);
×
564

NEW
565
        $response = $this->getJson('/api/post_with_morph_manies/1/comments', [
×
NEW
566
            'accept' => 'application/ld+json',
×
NEW
567
        ]);
×
NEW
568
        $response->assertStatus(200);
×
NEW
569
        $response->assertJsonCount(5, 'member');
×
570
    }
571

572
    public function testPostCommentItemFromMorphMany(): void
573
    {
NEW
574
        PostWithMorphManyFactory::new()->create();
×
575

NEW
576
        CommentMorphFactory::new()->count(5)->create([
×
NEW
577
            'commentable_id' => 1,
×
NEW
578
            'commentable_type' => PostWithMorphMany::class,
×
NEW
579
        ])->first();
×
580

NEW
581
        $response = $this->getJson('/api/post_with_morph_manies/1/comments/1', [
×
NEW
582
            'accept' => 'application/ld+json',
×
NEW
583
        ]);
×
NEW
584
        $response->assertStatus(200);
×
NEW
585
        $response->assertJson([
×
NEW
586
            '@context' => '/api/contexts/CommentMorph',
×
NEW
587
            '@id' => '/api/post_with_morph_manies/1/comments/1',
×
NEW
588
            '@type' => 'CommentMorph',
×
NEW
589
            'id' => 1,
×
NEW
590
        ]);
×
591
    }
592
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc