Skip to content

Commit

Permalink
Allow writing product options via API
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Mar 23, 2017
1 parent c537128 commit d954c9d
Show file tree
Hide file tree
Showing 10 changed files with 499 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ sylius_admin_api_product_option:
identifier: code
alias: sylius.product_option
section: admin_api
only: ['index', 'show']
serialization_version: $version
criteria:
code: $code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ Sylius\Component\Product\Model\ProductOption:
expose: true
type: array
groups: [Default, Detailed]
translations:
expose: true
type: array
groups: [Default, Detailed]
relations:
- rel: self
href:
Expand Down
248 changes: 248 additions & 0 deletions tests/Controller/ProductOptionApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
*/
final class ProductOptionApiTest extends JsonApiTestCase
{
/**
* @var array
*/
private static $authorizedHeaderWithContentType = [
'HTTP_Authorization' => 'Bearer SampleTokenNjZkNjY2MDEwMTAzMDkxMGE0OTlhYzU3NzYyMTE0ZGQ3ODcyMDAwM2EwMDZjNDI5NDlhMDdlMQ',
'CONTENT_TYPE' => 'application/json',
];

/**
* @var array
*/
Expand Down Expand Up @@ -84,6 +92,246 @@ public function it_allows_showing_product_option()
$this->assertResponse($response, 'product_option/show_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');
$productOption = $productOptions['mug-size'];

$this->client->request('DELETE', $this->getProductOptionUrl($productOption), [], [], 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_S",
"translations": {
"de_CH": {
"value": "Klein"
},
"en_US": {
"value": "Small"
}
}
},
{
"code": "MUG_SIZE_L",
"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);
}

/**
* @test
*/
public function it_allows_to_update_product_option_with_multiple_translations()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/locales.yml');
$productOptions = $this->loadFixturesFromFile('resources/product_options.yml');
$productOption = $productOptions['mug-size'];

$data = <<<EOT
{
"code": "MUG_SIZE",
"position": 2,
"translations": {
"de_CH": {
"name": "Bechergröße"
},
"en_US": {
"name": "Mug size"
}
},
"values": [
{
"code": "MUG_SIZE_S",
"translations": {
"de_CH": {
"value": "Klein"
},
"en_US": {
"value": "Small"
}
}
},
{
"code": "MUG_SIZE_L",
"translations": {
"de_CH": {
"value": "Groß"
},
"en_US": {
"value": "Large"
}
}
},
{
"code": "MUG_SIZE_M",
"translations": {
"de_CH": {
"value": "Mittel"
},
"en_US": {
"value": "Medium"
}
}
}
]
}
EOT;

$this->client->request('PUT', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithContentType, $data);

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

$this->client->request('GET', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithAccept);

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

/**
* @test
*/
public function it_allows_to_partially_update_product_option_with_multiple_translations()
{
$this->loadFixturesFromFile('authentication/api_administrator.yml');
$this->loadFixturesFromFile('resources/locales.yml');
$productOptions = $this->loadFixturesFromFile('resources/product_options.yml');
$productOption = $productOptions['mug-size'];

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

$this->client->request('PATCH', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithContentType, $data);

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

$this->client->request('GET', $this->getProductOptionUrl($productOption), [], [], static::$authorizedHeaderWithAccept);

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

/**
* @param ProductOptionInterface $productOption
*
Expand Down
54 changes: 54 additions & 0 deletions tests/Responses/Expected/product_option/create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"id": @integer@,
"code": "MUG_SIZE",
"position": 0,
"translations": {
"en_US": {
"id": @integer@,
"locale": "en_US",
"name": "Mug size"
},
"de_CH": {
"id": @integer@,
"locale": "de_CH",
"name": "Bechergröße"
}
},
"values": [
{
"code": "MUG_SIZE_S",
"translations": {
"en_US": {
"id": @integer@,
"locale": "en_US",
"value": "Small"
},
"de_CH": {
"id": @integer@,
"locale": "de_CH",
"value": "Klein"
}
}
},
{
"code": "MUG_SIZE_L",
"translations": {
"de_CH": {
"id": @integer@,
"locale": "de_CH",
"value": "Groß"
},
"en_US": {
"id": @integer@,
"locale": "en_US",
"value": "Large"
}
}
}
],
"_links": {
"self": {
"href": "\/api\/v1\/products\/MUG_SIZE"
}
}
}
Original file line number Diff line number Diff line change
@@ -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."
]
}
}
}
}
2 changes: 2 additions & 0 deletions tests/Responses/Expected/product_option/index_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"id": @integer@,
"code": "MUG_SIZE",
"position": 0,
"translations": {},
"values": [
{
"code": "MUG_SIZE_S",
Expand Down Expand Up @@ -52,6 +53,7 @@
"id": @integer@,
"code": "MUG_COLOR",
"position": 1,
"translations": {},
"values": [],
"_links": {
"self": {
Expand Down

0 comments on commit d954c9d

Please sign in to comment.