Skip to content

Commit

Permalink
refactor code & fix phpspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrSzymanski2000 committed Nov 29, 2021
1 parent 9092141 commit a900400
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 162 deletions.
52 changes: 32 additions & 20 deletions spec/Controller/Action/ExportWishlistToPdfActionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@

namespace spec\BitBag\SyliusWishlistPlugin\Controller\Action;

use BitBag\SyliusWishlistPlugin\Command\Wishlist\ExportSelectedProductsFromWishlistToPdfInterface;
use BitBag\SyliusWishlistPlugin\Context\WishlistContextInterface;
use BitBag\SyliusWishlistPlugin\Controller\Action\ExportWishlistToPdfAction;
use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
use BitBag\SyliusWishlistPlugin\Exporter\ExporterWishlistToPdfInterface;
use BitBag\SyliusWishlistPlugin\Factory\Command\CommandFactory;
use BitBag\SyliusWishlistPlugin\Factory\Command\CommandFactoryInterface;
use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType;
use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface;
use Doctrine\Common\Collections\ArrayCollection;
Expand All @@ -29,6 +31,10 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
Expand All @@ -43,8 +49,9 @@ function let(
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
Environment $twigEnvironment,
ExporterWishlistToPdfInterface $exporterWishlistToPdf,
WishlistCommandProcessorInterface $wishlistCommandProcessor
WishlistCommandProcessorInterface $wishlistCommandProcessor,
MessageBusInterface $messageBus,
CommandFactoryInterface $commandFactory
): void {
$this->beConstructedWith(
$wishlistContext,
Expand All @@ -54,8 +61,9 @@ function let(
$translator,
$urlGenerator,
$twigEnvironment,
$exporterWishlistToPdf,
$wishlistCommandProcessor
$wishlistCommandProcessor,
$messageBus,
$commandFactory
);
}

Expand All @@ -72,20 +80,22 @@ function it_renders_header_template(
OrderInterface $cart,
FormFactoryInterface $formFactory,
FormInterface $form,
FormErrorIterator $formErrorIterator,
ExporterWishlistToPdfInterface $exporterWishlistToPdf,
FlashBagInterface $flashBag,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator,
ArrayCollection $arrayCollection,
WishlistCommandProcessorInterface $wishlistCommandProcessor,
Collection $wishlistProducts,
ArrayCollection $commandsArray
FormErrorIterator $formErrorIterator,
UrlGeneratorInterface $urlGenerator,
ArrayCollection $commandsArray,
MessageBusInterface $messageBus,
ExportSelectedProductsFromWishlistToPdfInterface $exportSelectedProductsFromWishlistToPdf,
CommandFactoryInterface $commandFactory,
Environment $environment,
Response $response,
FormView $formView
): void {
$wishlistContext->getWishlist($request)->willReturn($wishlist);
$cartContext->getCart()->willReturn($cart);
$wishlist->getWishlistProducts()->willReturn($wishlistProducts);

$wishlistCommandProcessor->createFromWishlistProducts($wishlistProducts)->willReturn($commandsArray);

$formFactory
Expand All @@ -103,18 +113,20 @@ function it_renders_header_template(
$form->handleRequest($request)->shouldBeCalled();
$form->isSubmitted()->willReturn(true);
$form->isValid()->willReturn(true);
$form->get('items')->willReturn($form);
$form->get('items')->willReturn($form);// zmienic
$form->getData()->willReturn($arrayCollection);

$message = $exportSelectedProductsFromWishlistToPdf->getWrappedObject();

$commandFactory->createFrom($arrayCollection, $request)->willReturn($message);
$envelope = new Envelope($message,[new HandledStamp('result',MessageHandlerInterface::class)]);
$messageBus->dispatch($message)->willReturn($envelope);

$form->getErrors()->willReturn($formErrorIterator);

$exporterWishlistToPdf->handleWishlistItemsToGeneratePdf($arrayCollection, $request)->willReturn(false);
$translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products')->willReturn('Select at least one item.');
$flashBag->add('error', 'Select at least one item.');
$urlGenerator
->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products')
->willReturn('Content');
$form->createView()->willReturn($formView);

$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
$this->__invoke($request)->shouldHaveType(Response::class);
}

function it_renders_template_with_error(
Expand Down
121 changes: 0 additions & 121 deletions spec/Exporter/ExporterWishlistToPdfSpec.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/Command/Wishlist/ExportSelectedProductsFromWishlistToPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
*/

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Command\Wishlist;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Request;

final class ExportSelectedProductsFromWishlistToPdf implements ExportSelectedProductsFromWishlistToPdfInterface
{
private Request $request;
private ArrayCollection $wishlistProducts;

public function __construct(ArrayCollection $wishlistProducts, Request $request)
{
$this->wishlistProducts = $wishlistProducts;
$this->request = $request;
}

public function getWishlistProducts(): ?ArrayCollection
{
return $this->wishlistProducts;
}

public function getRequest(): Request
{
return $this->request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Command\Wishlist;

use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Request;

interface ExportSelectedProductsFromWishlistToPdfInterface
{
public function getWishlistProducts(): ?ArrayCollection;

public function getRequest(): Request;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@

declare(strict_types=1);

namespace BitBag\SyliusWishlistPlugin\Exporter;
namespace BitBag\SyliusWishlistPlugin\CommandHandler\Wishlist;

use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistProductInterface;
use BitBag\SyliusWishlistPlugin\Command\Wishlist\ExportSelectedProductsFromWishlistToPdfInterface;
use BitBag\SyliusWishlistPlugin\Model\Factory\VariantPdfModelFactoryInterface;
use BitBag\SyliusWishlistPlugin\Resolver\VariantImagePathResolverInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Dompdf\Dompdf;
use Dompdf\Options;
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Twig\Environment;

final class ExporterWishlistToPdf implements ExporterWishlistToPdfInterface
final class ExportSelectedProductsFromWishlistToPdfHandler implements MessageHandlerInterface
{
private ProductVariantRepositoryInterface $productVariantRepository;

Expand All @@ -43,8 +43,11 @@ public function __construct(
$this->twigEnvironment = $twigEnvironment;
}

public function handleWishlistItemsToGeneratePdf(ArrayCollection $wishlistProducts, Request $request): bool
public function __invoke(ExportSelectedProductsFromWishlistToPdfInterface $exportSelectedProductsFromWishlistToPdf): bool
{
$wishlistProducts = $exportSelectedProductsFromWishlistToPdf->getWishlistProducts();
$request = $exportSelectedProductsFromWishlistToPdf->getRequest();

$selectedProducts = [];

/** @var AddWishlistProductInterface $wishlistProduct */
Expand Down
Loading

0 comments on commit a900400

Please sign in to comment.