Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC][WIP] Expose product option information in API #7547

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,8 +3,10 @@ Sylius\Component\Product\Model\ProductOption:
xml_root_name: option
properties:
code:
exopse: true
type: string
name:
expose: true
type: string
values:
expose: true
virtual_properties:
getName:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of fetching the name with virtual property, we should expose all product option translations

serialized_name: name
Expand Up @@ -5,9 +5,8 @@ Sylius\Component\Product\Model\ProductOptionValue:
code:
expose: true
type: string
value:
expose: true
type: string
virtual_properties:
getName:
serialized_name: name
getValue:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Both virtual properties should be removed and we should expose all translations

serialized_name: value
162 changes: 162 additions & 0 deletions tests/Controller/ProductOptionApiTest.php
@@ -0,0 +1,162 @@
<?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.
*/

namespace Sylius\Tests\Controller;

use Lakion\ApiTestCase\JsonApiTestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;

/**
* @author alcaeus <alcaeus@alcaeus.org>
*/
final class ProductOptionTest extends JsonApiTestCase
{
/**
* @var array
*/
private static $authorizedHeaderWithContentType = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'CONTENT_TYPE' => 'application/json',
];

/**
* @var array
*/
private static $authorizedHeaderWithAccept = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'ACCEPT' => 'application/json',
];

/**
* @test
*/
public function it_does_not_allow_to_show_product_options_list_when_access_is_denied()
{
$this->client->request('GET', '/api/v1/product-options/');

$response = $this->client->getResponse();
$this->assertResponse($response, 'authentication/access_denied_response', Response::HTTP_UNAUTHORIZED);
}

/**
* @test
*/
public function it_allows_listing_product_options()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/product_options.yml');

$this->client->request('GET', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithAccept);

$response = $this->client->getResponse();
$this->assertResponse($response, 'product_option/index_response', Response::HTTP_OK);
}

/**
* @test
*/
public function it_does_not_allow_delete_product_option_if_it_does_not_exist()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');

$this->client->request('DELETE', '/api/v1/product-options/-1', [], [], static::$authorizedHeaderWithAccept);

$response = $this->client->getResponse();

$this->assertResponse($response, 'error/not_found_response', Response::HTTP_NOT_FOUND);
}

/**
* @test
*/
public function it_allows_delete_product_option()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$productOptions = $this->loadFixturesFromFile('resources/product_options.yml');

$this->client->request('DELETE', '/api/v1/product-options/'.$productOptions['productOption1']->getId(), [], [], static::$authorizedHeaderWithContentType, []);

$response = $this->client->getResponse();
$this->assertResponseCode($response, Response::HTTP_NO_CONTENT);

$this->client->request('GET', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithAccept);

$response = $this->client->getResponse();
$this->assertResponse($response, 'product_option/index_response_after_delete', Response::HTTP_OK);
}

/**
* @test
*/
public function it_allows_create_product_option_with_multiple_translations()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/locales.yml');

$data =
<<<EOT
{
"code": "MUG_SIZE",
"translations": {
"de_CH": {
"name": "Bechergröße"
},
"en_US": {
"name": "Mug size"
}
},
"values": [
{
"code": "MUG_SIZE_SMALL",
"translations": {
"de_CH": {
"value": "Klein"
},
"en_US": {
"value": "Small"
}
}
},
{
"code": "MUG_SIZE_LARGE",
"translations": {
"de_CH": {
"value": "Groß"
},
"en_US": {
"value": "Large"
}
}
}
]
}
EOT;

$this->client->request('POST', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithContentType, $data);

$response = $this->client->getResponse();
$this->assertResponse($response, 'product_option/create_response', Response::HTTP_CREATED);
}

/**
* @test
*/
public function it_does_not_allow_to_create_product_option_without_required_fields()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');

$this->client->request('POST', '/api/v1/product-options/', [], [], static::$authorizedHeaderWithContentType, []);

$response = $this->client->getResponse();
$this->assertResponse($response, 'product_option/create_validation_fail_response', Response::HTTP_BAD_REQUEST);
}
}
Expand Up @@ -6,10 +6,12 @@
"variants": [],
"options": [
{
"code": "MUG_SIZE"
"code": "MUG_SIZE",
"values": []
},
{
"code": "MUG_COLOR"
"code": "MUG_COLOR",
"values": []
}
],
"associations": [],
Expand Down
16 changes: 16 additions & 0 deletions tests/Responses/Expected/product_option/create_response.json
@@ -0,0 +1,16 @@
{
"name": "Mug size",
"code": "MUG_SIZE",
"values": [
{
"name": "Mug size",
"value": "Small",
"code": "MUG_SIZE_SMALL"
},
{
"name": "Mug size",
"value": "Large",
"code": "MUG_SIZE_LARGE"
}
]
}
@@ -0,0 +1,19 @@
{
"code": 400,
"message": "Validation Failed",
"errors": {
"errors": [
"Please add at least 2 option values."
],
"children": {
"position": {},
"translations": {},
"values": {},
"code": {
"errors": [
"Please enter option code."
]
}
}
}
}
29 changes: 29 additions & 0 deletions tests/Responses/Expected/product_option/index_response.json
@@ -0,0 +1,29 @@
{
"page": 1,
"limit": 10,
"pages": 1,
"total": 2,
"_links": {
"self": {
"href": "\/api\/v1\/product-options\/?page=1&limit=10"
},
"first": {
"href": "\/api\/v1\/product-options\/?page=1&limit=10"
},
"last": {
"href": "\/api\/v1\/product-options\/?page=1&limit=10"
}
},
"_embedded": {
"items": [
{
"code": "MUG_SIZE",
"values": []
},
{
"code": "MUG_COLOR",
"values": []
}
]
}
}
@@ -0,0 +1,25 @@
{
"page": 1,
"limit": 10,
"pages": 1,
"total": 1,
"_links": {
"self": {
"href": "\/api\/v1\/product-options\/?page=1&limit=10"
},
"first": {
"href": "\/api\/v1\/product-options\/?page=1&limit=10"
},
"last": {
"href": "\/api\/v1\/product-options\/?page=1&limit=10"
}
},
"_embedded": {
"items": [
{
"code": "MUG_COLOR",
"values": []
}
]
}
}