Test different variant images on cart page

This commit is contained in:
Mateusz Zalewski 2019-03-04 14:13:58 +01:00
parent 1dd2945b14
commit b30c8fa51f
No known key found for this signature in database
GPG key ID: 0545A7503DD474B8
9 changed files with 167 additions and 12 deletions

View file

@ -0,0 +1,11 @@
services:
sylius_test.filesystem:
class: Gaufrette\Filesystem
arguments:
- "%sylius.uploader.filesystem%"
factory: ['@knp_gaufrette.filesystem_map', 'get']
sylius.image_uploader:
class: Sylius\Behat\Service\Uploader\ImageUploader
arguments:
- "@sylius_test.filesystem"
public: true

View file

@ -0,0 +1,2 @@
imports:
- { resource: "../test/sylius_uploader.yaml" }

View file

@ -0,0 +1,25 @@
@shopping_cart
Feature: Having proper product image displayed in the cart
In order be aware of how the product that I'm buying looks like
As a Visitor
I want to have proper product image displayed in the cart
Background:
Given the store operates on a single channel in "United States"
And the store has a product "T-shirt Car"
And this product has "Small logo" variant priced at "$12.35"
And this product has "Medium logo" variant priced at "$15.35"
And this product has an image "lamborghini.jpg" with "main" type
And this product has an image "ford.jpg" with "main" type for "Medium logo" variant
@ui
Scenario: Having a variant's image displayed in the cart
When I add "Medium logo" variant of this product to the cart
Then I should be on my cart summary page
And 1st item in my cart should have "ford.jpg" image displayed
@ui
Scenario: Having a product's image displayed in the cart if variant does not have one
When I add "Small logo" variant of this product to the cart
Then I should be on my cart summary page
And 1st item in my cart should have "lamborghini.jpg" image displayed

View file

@ -22,6 +22,7 @@ use Sylius\Component\Core\Formatter\StringInflector;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Model\ChannelPricingInterface;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Model\ProductImageInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductTranslationInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
@ -697,18 +698,19 @@ final class ProductContext implements Context
*/
public function thisProductHasAnImageWithType(ProductInterface $product, $imagePath, $imageType)
{
$filesPath = $this->getParameter('files_path');
$this->createProductImage($product, $imagePath, $imageType);
}
/** @var ImageInterface $productImage */
$productImage = $this->productImageFactory->createNew();
$productImage->setFile(new UploadedFile($filesPath . $imagePath, basename($imagePath)));
$productImage->setType($imageType);
$this->imageUploader->upload($productImage);
$product->addImage($productImage);
$this->objectManager->persist($product);
$this->objectManager->flush();
/**
* @Given /^(this product) has an image "([^"]+)" with "([^"]+)" type for ("[^"]+" variant)$/
*/
public function thisProductHasAnImageWithTypeForVariant(
ProductInterface $product,
string $imagePath,
string $imageType,
ProductVariantInterface $variant
): void {
$this->createProductImage($product, $imagePath, $imageType, $variant);
}
/**
@ -944,4 +946,29 @@ final class ProductContext implements Context
$this->objectManager->persist($option);
$this->objectManager->flush();
}
private function createProductImage(
ProductInterface $product,
string $imagePath,
string $imageType,
?ProductVariantInterface $variant = 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);
if (null !== $variant) {
$productImage->addProductVariant($variant);
}
$this->imageUploader->upload($productImage);
$product->addImage($productImage);
$this->objectManager->persist($product);
$this->objectManager->flush();
}
}

View file

@ -409,6 +409,14 @@ final class CartContext implements Context
Assert::same($this->summaryPage->getCartTotal(), $total);
}
/**
* @Then /^(\d)(st|nd|rd|th) item in my cart should have "([^"]+)" image displayed$/
*/
public function itemShouldHaveImageDisplayed(int $itemNumber, string $image): void
{
Assert::contains($this->summaryPage->getItemImage($itemNumber), $image);
}
private function getPriceFromString(string $price): int
{
return (int) round((float) str_replace(['€', '£', '$'], '', $price) * 100, 2);

View file

@ -107,6 +107,13 @@ class SummaryPage extends SymfonyPage implements SummaryPageInterface
return $this->getPriceFromString(trim($unitPrice->getText()));
}
public function getItemImage(int $itemNumber): string
{
$itemImage = $this->getElement('item_image', ['%number%' => $itemNumber]);
return $itemImage->getAttribute('src');
}
/**
* {@inheritdoc}
*/
@ -276,12 +283,13 @@ class SummaryPage extends SymfonyPage implements SummaryPageInterface
{
return array_merge(parent::getDefinedElements(), [
'apply_coupon_button' => 'button:contains("Apply coupon")',
'base_grand_total' => '#sylius-cart-base-grand-total',
'cart_items' => '#sylius-cart-items',
'cart_total' => '#sylius-cart-total',
'clear_button' => '#sylius-cart-clear',
'coupon_field' => '#sylius_cart_promotionCoupon',
'grand_total' => '#sylius-cart-grand-total',
'base_grand_total' => '#sylius-cart-base-grand-total',
'item_image' => '#sylius-cart-items tbody tr:nth-child(%number%) img',
'product_discounted_total' => '#sylius-cart-items tr:contains("%name%") .sylius-discounted-total',
'product_row' => '#sylius-cart-items tbody tr:contains("%name%")',
'product_total' => '#sylius-cart-items tr:contains("%name%") .sylius-total',

View file

@ -64,6 +64,8 @@ interface SummaryPageInterface extends PageInterface
*/
public function getItemUnitPrice($productName);
public function getItemImage(int $itemNumber): string;
/**
* @param string $productName
*

View file

@ -13,6 +13,7 @@ default:
- sylius.behat.context.transform.shared_storage
- sylius.behat.context.transform.product
- sylius.behat.context.transform.product_option
- sylius.behat.context.transform.product_variant
- sylius.behat.context.transform.shipping_category
- sylius.behat.context.transform.tax_category
- sylius.behat.context.transform.zone

View file

@ -0,0 +1,71 @@
<?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\Service\Uploader;
use Sylius\Component\Core\Model\ImageInterface;
use Sylius\Component\Core\Uploader\ImageUploader as BaseImageUploader;
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
use Symfony\Component\HttpFoundation\File\File;
use Webmozart\Assert\Assert;
final class ImageUploader extends BaseImageUploader implements ImageUploaderInterface
{
public function upload(ImageInterface $image): void
{
if (!$image->hasFile()) {
return;
}
$file = $image->getFile();
/** @var File $file */
Assert::isInstanceOf($file, File::class);
if (null !== $image->getPath() && $this->has($image->getPath())) {
$this->remove($image->getPath());
}
do {
$hash = bin2hex(random_bytes(16));
$path = $this->expandPath($hash . '/' . $file->getFilename() . '.' . $file->guessExtension());
} while ($this->isAdBlockingProne($path) || $this->filesystem->has($path));
$image->setPath($path);
$this->filesystem->write(
$image->getPath(),
file_get_contents($image->getFile()->getPathname())
);
}
private function expandPath(string $path): string
{
return sprintf(
'%s/%s/%s',
substr($path, 0, 2),
substr($path, 2, 2),
substr($path, 4)
);
}
private function has(string $path): bool
{
return $this->filesystem->has($path);
}
private function isAdBlockingProne(string $path): bool
{
return strpos($path, 'ad') !== false;
}
}