|
16 | 16 | use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait; |
17 | 17 | use ApiPlatform\Laravel\workbench\app\Enums\BookStatus; |
18 | 18 | use Illuminate\Foundation\Testing\RefreshDatabase; |
| 19 | +use Illuminate\Support\Str; |
19 | 20 | use Orchestra\Testbench\Concerns\WithWorkbench; |
20 | 21 | use Orchestra\Testbench\TestCase; |
21 | 22 | use Workbench\Database\Factories\AuthorFactory; |
@@ -442,4 +443,84 @@ public function testBelongsTo(): void |
442 | 443 | $this->assertEquals($json['@id'], '/api/grand_sons/1/grand_father'); |
443 | 444 | $this->assertEquals($json['sons'][0], '/api/grand_sons/1'); |
444 | 445 | } |
| 446 | + |
| 447 | + public function testRelationIsHandledOnCreateWithNestedData(): void |
| 448 | + { |
| 449 | + $cartData = [ |
| 450 | + 'productSku' => 'SKU_TEST_001', |
| 451 | + 'quantity' => 2, |
| 452 | + 'priceAtAddition' => '19.99', |
| 453 | + 'shoppingCart' => [ |
| 454 | + 'userIdentifier' => 'user-'.Str::uuid()->toString(), |
| 455 | + 'status' => 'active', |
| 456 | + ], |
| 457 | + ]; |
| 458 | + |
| 459 | + $response = $this->postJson('/api/cart_items', $cartData, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']); |
| 460 | + $response->assertStatus(201); |
| 461 | + |
| 462 | + $response |
| 463 | + ->assertJson([ |
| 464 | + '@context' => '/api/contexts/CartItem', |
| 465 | + '@id' => '/api/cart_items/1', |
| 466 | + '@type' => 'CartItem', |
| 467 | + 'id' => 1, |
| 468 | + 'productSku' => 'SKU_TEST_001', |
| 469 | + 'quantity' => 2, |
| 470 | + 'priceAtAddition' => 19.99, |
| 471 | + 'shoppingCart' => [ |
| 472 | + '@id' => '/api/shopping_carts/1', |
| 473 | + '@type' => 'ShoppingCart', |
| 474 | + 'userIdentifier' => $cartData['shoppingCart']['userIdentifier'], |
| 475 | + 'status' => 'active', |
| 476 | + ], |
| 477 | + ]); |
| 478 | + } |
| 479 | + |
| 480 | + public function testRelationIsHandledOnCreateWithNestedDataToMany(): void |
| 481 | + { |
| 482 | + $cartData = [ |
| 483 | + 'userIdentifier' => 'user-'.Str::uuid()->toString(), |
| 484 | + 'status' => 'active', |
| 485 | + 'cartItems' => [ |
| 486 | + [ |
| 487 | + 'productSku' => 'SKU_TEST_001', |
| 488 | + 'quantity' => 2, |
| 489 | + 'priceAtAddition' => '19.99', |
| 490 | + ], |
| 491 | + [ |
| 492 | + 'productSku' => 'SKU_TEST_002', |
| 493 | + 'quantity' => 1, |
| 494 | + 'priceAtAddition' => '25.50', |
| 495 | + ], |
| 496 | + ], |
| 497 | + ]; |
| 498 | + |
| 499 | + $response = $this->postJson('/api/shopping_carts', $cartData, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']); |
| 500 | + $response->assertStatus(201); |
| 501 | + $response->assertJson([ |
| 502 | + '@context' => '/api/contexts/ShoppingCart', |
| 503 | + '@id' => '/api/shopping_carts/1', |
| 504 | + '@type' => 'ShoppingCart', |
| 505 | + 'id' => 1, |
| 506 | + 'userIdentifier' => $cartData['userIdentifier'], |
| 507 | + 'status' => 'active', |
| 508 | + 'cartItems' => [ |
| 509 | + [ |
| 510 | + '@id' => '/api/cart_items/1', |
| 511 | + '@type' => 'CartItem', |
| 512 | + 'productSku' => 'SKU_TEST_001', |
| 513 | + 'quantity' => 2, |
| 514 | + 'priceAtAddition' => '19.99', |
| 515 | + ], |
| 516 | + [ |
| 517 | + '@id' => '/api/cart_items/2', |
| 518 | + '@type' => 'CartItem', |
| 519 | + 'productSku' => 'SKU_TEST_002', |
| 520 | + 'quantity' => 1, |
| 521 | + 'priceAtAddition' => '25.50', |
| 522 | + ], |
| 523 | + ], |
| 524 | + ]); |
| 525 | + } |
445 | 526 | } |
0 commit comments