Skip to content

Commit

Permalink
OP-319 - Added PHPSpec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkalon committed Aug 7, 2024
1 parent 36bb663 commit 3cab4af
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace spec\BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener;

use BitBag\SyliusInPostPlugin\Entity\ShippingExportInterface;
use BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener\SelectParcelTemplateAction;
use BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener\SelectParcelTemplateActionInterface;
use BitBag\SyliusShippingExportPlugin\Repository\ShippingExportRepositoryInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class SelectParcelTemplateActionSpec extends ObjectBehavior
{
public function let(
ShippingExportRepositoryInterface $shippingExportRepository,
RequestStack $requestStack,
TranslatorInterface $translator,
): void {
$this->beConstructedWith($shippingExportRepository, $requestStack, $translator);
}

public function it_is_initializable()
{
$this->shouldHaveType(SelectParcelTemplateAction::class);
$this->shouldBeAnInstanceOf(SelectParcelTemplateActionInterface::class);
}

public function it_should_save_shipping_export_changes(
ShippingExportInterface $shippingExport,
ShippingExportRepositoryInterface $shippingExportRepository,
RequestStack $requestStack,
SessionInterface $session,
FlashBagInterface $flashBag
): void {
$shippingExportRepository->add($shippingExport)->shouldBeCalled();

$requestStack->getSession()->willReturn($session);
$session->getBag('flashes')->willReturn($flashBag);

$this->execute($shippingExport);
}

}
82 changes: 82 additions & 0 deletions spec/EventListener/SelectParcelTemplateEventListenerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?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 spec\BitBag\SyliusInPostPlugin\EventListener;

use BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener;
use BitBag\SyliusInPostPlugin\EventListener\SelectParcelTemplateEventListener\SelectParcelTemplateActionInterface;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
use Tests\BitBag\SyliusInPostPlugin\Spec\Builder\ShipmentBuilder;
use Tests\BitBag\SyliusInPostPlugin\Spec\Builder\ShippingExportBuilder;
use Tests\BitBag\SyliusInPostPlugin\Spec\Builder\ShippingGatewayBuilder;

final class SelectParcelTemplateEventListenerSpec extends ObjectBehavior
{
private const INCORRECT_PARCEL_TEMPLATE = 'x-large';

private const EXPECTED_PARCEL_TEMPLATE = 'small';

private const INPOST = 'inpost';

public function let(
SelectParcelTemplateActionInterface $action
): void {
$this->beConstructedWith($action);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(SelectParcelTemplateEventListener::class);
}

public function it_should_throw_exception_if_subject_is_not_shipping_export_instance(ResourceControllerEvent $event): void
{
$event->getSubject()->willReturn(new \stdClass());

$this
->shouldThrow(\InvalidArgumentException::class)
->during('setParcelTemplate', [$event])
;
}

public function it_should_throw_exception_if_incorrect_parcel_template_is_passed(ResourceControllerEvent $event): void
{
$shippingGateway = ShippingGatewayBuilder::create()->withCode(self::INPOST)->build();
$shipment = ShipmentBuilder::create()->build();
$shippingExport = ShippingExportBuilder::create()
->withShippingGateway($shippingGateway)
->withShipment($shipment)
->withParcelTemplate(self::INCORRECT_PARCEL_TEMPLATE)
->build();
$event->getSubject()->willReturn($shippingExport);

$this
->shouldThrow(\Exception::class)
->during('setParcelTemplate', [$event])
;
}

/**
* @throws \Exception
*/
public function it_should_perform_set_parcel_template_action(ResourceControllerEvent $event): void
{
$shippingGateway = ShippingGatewayBuilder::create()->withCode(self::INPOST)->build();
$shipment = ShipmentBuilder::create()->build();
$shippingExport = ShippingExportBuilder::create()
->withShippingGateway($shippingGateway)
->withShipment($shipment)
->withParcelTemplate(self::EXPECTED_PARCEL_TEMPLATE)
->build();
$event->getSubject()->willReturn($shippingExport);
$this->setParcelTemplate($event);
}
}
8 changes: 8 additions & 0 deletions src/EventListener/SelectParcelTemplateEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class SelectParcelTemplateEventListener
{
private SelectParcelTemplateActionInterface $action;

private const PARCEL_TEMPLATE_VALUES = [
'small', 'medium', 'large',
];

public function __construct(
SelectParcelTemplateActionInterface $action,
) {
Expand All @@ -32,6 +36,10 @@ public function setParcelTemplate(ResourceControllerEvent $exportShipmentEvent):
$shippingExport = $exportShipmentEvent->getSubject();
Assert::isInstanceOf($shippingExport, ShippingExportInterface::class);

if (!in_array($shippingExport->getParcelTemplate(), self::PARCEL_TEMPLATE_VALUES)) {
throw new \Exception(sprintf('"%s" is invalid parcel template!', $shippingExport->getParcelTemplate()));
}

$this->action->execute($shippingExport);
}
}
7 changes: 7 additions & 0 deletions tests/Spec/Builder/ShippingExportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public function withShippingGateway(ShippingGatewayInterface $shippingGateway):
return $this;
}

public function withParcelTemplate(string $template): self
{
$this->shippingExport->setParcelTemplate($template);

return $this;
}

public function build(): ShippingExportInterface
{
return $this->shippingExport;
Expand Down

0 comments on commit 3cab4af

Please sign in to comment.