|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 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 | +use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait; |
| 15 | +use Illuminate\Contracts\Config\Repository; |
| 16 | +use Illuminate\Foundation\Application; |
| 17 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 18 | +use Illuminate\Support\Str; |
| 19 | +use Orchestra\Testbench\Concerns\WithWorkbench; |
| 20 | +use Orchestra\Testbench\TestCase; |
| 21 | + |
| 22 | +class SnakeCaseApiTest extends TestCase |
| 23 | +{ |
| 24 | + use ApiTestAssertionsTrait; |
| 25 | + use RefreshDatabase; |
| 26 | + use WithWorkbench; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param Application $app |
| 30 | + */ |
| 31 | + protected function defineEnvironment($app): void |
| 32 | + { |
| 33 | + tap($app['config'], function (Repository $config): void { |
| 34 | + $config->set('api-platform.name_converter', null); |
| 35 | + $config->set('api-platform.formats', ['jsonld' => ['application/ld+json']]); |
| 36 | + $config->set('api-platform.docs_formats', ['jsonld' => ['application/ld+json']]); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + public function testRelationIsHandledOnCreateWithNestedDataSnakeCase(): void |
| 41 | + { |
| 42 | + $cartData = [ |
| 43 | + 'product_sku' => 'SKU_TEST_001', |
| 44 | + 'quantity' => 2, |
| 45 | + 'price_at_addition' => '19.99', |
| 46 | + 'shopping_cart' => [ |
| 47 | + 'user_identifier' => 'user-'.Str::uuid()->toString(), |
| 48 | + 'status' => 'active', |
| 49 | + ], |
| 50 | + ]; |
| 51 | + |
| 52 | + $response = $this->postJson('/api/cart_items', $cartData, ['accept' => 'application/ld+json', 'content-type' => 'application/ld+json']); |
| 53 | + $response->assertStatus(201); |
| 54 | + |
| 55 | + $response |
| 56 | + ->assertJson([ |
| 57 | + '@context' => '/api/contexts/CartItem', |
| 58 | + '@id' => '/api/cart_items/1', |
| 59 | + '@type' => 'CartItem', |
| 60 | + 'id' => 1, |
| 61 | + 'product_sku' => 'SKU_TEST_001', |
| 62 | + 'quantity' => 2, |
| 63 | + 'price_at_addition' => 19.99, |
| 64 | + 'shopping_cart' => [ |
| 65 | + '@id' => '/api/shopping_carts/1', |
| 66 | + '@type' => 'ShoppingCart', |
| 67 | + 'user_identifier' => $cartData['shopping_cart']['user_identifier'], |
| 68 | + 'status' => 'active', |
| 69 | + ], |
| 70 | + ]); |
| 71 | + } |
| 72 | +} |
0 commit comments