[API] ProductAssociationType endpoint created

This commit is contained in:
TheMilek 2022-05-11 14:13:12 +02:00
parent ee9116e81e
commit c2f10c81ac
No known key found for this signature in database
GPG key ID: FCAC2B762BB5D833
10 changed files with 399 additions and 9 deletions

View file

@ -16,46 +16,63 @@
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd"
>
<resource class="%sylius.model.product_association_type.class%" shortName="ProductAssociationType">
<attribute name="route_prefix">admin</attribute>
<attribute name="normalization_context">
<attribute name="groups">
<attribute>admin:product_association_type:read</attribute>
</attribute>
</attribute>
<attribute name="validation_groups">sylius</attribute>
<collectionOperations>
<collectionOperation name="admin_get">
<attribute name="method">GET</attribute>
<attribute name="path">/admin/product-association-types</attribute>
<attribute name="filters">
<attribute>sylius.api.product_association_type_filter</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:product_association_type:read</attribute>
</attribute>
</collectionOperation>
<collectionOperation name="admin_post">
<attribute name="method">POST</attribute>
<attribute name="path">/admin/product-association-types</attribute>
<attribute name="denormalization_context">
<attribute name="groups">admin:product_association_type:create</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:product_association_type:read</attribute>
</attribute>
</collectionOperation>
</collectionOperations>
<itemOperations>
<itemOperation name="admin_get">
<attribute name="method">GET</attribute>
<attribute name="path">/admin/product-association-types/{code}</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:product_association_type:read</attribute>
</attribute>
</itemOperation>
<itemOperation name="shop_get">
<attribute name="method">GET</attribute>
<attribute name="path">/shop/product-association-types/{code}</attribute>
<attribute name="normalization_context">
<attribute name="groups">shop:product_association_type:read</attribute>
</attribute>
</itemOperation>
<itemOperation name="admin_put">
<attribute name="method">PUT</attribute>
<attribute name="path">/admin/product-association-types/{code}</attribute>
<attribute name="denormalization_context">
<attribute name="groups">admin:product_association_type:update</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:product_association_type:read</attribute>
</attribute>
</itemOperation>
<itemOperation name="admin_delete">
<attribute name="method">DELETE</attribute>
<attribute name="path">/admin/product-association-types/{code}</attribute>
</itemOperation>
</itemOperations>

View file

@ -18,19 +18,24 @@
<class name="Sylius\Component\Product\Model\ProductAssociationType">
<attribute name="id">
<group>admin:product_association_type:read</group>
<group>shop:product_association_type:read</group>
</attribute>
<attribute name="code">
<group>admin:product_association_type:read</group>
<group>admin:product_association_type:create</group>
<group>shop:product_association_type:read</group>
</attribute>
<attribute name="name">
<group>admin:product_association_type:read</group>
<group>shop:product_association_type:read</group>
</attribute>
<attribute name="createdAt">
<group>admin:product_association_type:read</group>
<group>shop:product_association_type:read</group>
</attribute>
<attribute name="updatedAt">
<group>admin:product_association_type:read</group>
<group>shop:product_association_type:read</group>
</attribute>
<attribute name="translations">
<group>admin:product_association_type:read</group>

View file

@ -0,0 +1,164 @@
<?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\Product\Model\ProductAssociationTypeInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Sylius\Tests\Api\Utils\AdminUserLoginTrait;
use Symfony\Component\HttpFoundation\Response;
final class ProductAssociationTypesTest extends JsonApiTestCase
{
use AdminUserLoginTrait;
/** @test */
public function it_gets_product_association_type(): void
{
$fixtures = $this->loadFixturesFromFiles(['product/product_with_many_locales.yaml', 'authentication/api_administrator.yaml']);
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$container->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
$header = array_merge($header, self::CONTENT_TYPE_HEADER);
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $fixtures['product_association_type'];
$this->client->request('GET',
sprintf('/api/v2/admin/product-association-types/%s', $associationType->getCode()),
[],
[],
$header
);
$this->assertResponse(
$this->client->getResponse(),
'admin/get_product_association_type_response',
Response::HTTP_OK
);
}
/** @test */
public function it_returns_nothing_if_association_type_not_found(): void
{
$this->loadFixturesFromFiles(['product/product_with_many_locales.yaml', 'authentication/api_administrator.yaml']);
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$container->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
$header = array_merge($header, self::CONTENT_TYPE_HEADER);
$this->client->request('GET',
'/api/v2/admin/product-association-types/wrong input',
[],
[],
$header
);
$response = $this->client->getResponse();
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
}
/** @test */
public function it_returns_product_association_type_collection(): void
{
$this->loadFixturesFromFiles(['product/product_with_many_locales.yaml', 'authentication/api_administrator.yaml']);
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$container->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
$header = array_merge($header, self::CONTENT_TYPE_HEADER);
$this->client->request('GET',
'/api/v2/admin/product-association-types',
[],
[],
$header
);
$this->assertResponse(
$this->client->getResponse(),
'admin/get_product_association_type_collection_response',
Response::HTTP_OK
);
}
/** @test */
public function it_creates_product_association_type(): void
{
$this->loadFixturesFromFiles(['product/product_with_many_locales.yaml', 'authentication/api_administrator.yaml']);
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$container->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
$header = array_merge($header, self::CONTENT_TYPE_HEADER);
$this->client->request(
'POST',
'/api/v2/admin/product-association-types',
[],
[],
$header,
json_encode([
'code' => 'TEST',
'translations' => ['en_US' => [
'name' => 'test',
'description' => 'test description',
'locale' => 'en_US'
]]
], JSON_THROW_ON_ERROR)
);
$this->assertResponse(
$this->client->getResponse(),
'admin/post_product_association_type_response',
Response::HTTP_CREATED
);
}
/** @test */
public function it_updates_product_association_type(): void
{
$fixtures = $this->loadFixturesFromFiles(['product/product_with_many_locales.yaml', 'authentication/api_administrator.yaml']);
$token = $this->logInAdminUser('api@example.com');
$authorizationHeader = self::$container->getParameter('sylius.api.authorization_header');
$header['HTTP_' . $authorizationHeader] = 'Bearer ' . $token;
$header = array_merge($header, self::CONTENT_TYPE_HEADER);
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $fixtures['product_association_type'];
$this->client->request(
'PUT',
sprintf('/api/v2/admin/product-association-types/%s', $associationType->getCode()),
[],
[],
$header,
json_encode([
'code' => 'TEST',
'translations' => ['en_US' => [
'name' => 'test',
'description' => 'test description',
'locale' => 'de_DE'
]]
], JSON_THROW_ON_ERROR)
);
$this->assertResponse(
$this->client->getResponse(),
'admin/put_product_association_type_response',
Response::HTTP_OK
);
}
}

View file

@ -33,6 +33,12 @@ Sylius\Component\Core\Model\Product:
pl_PL: '@product_translation_mug_pl_PL'
de_DE: '@product_translation_mug_de_DE'
options: ['@product_option_color']
product_cup:
code: 'CUP'
channels: [ '@channel_web' ]
currentLocale: 'en_US'
translations:
en_US: '@product_translation_cup_en_US'
Sylius\Component\Core\Model\ProductTranslation:
product_translation_mug_en_US:
@ -55,7 +61,14 @@ Sylius\Component\Core\Model\ProductTranslation:
name: 'Becher'
description: 'das ist becher'
shortDescription: 'Kurzbeschreibung der Becher'
translatable: '@product_mug'
translatable: '@product_mug'
product_translation_cup_en_US:
slug: 'Cup'
locale: 'en_US'
name: 'Cup'
description: 'Short cup description'
shortDescription: 'Cup'
translatable: '@product_cup'
Sylius\Component\Core\Model\ProductVariant:
product_variant_mug_blue:
@ -145,3 +158,21 @@ Sylius\Component\Product\Model\ProductOptionValueTranslation:
locale: 'en_US'
value: 'Red'
translatable: '@product_option_value_color_red'
Sylius\Component\Product\Model\ProductAssociation:
product_association:
type: '@product_association_type'
owner: '@product_mug'
associatedProducts: ['@product_cup']
Sylius\Component\Product\Model\ProductAssociationType:
product_association_type:
code: 'similar_products'
translations:
en_US: '@product_association_type_translation'
Sylius\Component\Product\Model\ProductAssociationTypeTranslation:
product_association_type_translation:
name: 'Similar products'
locale: 'en_US'
translatable: '@product_association_type'

View file

@ -0,0 +1,44 @@
{
"@context": "\/api\/v2\/contexts\/ProductAssociationType",
"@id": "\/api\/v2\/admin\/product-association-types",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "\/api\/v2\/admin\/product-association-types\/similar_products",
"@type": "ProductAssociationType",
"id": @integer@,
"code": "similar_products",
"name": "Similar products",
"createdAt": @string@,
"updatedAt": @string@,
"translations": {
"en_US": {
"@id": "\/api\/v2\/admin\/product-association-type-translations\/@integer@",
"@type": "ProductAssociationTypeTranslation",
"id": @integer@,
"name": "Similar products"
}
}
}
],
"hydra:totalItems": 1,
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "\/api\/v2\/admin\/product-association-types{?translations.name,code}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
"@type": "IriTemplateMapping",
"variable": "translations.name",
"property": "translations.name",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "code",
"property": "code",
"required": false
}
]
}
}

View file

@ -0,0 +1,18 @@
{
"@context": "\/api\/v2\/contexts\/ProductAssociationType",
"@id": "\/api\/v2\/admin\/product-association-types\/similar_products",
"@type": "ProductAssociationType",
"id": @integer@,
"code": "similar_products",
"name": "Similar products",
"createdAt": @string@,
"updatedAt": @string@,
"translations": {
"en_US": {
"@id": "\/api\/v2\/admin\/product-association-type-translations\/@integer@",
"@type": "ProductAssociationTypeTranslation",
"id": @integer@,
"name": "Similar products"
}
}
}

View file

@ -0,0 +1,18 @@
{
"@context": "\/api\/v2\/contexts\/ProductAssociationType",
"@id": "\/api\/v2\/admin\/product-association-types\/TEST",
"@type": "ProductAssociationType",
"id": @integer@,
"code": "TEST",
"name": "test",
"createdAt": "@string@",
"updatedAt": "@string@",
"translations": {
"en_US": {
"@id": "\/api\/v2\/admin\/product-association-type-translations\/@integer@",
"@type": "ProductAssociationTypeTranslation",
"id": @integer@,
"name": "test"
}
}
}

View file

@ -0,0 +1,24 @@
{
"@context": "\/api\/v2\/contexts\/ProductAssociationType",
"@id": "\/api\/v2\/admin\/product-association-types\/similar_products",
"@type": "ProductAssociationType",
"id": @integer@,
"code": "similar_products",
"name": null,
"createdAt": "@string@",
"updatedAt": "@string@",
"translations": {
"de_DE": {
"@id": "\/api\/v2\/admin\/product-association-type-translations\/@integer@",
"@type": "ProductAssociationTypeTranslation",
"id": @integer@,
"name": "test"
},
"en": {
"@id": "\/api\/v2\/admin\/product-association-type-translations\/",
"@type": "ProductAssociationTypeTranslation",
"id": null,
"name": null
}
}
}

View file

@ -0,0 +1,10 @@
{
"@context": "\/api\/v2\/contexts\/ProductAssociationType",
"@id": "\/api\/v2\/shop\/product-association-types\/similar_products",
"@type": "ProductAssociationType",
"id": @integer@,
"code": "similar_products",
"name": "Similar products",
"createdAt": @string@,
"updatedAt": @string@
}

View file

@ -0,0 +1,59 @@
<?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\Shop;
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
use Sylius\Tests\Api\JsonApiTestCase;
use Symfony\Component\HttpFoundation\Response;
final class ProductAssociationTypesTest extends JsonApiTestCase
{
/** @test */
public function it_gets_product_association_type(): void
{
$fixtures = $this->loadFixturesFromFile('product/product_with_many_locales.yaml');
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $fixtures['product_association_type'];
$this->client->request('GET',
sprintf('/api/v2/shop/product-association-types/%s', $associationType->getCode()),
[],
[],
self::CONTENT_TYPE_HEADER
);
$this->assertResponse(
$this->client->getResponse(),
'shop/product/get_product_association_type_response',
Response::HTTP_OK
);
}
/** @test */
public function it_returns_nothing_if_association_type_not_found(): void
{
$this->loadFixturesFromFile('product/product_with_many_locales.yaml');
$this->client->request('GET',
sprintf('/api/v2/shop/product-association-types/%s', 'wrong input'),
[],
[],
self::CONTENT_TYPE_HEADER
);
$response = $this->client->getResponse();
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
}
}