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

[API] [Product] [ProductTranslation] Add api configuration and test for adding product and product translations #11213

Merged
merged 3 commits into from
Mar 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion features/product/managing_products/adding_product.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Feature: Adding a new product
Then I should be notified that it has been successfully created
And the product "Gentleman Jack" should appear in the store

@ui
@ui @api
Scenario: Adding a new configurable product without options
Given I want to create a new configurable product
When I specify its code as "WHISKEY_GENTLEMEN"
Expand Down
121 changes: 121 additions & 0 deletions src/Sylius/Behat/Context/Api/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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\Behat\Context\Api\Admin;

use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Webmozart\Assert\Assert;

final class ManagingProductsContext implements Context
{
/** @var ApiClientInterface */
private $client;

/** @var ResponseCheckerInterface */
private $responseChecker;

public function __construct(ApiClientInterface $client, ResponseCheckerInterface $responseChecker)
{
$this->client = $client;
$this->responseChecker = $responseChecker;
}

/**
* @When I want to create a new configurable product
*/
public function iWantToCreateANewConfigurableProduct(): void
{
$this->client->buildCreateRequest();
}

/**
* @When I specify its code as :code
* @When I do not specify its code
*/
public function iSpecifyItsCodeAs(string $code = null): void
{
$this->client->addRequestData('code', $code);
}

/**
* @When I name it :name in :localeCode
* @When I rename it to :name in :localeCode
*/
public function iRenameItToIn(string $name, string $localeCode): void
{
$data = [
'translations' => [
$localeCode => [
'locale' => $localeCode,
'name' => $name,
],
],
];

$this->client->updateRequestData($data);
}

/**
* @When I set its slug to :slug
* @When I set its slug to :slug in :localeCode
* @When I remove its slug
*/
public function iSetItsSlugTo(?string $slug = null, $localeCode = 'en_US'): void
{
$data = [
'translations' => [
$localeCode => [
'slug' => $slug,
Copy link
Member

Choose a reason for hiding this comment

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

This should work now:

Suggested change
'slug' => $slug,
'locale' => $localeCode,
'slug' => $slug,

],
],
];

$this->client->updateRequestData($data);
}

/**
* @When I add it
* @When I try to add it
*/
public function iAddIt(): void
{
$this->client->create();
}

/**
* @Then I should see the product :productName in the list
* @Then the product :productName should appear in the store
* @Then the product :productName should be in the shop
* @Then this product should still be named :productName
*/
public function theProductShouldAppearInTheShop(string $productName): void
{
$this->client->index();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$this->client->index();
$response = $this->client->index();


Assert::true(
$this
->responseChecker
->hasItemWithTranslation($this->client->getLastResponse(),'en_US', 'name', $productName)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
->hasItemWithTranslation($this->client->getLastResponse(),'en_US', 'name', $productName)
->hasItemWithTranslation($response, 'en_US', 'name', $productName)

);
Copy link
Member

Choose a reason for hiding this comment

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

Missing exception message

}

/**
* @Then I should be notified that it has been successfully created
*/
public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated(): void
{
Assert::true($this->responseChecker->isCreationSuccessful($this->client->getLastResponse()));
Copy link
Member

Choose a reason for hiding this comment

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

Missing exception message

}
}
6 changes: 6 additions & 0 deletions src/Sylius/Behat/Resources/config/services/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
<argument>product_reviews</argument>
</service>

<service id="sylius.behat.api_platform_client.products" class="Sylius\Behat\Client\ApiPlatformClient">
<argument type="service" id="test.client" />
<argument type="service" id="sylius.behat.shared_storage" />
<argument>products</argument>
</service>

<service id="sylius.behat.api_platform_client.shipping_category" class="Sylius\Behat\Client\ApiPlatformClient">
<argument type="service" id="test.client" />
<argument type="service" id="sylius.behat.shared_storage" />
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Behat/Resources/config/services/contexts/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<argument type="service" id="session" />
</service>

<service id="sylius.behat.context.api.admin.managing_products" class="Sylius\Behat\Context\Api\Admin\ManagingProductsContext">
<argument type="service" id="sylius.behat.api_platform_client.products" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
</service>

<service id="sylius.behat.context.api.admin.managing_tax_categories" class="Sylius\Behat\Context\Api\Admin\ManagingTaxCategoriesContext">
<argument type="service" id="sylius.behat.api_platform_client.tax_category" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
Expand Down
1 change: 1 addition & 0 deletions src/Sylius/Behat/Resources/config/suites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ imports:
- suites/api/product/managing_product_options.yml
- suites/api/product/managing_product_reviews.yml
- suites/api/product/managing_product_variants.yml
- suites/api/product/managing_products.yml
- suites/api/shipping/managing_shipping_categories.yml
- suites/api/taxation/managing_tax_categories.yml
- suites/api/taxon/managing_taxons.yml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This file is part of the Sylius package.
# (c) Paweł Jędrzejewski

default:
suites:
api_managing_products:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.address
- sylius.behat.context.transform.admin
- sylius.behat.context.transform.channel
- sylius.behat.context.transform.currency
- sylius.behat.context.transform.customer
- sylius.behat.context.transform.lexical
- sylius.behat.context.transform.locale
- sylius.behat.context.transform.payment
- sylius.behat.context.transform.product
- sylius.behat.context.transform.product_association_type
- sylius.behat.context.transform.product_option
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.shipping_method
- sylius.behat.context.transform.taxon
- sylius.behat.context.transform.zone

- sylius.behat.context.setup.admin_api_security
- sylius.behat.context.setup.admin_user
- sylius.behat.context.setup.channel
- sylius.behat.context.setup.currency
- sylius.behat.context.setup.geographical
- sylius.behat.context.setup.locale
- sylius.behat.context.setup.order
- sylius.behat.context.setup.payment
- sylius.behat.context.setup.product
- sylius.behat.context.setup.product_association
- sylius.behat.context.setup.product_attribute
- sylius.behat.context.setup.product_option
- sylius.behat.context.setup.product_review
- sylius.behat.context.setup.product_taxon
- sylius.behat.context.setup.shipping
- sylius.behat.context.setup.shipping_category
- sylius.behat.context.setup.taxonomy
- sylius.behat.context.setup.zone

- sylius.behat.context.api.admin.managing_products

filters:
tags: "@managing_products && @api"
javascript: false
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
<attribute>product:read</attribute>
</attribute>
</attribute>
<attribute name="validation_groups">sylius</attribute>

<collectionOperations />
<collectionOperations>
<collectionOperation name="get" />
<collectionOperation name="post">
<attribute name="denormalization_context">
<attribute name="groups">product:create</attribute>
</attribute>
</collectionOperation>
</collectionOperations>

<itemOperations>
<itemOperation name="get" />
</itemOperations>

<property name="id" identifier="true" writable="false" />
<property name="id" identifier="false" writable="false" />
<property name="code" identifier="true" required="true" />
<property name="createdAt" writable="false" />
<property name="updatedAt" writable="false" />
<property name="translations" readable="true" writable="true" />
</resource>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" ?>

<resources xmlns="https://api-platform.com/schema/metadata"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://api-platform.com/schema/metadata https://api-platform.com/schema/metadata/metadata-2.0.xsd">

<resource
class="%sylius.model.product_translation.class%"
shortName="ProductTranslation"
>
<attribute name="validation_groups">sylius</attribute>

<collectionOperations />

<itemOperations>
<itemOperation name="get" />
</itemOperations>

<property name="id" identifier="true" writable="false" />
<property name="name" required="true" />
<property name="locale" required="true" />
<property name="slug" required="true" />
<property name="description" required="false" />
<property name="metaKeywords" required="false" />
<property name="metaDescription" required="false" />
<property name="shortDescription" required="false" />
</resource>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@
<attribute name="id">
<group>product:read</group>
</attribute>
<attribute name="code">
<group>product:read</group>
<group>product:create</group>
</attribute>
<attribute name="createdAt">
<group>product:read</group>
</attribute>
<attribute name="updatedAt">
<group>product:read</group>
</attribute>
<attribute name="translations">
<group>product:read</group>
<group>product:create</group>
</attribute>
</class>
</serializer>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>

<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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\Product\Model\ProductTranslation">
<attribute name="id">
<group>product:read</group>
</attribute>
<attribute name="name">
<group>product:read</group>
<group>product:create</group>
</attribute>
<attribute name="locale">
<group>product:create</group>
</attribute>
<attribute name="slug">
<group>product:create</group>
<group>product:read</group>
</attribute>
</class>
</serializer>