Skip to content

Commit

Permalink
Fix validating resending shipment confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
NoResponseMate committed Feb 2, 2024
1 parent ae6f10f commit 6cbe4b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Sylius\Bundle\CoreBundle\Message\ResendShipmentConfirmationEmail;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
Expand All @@ -42,9 +41,8 @@ public function validate(mixed $value, Constraint $constraint): void

/** @var ShipmentInterface|null $shipment */
$shipment = $this->shipmentRepository->findOneBy(['id' => $value->getShipmentId()]);

if (null === $shipment) {
throw new NotFoundHttpException(sprintf('Shipment with %s id has not been found.', $value->getShipmentId()));
return;
}

if ($shipment->getState() !== ShipmentInterface::STATE_SHIPPED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@
namespace spec\Sylius\Bundle\CoreBundle\Validator\Constraints;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\Message\ResendShipmentConfirmationEmail;
use Sylius\Bundle\CoreBundle\Validator\Constraints\ResendShipmentConfirmationEmailWithValidShipmentState;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilderInterface;

final class ResendShipmentConfirmationEmailWithValidShipmentStateValidatorSpec extends ObjectBehavior
{
const MESSAGE = 'sylius.resend_shipment_confirmation_email.invalid_shipment_state';

function let(RepositoryInterface $shipmentRepository, ExecutionContextInterface $context): void
{
$this->beConstructedWith($shipmentRepository);
Expand All @@ -36,11 +34,18 @@ function let(RepositoryInterface $shipmentRepository, ExecutionContextInterface

function it_throws_an_exception_if_constraint_is_not_an_instance_of_resend_order_confirmation_email_with_valid_order_state(
Constraint $constraint,
ResendShipmentConfirmationEmail $value,
): void {
$this
->shouldThrow(UnexpectedTypeException::class)
->during('validate', [$value, $constraint])
->during('validate', [new ResendShipmentConfirmationEmail(123), $constraint])
;
}

function it_throws_an_exception_if_value_is_not_resend_shipment_confirmation(): void
{
$this
->shouldThrow(UnexpectedTypeException::class)
->during('validate', [new \stdClass(), new ResendShipmentConfirmationEmailWithValidShipmentState()])
;
}

Expand All @@ -51,22 +56,29 @@ function it_does_nothing_if_the_state_is_valid(
): void {
$shipmentRepository->findOneBy(['id' => 2])->willReturn($shipment);
$shipment->getState()->willReturn(ShipmentInterface::STATE_SHIPPED);
$this->validate(new ResendShipmentConfirmationEmail(2), new ResendShipmentConfirmationEmailWithValidShipmentState());

$context->buildViolation(self::MESSAGE)->shouldNotHaveBeenCalled();
$context->buildViolation(Argument::any())->shouldNotBeCalled();

$this->validate(
new ResendShipmentConfirmationEmail(2),
new ResendShipmentConfirmationEmailWithValidShipmentState()
);
}

function it_adds_a_violation_if_order_has_invalid_state(
RepositoryInterface $shipmentRepository,
ShipmentInterface $shipment,
ExecutionContextInterface $context,
ConstraintViolationBuilderInterface $constraintViolationBuilder,
): void {
$constraint = new ResendShipmentConfirmationEmailWithValidShipmentState();
$shipmentRepository->findOneBy(['id' => 2])->willReturn($shipment);
$shipment->getState()->willReturn(ShipmentInterface::STATE_CANCELLED);

$context->addViolation(self::MESSAGE, ['%state%' => ShipmentInterface::STATE_CANCELLED])->shouldBeCalled()->willReturn($constraintViolationBuilder);
$context
->addViolation($constraint->message, ['%state%' => ShipmentInterface::STATE_CANCELLED])
->shouldBeCalled()
;

$this->validate(new ResendShipmentConfirmationEmail(2), new ResendShipmentConfirmationEmailWithValidShipmentState());
$this->validate(new ResendShipmentConfirmationEmail(2), $constraint);
}
}

0 comments on commit 6cbe4b7

Please sign in to comment.