diff --git a/composer.json b/composer.json index 583a310a..feed878d 100644 --- a/composer.json +++ b/composer.json @@ -32,8 +32,9 @@ "symfony/intl": "^4.4 || ^5.2", "symfony/web-profiler-bundle": "^4.4 || ^5.2", "symfony/web-server-bundle": "^4.4|^5.2", - "symfony/webpack-encore-bundle": "^1.12", - "symfony/dependency-injection": "<4.4.19 || >=5.0.0 <5.2.2" + "symfony/dependency-injection": "<4.4.19 || >=5.0.0 <5.2.2", + "dompdf/dompdf": "1.0.2", + "symfony/webpack-encore-bundle": "^1.12" }, "conflict": { "doctrine/dbal": "^3.0", diff --git a/features/adding_product_to_wishlist.feature b/features/adding_product_to_wishlist.feature index c642f990..d3eb280d 100644 --- a/features/adding_product_to_wishlist.feature +++ b/features/adding_product_to_wishlist.feature @@ -43,5 +43,3 @@ Feature: Adding a product to wishlist And I log out And I log in again Then I should have one item in my wishlist - - diff --git a/features/exporting_product_from_wishlist_to_pdf.feature b/features/exporting_product_from_wishlist_to_pdf.feature new file mode 100644 index 00000000..b09b284f --- /dev/null +++ b/features/exporting_product_from_wishlist_to_pdf.feature @@ -0,0 +1,16 @@ +@wishlist +Feature: Exporting a product from wishlist to pdf + In order to save products and buy later + As a Visitor + I want to be able to exporting products to pdf + + Background: + Given the store operates on a single channel in "United States" + + @ui + Scenario: Exporting selected products from wishlist + Given the store has a product "Jack Daniels Gentleman" priced at "$10.00" + And I have this product in my wishlist + When I go to the wishlist page + Then I check "Jack Daniels Gentleman" + And I export to pdf selected products from wishlist and file is downloaded diff --git a/spec/Model/Factory/VariantPdfModelFactorySpec.php b/spec/Model/Factory/VariantPdfModelFactorySpec.php new file mode 100644 index 00000000..f4665a23 --- /dev/null +++ b/spec/Model/Factory/VariantPdfModelFactorySpec.php @@ -0,0 +1,53 @@ +shouldHaveType(VariantPdfModelFactory::class); + } + + function it_implements_variant_pdf_model_factory_interface(): void + { + $this->shouldHaveType(VariantPdfModelFactoryInterface::class); + } + + function it_returns_product_pdf_model(): void + { + $productVariant = new ProductVariant(); + $productPdfModel = $this->createWithVariantAndImagePath( + $productVariant, + 'http://127.0.0.1:8000/media/image/b4/c2/fc6b3202ee567e0fb05f293b709c.jpg', + 10, + 'variant test' + ); + + $productPdfModel->getVariant()->shouldReturn($productVariant); + $productPdfModel->getImagePath()->shouldReturn('http://127.0.0.1:8000/media/image/b4/c2/fc6b3202ee567e0fb05f293b709c.jpg'); + $productPdfModel->getQuantity()->shouldReturn(10); + $productPdfModel->getActualVariant()->shouldReturn('variant test'); + + $this->createWithVariantAndImagePath( + $productVariant, + 'http://127.0.0.1:8000/media/image/b4/c2/fc6b3202ee567e0fb05f293b709c.jpg', + 10, + 'variant test' + )->shouldBeAnInstanceOf(VariantPdfModel::class); + } +} diff --git a/spec/Model/VariantPdfModelSpec.php b/spec/Model/VariantPdfModelSpec.php new file mode 100644 index 00000000..58865543 --- /dev/null +++ b/spec/Model/VariantPdfModelSpec.php @@ -0,0 +1,42 @@ +shouldHaveType(VariantPdfModel::class); + } + + function it_implements_variant_pdf_model_interface(): void + { + $this->shouldHaveType(VariantPdfModelInterface::class); + } + + function it_returns_property_of_variant_pdf_model(ProductVariantInterface $productVariant): void + { + $this->setActualVariant('variant test'); + $this->setVariant($productVariant); + $this->setImagePath('/image/123/image.jpg'); + $this->setQuantity(10); + + $this->getActualVariant()->shouldReturn('variant test'); + $this->getVariant()->shouldReturn($productVariant); + $this->getImagePath()->shouldReturn('/image/123/image.jpg'); + $this->getQuantity()->shouldReturn(10); + } +} diff --git a/spec/Resolver/VariantImageToDataUriResolverSpec.php b/spec/Resolver/VariantImageToDataUriResolverSpec.php new file mode 100644 index 00000000..401e97c2 --- /dev/null +++ b/spec/Resolver/VariantImageToDataUriResolverSpec.php @@ -0,0 +1,62 @@ +beConstructedWith( + $dataUriForImageResolver + ); + } + + public function it_is_initializable(): void + { + $this->shouldHaveType(VariantImageToDataUriResolver::class); + } + + public function it_resolve_empty_image_path( + ProductVariantInterface $variant, + ProductInterface $product, + Collection $productImages + ): void { + $variant->getProduct()->willReturn($product); + $product->getImages()->willReturn($productImages); + $productImages->first()->willReturn(false); + + $this->resolve($variant, self::TEST_BASE_URL)->shouldReturn(''); + } + + public function it_resolve_image_path( + ProductVariantInterface $variant, + ProductInterface $product, + Collection $productImages, + ProductImage $productImage, + GenerateDataUriForImageResolverInterface $dataUriForImageResolver + ): void { + $variant->getProduct()->willReturn($product); + $product->getImages()->willReturn($productImages); + $productImages->first()->willReturn($productImage); + $dataUriForImageResolver->resolve($productImage)->willReturn(self::TEST_BASE_URL); + $this->resolve($variant, self::TEST_BASE_URL)->shouldReturn(self::TEST_BASE_URL); + } +} diff --git a/src/Command/Wishlist/AddProductsToWishlist.php b/src/Command/Wishlist/AddProductsToWishlist.php index e8bdb6ce..2131187d 100644 --- a/src/Command/Wishlist/AddProductsToWishlist.php +++ b/src/Command/Wishlist/AddProductsToWishlist.php @@ -14,7 +14,7 @@ final class AddProductsToWishlist { - /** @var Collection */ + /** @var Collection */ private Collection $wishlistProducts; public function __construct(Collection $wishlistProducts) diff --git a/src/Command/Wishlist/AddSelectedProductsToCart.php b/src/Command/Wishlist/AddSelectedProductsToCart.php index 2cc110b2..90b3463d 100644 --- a/src/Command/Wishlist/AddSelectedProductsToCart.php +++ b/src/Command/Wishlist/AddSelectedProductsToCart.php @@ -14,7 +14,7 @@ final class AddSelectedProductsToCart { - /** @var Collection */ + /** @var Collection */ private Collection $wishlistProducts; public function __construct(Collection $wishlistProducts) diff --git a/src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdf.php b/src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdf.php new file mode 100644 index 00000000..633edae7 --- /dev/null +++ b/src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdf.php @@ -0,0 +1,31 @@ + */ + private Collection $wishlistProducts; + + public function __construct(Collection $wishlistProducts) + { + $this->wishlistProducts = $wishlistProducts; + } + + public function getWishlistProducts(): ?Collection + { + return $this->wishlistProducts; + } +} diff --git a/src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdfInterface.php b/src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdfInterface.php new file mode 100644 index 00000000..bac97379 --- /dev/null +++ b/src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdfInterface.php @@ -0,0 +1,12 @@ + */ + /** @var Collection */ private Collection $wishlistProducts; public function __construct(Collection $wishlistProducts) diff --git a/src/Command/Wishlist/AddWishlistProduct.php b/src/Command/Wishlist/WishlistItem.php similarity index 96% rename from src/Command/Wishlist/AddWishlistProduct.php rename to src/Command/Wishlist/WishlistItem.php index 2a3b3d44..0fca0a83 100644 --- a/src/Command/Wishlist/AddWishlistProduct.php +++ b/src/Command/Wishlist/WishlistItem.php @@ -13,7 +13,7 @@ use BitBag\SyliusWishlistPlugin\Entity\WishlistProductInterface; use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface; -class AddWishlistProduct +class WishlistItem implements WishlistItemInterface { private ?WishlistProductInterface $wishlistProduct; diff --git a/src/Command/Wishlist/WishlistItemInterface.php b/src/Command/Wishlist/WishlistItemInterface.php new file mode 100644 index 00000000..1896da95 --- /dev/null +++ b/src/Command/Wishlist/WishlistItemInterface.php @@ -0,0 +1,23 @@ +productCanBeProcessed($wishlistProduct)) { $this->addProductToWishlist($wishlistProduct); @@ -57,7 +57,7 @@ private function addProductsToWishlist(Collection $wishlistProducts): void } } - private function productCanBeProcessed(AddWishlistProduct $wishlistProduct): bool + private function productCanBeProcessed(WishlistItem $wishlistProduct): bool { $cartItem = $wishlistProduct->getCartItem()->getCartItem(); @@ -85,7 +85,7 @@ private function productHasPositiveQuantity(OrderItemInterface $product): bool return false; } - private function addProductToWishlist(AddWishlistProduct $wishlistProduct): void + private function addProductToWishlist(WishlistItem $wishlistProduct): void { $cart = $wishlistProduct->getCartItem()->getCart(); $cartItem = $wishlistProduct->getCartItem()->getCartItem(); diff --git a/src/CommandHandler/Wishlist/AddSelectedProductsToCartHandler.php b/src/CommandHandler/Wishlist/AddSelectedProductsToCartHandler.php index 8574830c..8a0a45e5 100644 --- a/src/CommandHandler/Wishlist/AddSelectedProductsToCartHandler.php +++ b/src/CommandHandler/Wishlist/AddSelectedProductsToCartHandler.php @@ -11,7 +11,7 @@ namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddSelectedProductsToCart; -use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct; +use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem; use Doctrine\Common\Collections\Collection; use Sylius\Component\Core\Repository\OrderRepositoryInterface; use Sylius\Component\Order\Modifier\OrderItemQuantityModifierInterface; @@ -57,7 +57,7 @@ public function __invoke(AddSelectedProductsToCart $addSelectedProductsToCartCom private function addSelectedProductsToCart(Collection $wishlistProducts): void { - /** @var AddWishlistProduct $wishlistProduct */ + /** @var WishlistItem $wishlistProduct */ foreach ($wishlistProducts as $wishlistProduct) { if (!$wishlistProduct->isSelected()) { continue; @@ -71,7 +71,7 @@ private function addSelectedProductsToCart(Collection $wishlistProducts): void } } - private function isInStock(AddWishlistProduct $wishlistProduct): bool + private function isInStock(WishlistItem $wishlistProduct): bool { $cartItem = $wishlistProduct->getCartItem()->getCartItem(); @@ -84,7 +84,7 @@ private function isInStock(AddWishlistProduct $wishlistProduct): bool return false; } - private function addProductToWishlist(AddWishlistProduct $wishlistProduct): void + private function addProductToWishlist(WishlistItem $wishlistProduct): void { $cart = $wishlistProduct->getCartItem()->getCart(); $cartItem = $wishlistProduct->getCartItem()->getCartItem(); diff --git a/src/CommandHandler/Wishlist/ExportSelectedProductsFromWishlistToPdfHandler.php b/src/CommandHandler/Wishlist/ExportSelectedProductsFromWishlistToPdfHandler.php new file mode 100644 index 00000000..2abf1a1f --- /dev/null +++ b/src/CommandHandler/Wishlist/ExportSelectedProductsFromWishlistToPdfHandler.php @@ -0,0 +1,38 @@ +request = $request; + $this->exporterWishlistToPdf = $exporterWishlistToPdf; + } + + public function __invoke(ExportSelectedProductsFromWishlistToPdfInterface $exportSelectedProductsFromWishlistToPdf): void + { + $wishlistProducts = $exportSelectedProductsFromWishlistToPdf->getWishlistProducts(); + $this->exporterWishlistToPdf + ->createModelToPdfAndExportToPdf($wishlistProducts, $this->request->getCurrentRequest()); + } +} diff --git a/src/CommandHandler/Wishlist/RemoveSelectedProductsFromWishlistHandler.php b/src/CommandHandler/Wishlist/RemoveSelectedProductsFromWishlistHandler.php index 1cf5a655..1ff2786f 100644 --- a/src/CommandHandler/Wishlist/RemoveSelectedProductsFromWishlistHandler.php +++ b/src/CommandHandler/Wishlist/RemoveSelectedProductsFromWishlistHandler.php @@ -10,7 +10,7 @@ namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist; -use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProduct; +use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItem; use BitBag\SyliusWishlistPlugin\Command\Wishlist\RemoveSelectedProductsFromWishlist; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\EntityManagerInterface; @@ -53,7 +53,7 @@ public function __invoke(RemoveSelectedProductsFromWishlist $removeSelectedProdu private function removeSelectedProductsFromWishlist(Collection $wishlistProducts): void { - /** @var AddWishlistProduct $wishlistProduct */ + /** @var WishlistItem $wishlistProduct */ foreach ($wishlistProducts as $wishlistProduct) { if ($wishlistProduct->isSelected()) { $this->removeProductFromWishlist($wishlistProduct); @@ -61,7 +61,7 @@ private function removeSelectedProductsFromWishlist(Collection $wishlistProducts } } - private function removeProductFromWishlist(AddWishlistProduct $wishlistProduct): void + private function removeProductFromWishlist(WishlistItem $wishlistProduct): void { $productVariant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant()); diff --git a/src/Controller/Action/ExportWishlistToPdfAction.php b/src/Controller/Action/ExportWishlistToPdfAction.php new file mode 100644 index 00000000..a64d8f5d --- /dev/null +++ b/src/Controller/Action/ExportWishlistToPdfAction.php @@ -0,0 +1,31 @@ +get('items')->getData()); + $this->messageBus->dispatch($command); + } catch (HandlerFailedException $ex) { + $this->flashBag->add( + 'error', + 'Select at least one product' + ); + } + } +} diff --git a/src/Exception/NoProductSelectedException.php b/src/Exception/NoProductSelectedException.php new file mode 100644 index 00000000..228747d9 --- /dev/null +++ b/src/Exception/NoProductSelectedException.php @@ -0,0 +1,17 @@ +setRequired('cart') - ->setDefault('data_class', AddWishlistProduct::class) + ->setDefault('data_class', WishlistItem::class) ->setDefault('validation_groups', $this->validationGroups); } } diff --git a/src/Model/Factory/VariantPdfModelFactory.php b/src/Model/Factory/VariantPdfModelFactory.php new file mode 100644 index 00000000..d1a5dc34 --- /dev/null +++ b/src/Model/Factory/VariantPdfModelFactory.php @@ -0,0 +1,33 @@ +setVariant($variant); + $productPdfModel->setImagePath($path); + $productPdfModel->setQuantity($quantity); + $productPdfModel->setActualVariant($actualVariant); + + return $productPdfModel; + } +} diff --git a/src/Model/Factory/VariantPdfModelFactoryInterface.php b/src/Model/Factory/VariantPdfModelFactoryInterface.php new file mode 100644 index 00000000..70221b19 --- /dev/null +++ b/src/Model/Factory/VariantPdfModelFactoryInterface.php @@ -0,0 +1,24 @@ +variant; + } + + public function setVariant(ProductVariantInterface $variant): void + { + $this->variant = $variant; + } + + public function getImagePath(): string + { + return $this->imagePath; + } + + public function setImagePath(string $imagePath): void + { + $this->imagePath = $imagePath; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function setQuantity(int $quantity): void + { + $this->quantity = $quantity; + } + + public function getActualVariant(): string + { + return $this->actualVariant; + } + + public function setActualVariant(string $actualVariant): void + { + $this->actualVariant = $actualVariant; + } +} diff --git a/src/Model/VariantPdfModelInterface.php b/src/Model/VariantPdfModelInterface.php new file mode 100644 index 00000000..998c7eed --- /dev/null +++ b/src/Model/VariantPdfModelInterface.php @@ -0,0 +1,32 @@ +setWishlistProduct($wishlistProductItem); $commandsArray->add($wishlistProductCommand); } diff --git a/src/Resolver/GenerateDataUriForImageResolver.php b/src/Resolver/GenerateDataUriForImageResolver.php new file mode 100644 index 00000000..07d2b3ea --- /dev/null +++ b/src/Resolver/GenerateDataUriForImageResolver.php @@ -0,0 +1,44 @@ +package = $package; + $this->filterService = $filterService; + $this->imageFilterName = $imageFilterName; + } + + public function resolve(ProductImageInterface $image): string + { + $pathToReadFile = $this->package->getUrl($image->getPath()); + $targetUrl = $this->filterService->getUrlOfFilteredImage($pathToReadFile, $this->imageFilterName); + $data = file_get_contents($targetUrl); + $type = pathinfo($image->getPath(), \PATHINFO_EXTENSION); + + return 'data:image/' . $type . ';base64,' . base64_encode($data); + } +} diff --git a/src/Resolver/GenerateDataUriForImageResolverInterface.php b/src/Resolver/GenerateDataUriForImageResolverInterface.php new file mode 100644 index 00000000..558b18a6 --- /dev/null +++ b/src/Resolver/GenerateDataUriForImageResolverInterface.php @@ -0,0 +1,18 @@ +dataUriForImageResolver = $dataUriForImageResolver; + } + + public function resolve(ProductVariantInterface $variant, string $baseUrl): string + { + $image = $variant->getProduct()->getImages()->first(); + if (false === $image) { + return ''; + } + + return $this->dataUriForImageResolver->resolve($image); + } +} diff --git a/src/Resolver/VariantImageToDataUriResolverInterface.php b/src/Resolver/VariantImageToDataUriResolverInterface.php new file mode 100644 index 00000000..c9735586 --- /dev/null +++ b/src/Resolver/VariantImageToDataUriResolverInterface.php @@ -0,0 +1,20 @@ + +
+ +
diff --git a/src/Resources/views/_wishlist_pdf.html.twig b/src/Resources/views/_wishlist_pdf.html.twig new file mode 100644 index 00000000..48586e2c --- /dev/null +++ b/src/Resources/views/_wishlist_pdf.html.twig @@ -0,0 +1,44 @@ +{% import "@SyliusShop/Common/Macro/money.html.twig" as money %} + + + + + {{ title }} + + +
+
+

{{ title }}

+
+
+

{{ date }}

+
+
+

+ + + + + + + + + + + + + + {% for product in products %} + + + + + + + + + {% endfor %} + +
ImageNameNumberVariantQuantityPrice
Product Image{{ product.variant.product.name }}{{ product.variant.product.code }}{{ product.actualVariant }}{{ product.quantity }}{{ money.calculatePrice(product.variant.product|sylius_resolve_variant) }}
+ + \ No newline at end of file diff --git a/src/Services/Exporter/WishlistToPdfExporter.php b/src/Services/Exporter/WishlistToPdfExporter.php new file mode 100644 index 00000000..8669ceec --- /dev/null +++ b/src/Services/Exporter/WishlistToPdfExporter.php @@ -0,0 +1,110 @@ +productVariantRepository = $productVariantRepository; + $this->twigEnvironment = $twigEnvironment; + $this->modelCreator = $modelCreator; + } + + public function createModelToPdfAndExportToPdf(Collection $wishlistProducts, Request $request): void + { + $productsToExport = $this->createVariantModelToPdf($wishlistProducts, $request); + + if (empty($productsToExport)) { + throw new NoProductSelectedException(); + } + $this->exportToPdf($productsToExport); + } + + private function createVariantModelToPdf(Collection $wishlistProducts, Request $request): array + { + /** @var WishlistItemInterface $wishlistProduct */ + foreach ($wishlistProducts as $wishlistProduct) { + $selectedProducts[] = $this->createCollectionOfWishlistItems($wishlistProduct, $request); + } + + return $selectedProducts; + } + + private function createCollectionOfWishlistItems( + WishlistItemInterface $wishlistProduct, + Request $request + ): VariantPdfModelInterface { + $itemWishlistModel = null; + + if ($wishlistProduct->isSelected()) { + $variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant()); + $this->wishlistThrowException($wishlistProduct, $variant); + $itemWishlistModel = $this->modelCreator->createWishlistItemToPdf($wishlistProduct, $request, $variant); + } + + if (null === $itemWishlistModel) { + throw new ProductVariantNotFoundException( + sprintf('The Product does not selected') + ); + } + + return $itemWishlistModel; + } + + private function wishlistThrowException(WishlistItemInterface $wishlistProduct, ProductVariant $variant) + { + if (null === $variant || null === $wishlistProduct) { + throw new ProductVariantNotFoundException( + sprintf('The Product does not exist') + ); + } + } + + private function exportToPdf(array $selectedProducts): void + { + $pdfOptions = new Options(); + $pdfOptions->set('isRemoteEnabled', true); + $pdfOptions->set('defaultFont', 'Arial'); + $dompdf = new Dompdf($pdfOptions); + $html = $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/_wishlist_pdf.html.twig', [ + 'title' => 'My wishlist products', + 'date' => date('d.m.Y'), + 'products' => $selectedProducts, + ]); + $dompdf->loadHtml($html); + $dompdf->setPaper('A4', 'portrait'); + $dompdf->render(); + $dompdf->stream('wishlist.pdf', ['Attachment' => true]); + } +} diff --git a/src/Services/Exporter/WishlistToPdfExporterInterface.php b/src/Services/Exporter/WishlistToPdfExporterInterface.php new file mode 100644 index 00000000..0d896da0 --- /dev/null +++ b/src/Services/Exporter/WishlistToPdfExporterInterface.php @@ -0,0 +1,13 @@ +variantImageToDataUriResolver = $variantImageToDataUriResolver; + $this->variantPdfModelFactory = $variantPdfModelFactory; + } + + public function createWishlistItemToPdf( + WishlistItemInterface $wishlistProduct, + Request $request, + ProductVariant $variant + ): VariantPdfModelInterface + { + $cartItem = $wishlistProduct->getCartItem()->getCartItem(); + $quantity = $cartItem->getQuantity(); + $baseUrl = $request->getSchemeAndHttpHost(); + $urlToImage = $this->variantImageToDataUriResolver->resolve($variant, $baseUrl); + $actualVariant = $cartItem->getVariant()->getCode(); + + return $this->variantPdfModelFactory->createWithVariantAndImagePath( + $variant, + $urlToImage, + $quantity, + $actualVariant + ); + } +} diff --git a/src/Services/Generator/ModelCreatorInterface.php b/src/Services/Generator/ModelCreatorInterface.php new file mode 100644 index 00000000..63b60b53 --- /dev/null +++ b/src/Services/Generator/ModelCreatorInterface.php @@ -0,0 +1,25 @@ +productRepository = $productRepository; $this->productIndexPage = $productIndexPage; $this->wishlistPage = $wishlistPage; @@ -57,6 +64,8 @@ public function __construct( $this->loginer = $loginer; $this->wishlistCreator = $wishlistCreator; $this->productShowPage = $productShowPage; + $this->session = $session; + $this->router = $router; } /** @@ -175,6 +184,39 @@ public function iRemoveSelectedProductsFromWishlist(): void $this->wishlistPage->removeSelectedProductsFromWishlist(); } + /** + * @When I export to pdf selected products from wishlist and file is downloaded + */ + public function iExportToPdfSelectedProductsFromWishlistAndFileIsDownloaded(): void + { + $this->wishlistPage->exportToPdfSelectedProductsFromWishlist(); + + $cookieName = $this->session->getName(); + $sessionId = $this->session->getId(); + $baseUrl = $this->getMinkParameter('base_url'); + $domain = parse_url($baseUrl)['host']; + + $cookieJar = \GuzzleHttp\Cookie\CookieJar::fromArray([ + $cookieName => $sessionId, + ], $domain); + + $guzzle = new \GuzzleHttp\Client([ + 'timeout' => 10, + 'cookies' => $cookieJar, + ]); + + $url = $this->router->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_export_to_pdf',[], UrlGeneratorInterface::RELATIVE_PATH); + $response = $guzzle->get(sprintf('%s%s', $baseUrl,$url)); + $driver = $this->getSession()->getDriver(); + $contentType = $response->getHeader('Content-Type')[0]; + + if ($contentType !== "text/html; charset=UTF-8") { + throw new \Behat\Mink\Exception\ExpectationException('The content type of the downloaded file is not correct.', $driver); + } + + Assert::eq($this->getSession()->getStatusCode(), '200'); + } + /** * @Then I should be on my wishlist page */ @@ -224,7 +266,7 @@ public function iShouldHaveProductInMyWishlist(string $productName): void } /** - * @Then I should have :productName product in my cart + * @Then I should have ":productName" product in my cart */ public function iShouldHaveProductInMyCart(string $productName): void { diff --git a/tests/Behat/Page/Shop/WishlistPage.php b/tests/Behat/Page/Shop/WishlistPage.php index c0b29916..efb940c1 100644 --- a/tests/Behat/Page/Shop/WishlistPage.php +++ b/tests/Behat/Page/Shop/WishlistPage.php @@ -52,6 +52,11 @@ public function removeSelectedProductsFromWishlist(): void $this->getElement('remove_selected')->press(); } + public function exportToPdfSelectedProductsFromWishlist(): void + { + $this->getElement('export_selected_pdf')->press(); + } + public function selectProductQuantity(string $productName, int $quantity): void { $addToCartElements = $this->getDocument()->findAll('css', '[data-test-wishlist-item-quantity] input'); @@ -106,7 +111,8 @@ protected function getDefinedElements(): array 'add' => '[data-test-wishlist-add-all-to-cart]', 'add_selected' => '[data-test-wishlist-add-selected-to-cart]', 'remove_selected' => '[data-test-wishlist-remove-selected-from-wishlist]', - 'items_count' => '[data-test-wishlist-primary-items-count]' + 'items_count' => '[data-test-wishlist-primary-items-count]', + 'export_selected_pdf' => '[data-test-wishlist-export-to-pdf-from-wishlist]' ]; } } diff --git a/tests/Behat/Resources/services.yml b/tests/Behat/Resources/services.yml index f7889a14..af059e73 100644 --- a/tests/Behat/Resources/services.yml +++ b/tests/Behat/Resources/services.yml @@ -24,6 +24,8 @@ services: - "@sylius.behat.notification_checker" - "@bitbag_sylius_cms_plugin.behat.loginer" - "@bitbag_sylius_cms_plugin.behat.wishlist_creator" + - "@session" + - "@router" bitbag_sylius_cms_plugin.behat.page.shop.wishlist: class: Tests\BitBag\SyliusWishlistPlugin\Behat\Page\Shop\WishlistPage