[API][ShippingCategory] Update mapping

This commit is contained in:
Jan Goralski 2022-11-28 14:58:31 +01:00
parent 5a281b1b5e
commit 06127039c9
No known key found for this signature in database
GPG key ID: 95D91BA380F31EDD
8 changed files with 201 additions and 3 deletions

View file

@ -33,6 +33,9 @@
<attribute name="denormalization_context">
<attribute name="groups">admin:shipping_category:create</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:shipping_category:read</attribute>
</attribute>
</collectionOperation>
</collectionOperations>
@ -49,6 +52,9 @@
<attribute name="denormalization_context">
<attribute name="groups">admin:shipping_category:update</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:shipping_category:read</attribute>
</attribute>
</itemOperation>
<itemOperation name="admin_delete">

View file

@ -16,9 +16,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd"
>
<class name="Sylius\Component\Shipping\Model\ShippingCategory">
<attribute name="id">
<group>admin:shipping_category:read</group>
</attribute>
<attribute name="code">
<group>admin:shipping_category:read</group>
<group>admin:shipping_category:create</group>

View file

@ -0,0 +1,131 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Tests\Api\Admin;
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
use Symfony\Component\HttpFoundation\Response;
final class ShippingCategoriesTest extends JsonApiTestCase
{
use AdminUserLoginTrait;
/** @test */
public function it_gets_a_shipping_category(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'shipping_category.yaml']);
$header = $this->getLoggedHeader();
/** @var ShippingCategoryInterface $shippingCategory */
$shippingCategory = $fixtures['shipping_category_special'];
$this->client->request(
'GET',
sprintf('/api/v2/admin/shipping-categories/%s', $shippingCategory->getCode()),
[],
[],
$header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/shipping_category/get_shipping_category_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_gets_shipping_categories(): void
{
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'shipping_category.yaml']);
$header = $this->getLoggedHeader();
$this->client->request(
'GET',
'/api/v2/admin/shipping-categories',
[],
[],
$header,
);
$this->assertResponse(
$this->client->getResponse(),
'admin/shipping_category/get_shipping_categories_response',
Response::HTTP_OK,
);
}
/** @test */
public function it_creates_a_shipping_category(): void
{
$this->loadFixturesFromFiles(['authentication/api_administrator.yaml']);
$header = $this->getLoggedHeader();
$this->client->request(
'POST',
'/api/v2/admin/shipping-categories',
[],
[],
$header,
json_encode([
'code' => 'ultra',
'name' => 'Ultra',
], JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/shipping_category/post_shipping_category_response',
Response::HTTP_CREATED,
);
}
/** @test */
public function it_updates_an_existing_shipping_category(): void
{
$fixtures = $this->loadFixturesFromFiles(['authentication/api_administrator.yaml', 'shipping_category.yaml']);
/** @var ShippingCategoryInterface $shippingCategory */
$shippingCategory = $fixtures['shipping_category_default'];
$header = $this->getLoggedHeader();
$this->client->request(
'PUT',
'/api/v2/admin/shipping-categories/' . $shippingCategory->getCode(),
[],
[],
$header,
json_encode([
'name' => 'Not so default',
], JSON_THROW_ON_ERROR),
);
$this->assertResponse(
$this->client->getResponse(),
'admin/shipping_category/put_shipping_category_response',
Response::HTTP_OK,
);
}
private function getLoggedHeader(): array
{
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$kernel->getContainer()->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
return array_merge($header, self::CONTENT_TYPE_HEADER);
}
}

View file

@ -0,0 +1,8 @@
Sylius\Component\Shipping\Model\ShippingCategory:
shipping_category_default:
code: 'default'
name: 'Default'
shipping_category_special:
code: 'special'
name: 'Special'
description: 'For special care'

View file

@ -0,0 +1,26 @@
{
"@context": "\/api\/v2\/contexts\/ShippingCategory",
"@id": "\/api\/v2\/admin\/shipping-categories",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "\/api\/v2\/admin\/shipping-categories\/default",
"@type": "ShippingCategory",
"code": "default",
"name": "Default",
"description": null,
"createdAt": @date@,
"updatedAt": @date@
},
{
"@id": "\/api\/v2\/admin\/shipping-categories\/special",
"@type": "ShippingCategory",
"code": "special",
"name": "Special",
"description": "For special care",
"createdAt": @date@,
"updatedAt": @date@
}
],
"hydra:totalItems": 2
}

View file

@ -0,0 +1,10 @@
{
"@context": "\/api\/v2\/contexts\/ShippingCategory",
"@id": "\/api\/v2\/admin\/shipping-categories\/special",
"@type": "ShippingCategory",
"code": "special",
"name": "Special",
"description": "For special care",
"createdAt": @date@,
"updatedAt": @date@
}

View file

@ -0,0 +1,10 @@
{
"@context": "\/api\/v2\/contexts\/ShippingCategory",
"@id": "\/api\/v2\/admin\/shipping-categories\/ultra",
"@type": "ShippingCategory",
"code": "ultra",
"name": "Ultra",
"description": null,
"createdAt": @date@,
"updatedAt": @date@
}

View file

@ -0,0 +1,10 @@
{
"@context": "\/api\/v2\/contexts\/ShippingCategory",
"@id": "\/api\/v2\/admin\/shipping-categories\/default",
"@type": "ShippingCategory",
"code": "default",
"name": "Not so default",
"description": null,
"createdAt": @date@,
"updatedAt": @date@
}