Skip to content

Commit

Permalink
Cover behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMilek committed Sep 23, 2024
1 parent 848083e commit ff43452
Show file tree
Hide file tree
Showing 14 changed files with 321 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@managing_products
Feature: Reordering product images
In order to have the most important product image displayed first
As an Administrator
I want to be able to reorder product images

Background:
Given the store operates on a channel named "Web-US" in "USD" currency
And the store has a product "Dice Brewing" priced at "$10.00" in "Web-US" channel
And this product has an image "ford.jpg" with "small" type at position 0
And this product has an image "ford.jpg" with "medium" type at position 1
And this product has an image "ford.jpg" with "large" type at position 2
And I am logged in as an administrator

@api @ui @mink:chromedriver
Scenario: Reordering product images
When I want to modify the images of "Dice Brewing" product
And I change the "small" image position to 2
And I change the "medium" image position to 0
And I change the "large" image position to 1
Then I save my changes to the images
And the one before last image on the list should have type "large" with position 1
And the last image on the list should have type small with position 2
16 changes: 0 additions & 16 deletions features/shop/product/show/viewing_product_image.feature

This file was deleted.

28 changes: 28 additions & 0 deletions features/shop/product/show/viewing_product_images.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@viewing_products
Feature: Viewing a product's images on a product details page
In order to see images of a product
As a Visitor
I want to be able to view a product's images

Background:
Given the store operates on a single channel in "United States"

@api @ui @mink:chromedriver
Scenario: Viewing a product's main image on a product details page
Given the store has a product "Lamborghini Gallardo Model"
And this product has an image "lamborghini.jpg" with "other" type at position 2
And this product has an image "lamborghini.jpg" with "main" type at position 1
When I check this product's details
Then I should see the product name "Lamborghini Gallardo Model"
And I should see a main image of type "main"

@api @ui @mink:chromedriver
Scenario: Viewing a products images in correct order
Given the store has a product "Lamborghini Gallardo Model"
And this product has an image "lamborghini.jpg" with "other" type at position 2
And this product has an image "lamborghini.jpg" with "thumbnail" type at position 1
When I check this product's details
Then I should see the product name "Lamborghini Gallardo Model"
And the main image should be of type "thumbnail"
And the first thumbnail image should be of type "thumbnail"
And the second thumbnail image should be of type "other"
57 changes: 57 additions & 0 deletions src/Sylius/Behat/Context/Api/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\RequestBuilder;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Admin\Helper\ValidationTrait;
use Sylius\Behat\Context\Api\Resources;
Expand All @@ -32,6 +33,7 @@
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
use Sylius\Component\Product\Model\ProductAttributeInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -343,6 +345,39 @@ public function iSetTheInvalidStringValueOfTheNonTranslatableAttributeTo(Product
);
}

/**
* @When I want to modify the images of :product product
*/
public function iWantToModifyTheImagesOfProduct(ProductInterface $product): void
{
$this->sharedStorage->set('productIri', $this->iriConverter->getIriFromResource($product));
}

/**
* @When I change the :type image position to :position
*/
public function iChangeTheImagePositionTo(string $imageType, int $position): void
{
$images = $this->responseChecker->getValue($this->client->showByIri($this->sharedStorage->get('productIri')), 'images');
$productCode = $this->responseChecker->getValue($this->client->getLastResponse(), 'code');

foreach ($images as $key => $imageData) {
if ($imageData['type'] === $imageType) {
$imageId = $imageData['id'];
}
}

$builder = RequestBuilder::create(
sprintf('/api/v2/admin/products/%s/images/%s', $productCode, $imageId),
Request::METHOD_PUT,
);
$builder->withContent(['position' => $position]);
$builder->withHeader('HTTP_Authorization', 'Bearer ' . $this->sharedStorage->get('token'));
$builder->withHeader('CONTENT_TYPE', 'application/ld+json');

$this->client->request($builder->build());
}

/**
* @When I set its :attribute attribute to :value
* @When I set its :attribute attribute to :value in :localeCode locale
Expand Down Expand Up @@ -616,6 +651,28 @@ public function iShouldBeNotifiedThatIsRequired(string $element): void
);
}

/**
* @Then the one before last image on the list should have type :type with position :position
*/
public function theOneBeforeLastImageOnTheListShouldHaveNameWithPosition(string $imageType, int $position): void
{
$images = $this->responseChecker->getValue($this->client->showByIri($this->sharedStorage->get('productIri')), 'images');

Assert::same($images[count($images) - 2]['type'], $imageType);
Assert::same($images[count($images) - 2]['position'], $position);
}

/**
* @Then the last image on the list should have type :type with position :position
*/
public function theLastImageOnTheListShouldHaveNameWithPosition(string $imageType, int $position): void
{
$images = $this->responseChecker->getValue($this->client->showByIri($this->sharedStorage->get('productIri')), 'images');

Assert::same($images[count($images) - 1]['type'], $imageType);
Assert::same($images[count($images) - 1]['position'], $position);
}

/**
* @Then I should be notified that meta keywords are too long
*/
Expand Down
34 changes: 34 additions & 0 deletions src/Sylius/Behat/Context/Api/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ApiPlatform\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Persistence\ObjectManager;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\RequestFactoryInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
Expand All @@ -41,6 +42,7 @@ public function __construct(
private IriConverterInterface $iriConverter,
private ChannelContextSetterInterface $channelContextSetter,
private RequestFactoryInterface $requestFactory,
private ObjectManager $objectManager,
private string $apiUrlPrefix,
) {
}
Expand All @@ -52,6 +54,7 @@ public function __construct(
*/
public function iViewProduct(ProductInterface $product): void
{
$this->objectManager->clear(); // it's needed to clear the entity manager to receive the product images in correct order, as the images are using fallback order when added programmatically
$this->client->show(Resources::PRODUCTS, $product->getCode());

/** @var ProductVariantInterface $productVariant */
Expand Down Expand Up @@ -473,6 +476,37 @@ public function iShouldSeeProductName(string $name): void
Assert::true($this->responseChecker->hasValue($this->client->getLastResponse(), 'name', $name));
}

/**
* @Then the main image should be of type :type
* @Then I should see a main image of type :type
*/
public function theMainImageShouldBeOfType(string $type): void
{
$images = $this->responseChecker->getValue($this->client->getLastResponse(), 'images');

Assert::same($images[0]['type'], $type);
}

/**
* @Then the first thumbnail image should be of type :type
*/
public function theFirstThumbnailImageShouldBeOfType(string $type): void
{
$images = $this->responseChecker->getValue($this->client->getLastResponse(), 'images');

Assert::same($images[0]['type'], $type);
}

/**
* @Then the second thumbnail image should be of type :type
*/
public function theSecondThumbnailImageShouldBeOfType(string $type): void
{
$images = $this->responseChecker->getValue($this->client->getLastResponse(), 'images');

Assert::same($images[1]['type'], $type);
}

/**
* @Then /^I should not be able to view (this product) in the ("([^"]+)" locale)$/
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Sylius/Behat/Context/Setup/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,14 @@ public function thisProductHasAnImageWithType(ProductInterface $product, $imageP
$this->createProductImage($product, $imagePath, $imageType);
}

/**
* @Given /^(this product) has an image "([^"]+)" with "([^"]+)" type at position (\d+)$/
*/
public function thisProductHasAnImageWithTypeAtPosition(ProductInterface $product, string $imagePath, string $imageType, int $position): void
{
$this->createProductImage($product, $imagePath, $imageType, null, $position);
}

/**
* @Given /^(this product) has an image "([^"]+)" with "([^"]+)" type for ("[^"]+" variant)$/
*/
Expand Down Expand Up @@ -1585,13 +1593,15 @@ private function createProductImage(
string $imagePath,
string $imageType,
?ProductVariantInterface $variant = null,
?int $position = null,
): void {
$filesPath = $this->getParameter('files_path');

/** @var ProductImageInterface $productImage */
$productImage = $this->productImageFactory->createNew();
$productImage->setFile(new UploadedFile($filesPath . $imagePath, basename($imagePath)));
$productImage->setType($imageType);
$productImage->setPosition($position);

if (null !== $variant) {
$productImage->addProductVariant($variant);
Expand Down
35 changes: 35 additions & 0 deletions src/Sylius/Behat/Context/Ui/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,32 @@ public function theOneBeforeLastProductOnTheListShouldHaveNameWithPosition(strin
$this->sharedStorage->set('product_taxon_name', $productName);
}

/**
* @Then the one before last image on the list should have type :type with position :position
*/
public function theOneBeforeLastImageOnTheListShouldHaveNameWithPosition(string $imageType, int $position): void
{
$images = $this->mediaFormElement->getImages();
if (count($images) < 2) {
throw new \Exception('There are less than two images on the list.');
}

$oneBeforeLastImage = $images[count($images) - 2];

$this->mediaFormElement->assertImageTypeAndPosition($oneBeforeLastImage, $imageType, $position);
}

/**
* @Then the last image on the list should have type :type with position :position
*/
public function theLastImageOnTheListShouldHaveNameWithPosition(string $imageType, int $position): void
{
$images = $this->mediaFormElement->getImages();
$lastImage = end($images);

$this->mediaFormElement->assertImageTypeAndPosition($lastImage, $imageType, $position);
}

/**
* @Then the last product on the list should have :field :value
* @Then the last product on the list within this taxon should have :field :value
Expand Down Expand Up @@ -492,6 +518,7 @@ public function productShouldExistInTheProductCatalog(ProductInterface $product)
* @When /^I want to modify (this product)$/
* @When /^I want to edit (this product)$/
* @When I modify the :product product
* @When I want to modify the images of :product product
*/
public function iWantToModifyAProduct(ProductInterface $product): void
{
Expand Down Expand Up @@ -1000,6 +1027,14 @@ public function iChangeTheFirstImageTypeTo(string $type): void
$this->mediaFormElement->modifyFirstImageType($type);
}

/**
* @When I change the :type image position to :position
*/
public function iChangeTheImagePositionTo(string $image, int $position): void
{
$this->mediaFormElement->modifyPositionOfImageWithType($image, $position);
}

/**
* @Then /^(this product) should not have any images$/
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public function iWantToModifyAProduct(ProductInterface $product): void
$this->updateSimpleProductPage->open(['id' => $product->getId()]);
}

/**
* @When I change position of the :image image to :position
*/
public function iChangePositionOfTheImageTo(string $image, int $position): void
{
$this->updateSimpleProductPage->changeImagePosition($image, $position);
}

/**
* @Then I should be on :product product edit page
*/
Expand Down
30 changes: 27 additions & 3 deletions src/Sylius/Behat/Context/Ui/Shop/ProductContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,35 @@ public function iShouldBeNotifiedThatTheQuantityOfThisProductMustBeBetween1And99
}

/**
* @Then I should see a main image
* @Then I should see a main image of type :type
*/
public function iShouldSeeAMainImage(): void
public function iShouldSeeAMainImageOfType(string $type): void
{
Assert::true($this->showPage->isMainImageDisplayed());
Assert::true($this->showPage->isMainImageOfTypeDisplayed($type));
}

/**
* @Then the main image should be of type :type
*/
public function theMainImageShouldBeOfType(string $type): void
{
Assert::true($this->showPage->isMainImageOfType($type));
}

/**
* @Then the first thumbnail image should be of type :type
*/
public function theFirstThumbnailImageShouldBeOfType(string $type): void
{
Assert::true($this->showPage->getFirstThumbnailsImageType() === $type);
}

/**
* @Then the second thumbnail image should be of type :type
*/
public function theSecondThumbnailImageShouldBeOfType(string $type): void
{
Assert::true($this->showPage->getSecondThumbnailsImageType() === $type);
}

/**
Expand Down
Loading

0 comments on commit ff43452

Please sign in to comment.