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

Finish covering managing product-related resources #15630

Merged
merged 9 commits into from
Dec 18, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ Feature: Editing product reviews
Then I should be notified that it has been successfully edited
And this product review rating should be 5

@ui
@ui @no-api
Scenario: Seeing a product's name while editing a product review
When I want to modify the "Awesome" product review
Then I should be editing review of product "Lamborghini Gallardo Model"

@ui
@ui @no-api
Scenario: Seeing a customer's name while editing a product review
When I want to modify the "Awesome" product review
Then I should see the customer's name "Mike Ross"
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Feature: Browsing product reviews
And this product has a new review titled "Bad" and rated 1 added by customer "ross@teammike.com"
And I am logged in as an administrator

@ui
@ui @api
Scenario: Browsing accepted reviews
When I want to browse product reviews
And I choose "Accepted" as a status filter
And I choose "accepted" as a status filter
And I filter
Then I should see a single product review in the list
And I should see the product review "Awesome" in the list
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Feature: Recalculating product average rating
And this product has a review titled "Not bad" and rated 3 with a comment "Not bad car" added by customer "specter@teamharvey.com"
And I am logged in as an administrator

@ui @javascript
@ui @javascript @api
Scenario: Product's average rating is correctly recalculated after review's rate change
When I want to modify the "Awesome" product review
And I choose 5 as its rating
And I save my changes
Then I should be notified that it has been successfully edited
And average rating of product "Lamborghini Gallardo Model" should be 4

@ui
@ui @api
Scenario: Product's average rating is correctly recalculated after review's rate change
When I delete the "Awesome" product review
Then I should be notified that it has been successfully deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,46 @@ Feature: Sorting listed product variants from a product by position
And this product has also an "Opel Insignia Sedan" variant at position 1
And I am logged in as an administrator

@ui
@ui @api
Scenario: Product variants are sorted by position in ascending order by default
When I view all variants of the product "Opel Insignia"
Then I should see 3 variants in the list
And the first variant in the list should have name "Opel Insignia Hatchback"
And the last variant in the list should have name "Opel Insignia Sports Tourer"

@ui
@ui @api
Scenario: Sorting product variants in descending order
When I view all variants of the product "Opel Insignia"
And I start sorting variants by position
Then the first variant in the list should have name "Opel Insignia Sports Tourer"
And the last variant in the list should have name "Opel Insignia Hatchback"

@ui
@ui @api
Scenario: New product variant with no position is added as the last one
Given the product "Opel Insignia" has also an "Opel Insignia Country Tourer" variant
When I view all variants of the product "Opel Insignia"
Then I should see 4 variants in the list
And the last variant in the list should have name "Opel Insignia Country Tourer"

@ui
@ui @api
Scenario: New product variant with position 0 is added as the first one
Given the product "Opel Insignia" has also an "Opel Insignia Country Tourer" variant at position 0
When I view all variants of the product "Opel Insignia"
Then I should see 4 variants in the list
And the first variant in the list should have name "Opel Insignia Country Tourer"

@ui @javascript
@ui @javascript @api
Scenario: Setting product variant as the first one in the list
When I view all variants of the product "Opel Insignia"
And I set the position of "Opel Insignia Sedan" to 0
And I save my new configuration
And the first variant in the list should have name "Opel Insignia Sedan"
And I view all variants of the product "Opel Insignia" again
Then the first variant in the list should have name "Opel Insignia Sedan"

@ui @javascript
@ui @javascript @api
Scenario: Setting product variant as the last one in the list
When I view all variants of the product "Opel Insignia"
And I set the position of "Opel Insignia Sedan" to 7
And I save my new configuration
And the last variant in the list should have name "Opel Insignia Sedan"
And I view all variants of the product "Opel Insignia" again
Then the last variant in the list should have name "Opel Insignia Sedan"
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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 Sylius\Behat\Context\Api\Resources;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Webmozart\Assert\Assert;

final class BrowsingProductVariantsContext implements Context
{
public function __construct (
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,
) {
}

/**
* @When I start sorting variants by position
*/
public function iSortProductsByPosition(): void
Wojdylak marked this conversation as resolved.
Show resolved Hide resolved
{
$this->client->index(
Resources::PRODUCT_VARIANTS,
[
'order[position]' => 'desc',
],
);
}

/**
* @When I set the position of :productVariant to :position
*/
public function iSetThePositionOfTo(ProductVariantInterface $productVariant, int $position): void
{
$this->client->buildUpdateRequest(Resources::PRODUCT_VARIANTS, $productVariant->getCode());
$this->client->updateRequestData(['position' => $position]);
}

/**
* @When I save my new configuration
*/
public function iSaveMyNewConfiguration(): void
{
$this->client->update();
}

/**
* @Then the first variant in the list should have name :variantName
*/
public function theFirstVariantInTheListShouldHaveName(string $variantName): void
{
$variants = $this->responseChecker->getCollection($this->client->getLastResponse());

$firstVariant = reset($variants);

$this->assertProductVariantName($firstVariant['translations']['en_US']['name'], $variantName);
}

/**
* @Then the last variant in the list should have name :variantName
*/
public function theLastVariantInTheListShouldHaveName(string $variantName): void
{
$variants = $this->responseChecker->getCollection($this->client->getLastResponse());

$lastVariant = end($variants);

$this->assertProductVariantName($lastVariant['translations']['en_US']['name'], $variantName);
}

private function assertProductVariantName(string $variantName, string $expectedVariantName): void
{
Assert::same(
$variantName,
$expectedVariantName,
sprintf(
'Expected product variant to have name "%s", but it is named "%s".',
$expectedVariantName,
$variantName,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Review\Model\ReviewInterface;
use Webmozart\Assert\Assert;

Expand All @@ -38,6 +39,22 @@ public function iWantToBrowseProductReviews(): void
$this->client->index(Resources::PRODUCT_REVIEWS);
}

/**
* @When I choose :status as a status filter
*/
public function iChooseAsStatusFilter(string $status): void
{
$this->client->addFilter('status', $status);
}

/**
* @When I filter
*/
public function iFilter(): void
{
$this->client->filter();
}

/**
* @When I want to modify the :productReview product review
*/
Expand Down Expand Up @@ -192,6 +209,20 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyDeleted(): void
);
}

/**
* @Then average rating of product :product should be :expectedRating
*/
public function averageRatingOfProductShouldBe(ProductInterface $product, int $expectedRating): void
{
$averageRating = $this->responseChecker->getValue($this->client->show(Resources::PRODUCTS, (string) $product->getCode()), 'averageRating');

Assert::same(
$averageRating,
$expectedRating,
sprintf('Average rating of product %s is not %s', $product->getName(), $expectedRating),
);
}

private function isItemOnIndex(string $property, string $value): bool
{
return $this->responseChecker->hasItemWithValue($this->client->index(Resources::PRODUCT_REVIEWS), $property, $value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,13 @@ public function iDoNotWantToHaveShippingRequiredForThisProductVariant(): void

/**
* @When /^I want to view all variants of (this product)$/
* @When /^I view(?:| all) variants of the (product "[^"]+")$/
* @When /^I view(?:| all) variants of the (product "[^"]+")(?:| again)$/
*/
public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product): void
{
$this->client->index(Resources::PRODUCT_VARIANTS);
$this->client->addFilter('product', $this->iriConverter->getIriFromResource($product));
$this->client->addFilter('order[position]', 'asc');
$this->client->filter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function theProductShouldHaveOnlyOneVariant(ProductInterface $product)
/**
* @When /^I browse variants of (this product)$/
* @When /^I (?:|want to )view all variants of (this product)$/
* @When /^I view(?:| all) variants of the (product "[^"]+")$/
* @When /^I view(?:| all) variants of the (product "[^"]+")(?:| again)$/
*/
public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<argument type="service" id="api_platform.iri_converter" />
</service>

<service id="Sylius\Behat\Context\Api\Admin\BrowsingProductVariantsContext">
<argument type="service" id="sylius.behat.api_platform_client.admin" />
<argument type="service" id="Sylius\Behat\Client\ResponseCheckerInterface" />
</service>

<service id="Sylius\Behat\Context\Api\Admin\CreatingProductVariantContext">
<argument type="service" id="sylius.behat.api_platform_client.admin" />
<argument type="service" id="api_platform.iri_converter" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ default:
- sylius.behat.context.api.admin.managing_product_variants
- sylius.behat.context.api.admin.response
- sylius.behat.context.api.admin.save
- Sylius\Behat\Context\Api\Admin\BrowsingProductVariantsContext
- Sylius\Behat\Context\Api\Admin\ChannelPricingLogEntryContext

filters:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* 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\Bundle\ApiBundle\OpenApi\Documentation;

use ApiPlatform\Core\OpenApi\Model\Parameter;
use ApiPlatform\OpenApi\OpenApi;
use Sylius\Component\Review\Model\ReviewInterface;

/** @experimental */
final class ProductReviewDocumentationModifier implements DocumentationModifierInterface
{
public const PATH = '%s/admin/product-reviews';

public function __construct(
private string $apiRoute,
) {
}

public function modify(OpenApi $docs): OpenApi
{
$paths = $docs->getPaths();

$path = sprintf(self::PATH, $this->apiRoute);
$pathItem = $paths->getPath($path);
$operation = $pathItem->getGet();

$parameters = $operation->getParameters();
$parameters = array_filter(
$parameters,
fn (Parameter $parameter) => $parameter->getName() !== 'status' && $parameter->getName() !== 'status[]',
);
$parameters[] = new Parameter(
name: 'status',
in: 'query',
description: 'Status of product reviews you want to get',
schema: [
'type' => 'string',
'enum' => [ReviewInterface::STATUS_NEW, ReviewInterface::STATUS_ACCEPTED, ReviewInterface::STATUS_REJECTED],
'nullable' => true,
'default' => null,
],
);
$parameters = array_values($parameters);

$operation = $operation->withParameters($parameters);
$pathItem = $pathItem->withGet($operation);
$paths->addPath($path, $pathItem);

return $docs->withPaths($paths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<collectionOperation name="admin_get">
<attribute name="method">GET</attribute>
<attribute name="path">/admin/product-reviews</attribute>
<attribute name="filters">
<attribute>sylius.api.product_review_status_filter</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">admin:product_review:read</attribute>
</attribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<attribute name="method">GET</attribute>
<attribute name="path">/admin/product-variants</attribute>
<attribute name="filters">
<attribute>sylius.api.product_variant_position_filter</attribute>
<attribute>sylius.api.product_variant_product_filter</attribute>
<attribute>Sylius\Bundle\ApiBundle\Filter\Doctrine\ProductVariantCatalogPromotionFilter</attribute>
</attribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
<tag name="sylius.open_api.modifier" />
</service>

<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\ProductReviewDocumentationModifier">
<argument>%sylius.security.new_api_route%</argument>
<tag name="sylius.open_api.modifier" />
</service>

<service id="Sylius\Bundle\ApiBundle\OpenApi\Documentation\ProductSlugDocumentationModifier">
<argument>%sylius.security.new_api_route%</argument>
<tag name="sylius.open_api.modifier" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<group>shop:product:read</group>
</attribute>
<attribute name="averageRating">
<group>admin:product:read</group>
jakubtobiasz marked this conversation as resolved.
Show resolved Hide resolved
<group>shop:product:read</group>
</attribute>
<attribute name="associations">
Expand Down