Completed
Push — 1.2-packages-require-1.2 ( d8563e )
by Kamil
35:32 queued 14:53
created

CartItemAvailabilityValidatorSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 133
c 0
b 0
f 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 6 1
A it_is_constraint_validator() 0 4 1
A it_validates_only_add_cart_item_commands() 0 6 1
A it_is_cart_item_availability_validator() 0 6 1
A it_does_not_add_violation_if_requested_cart_item_is_available() 0 22 1
B it_adds_violation_if_requested_cart_item_is_not_available() 0 24 1
B it_adds_violation_if_total_quantity_of_cart_items_exceed_available_quantity() 0 28 1
B it_does_not_add_violation_if_total_quantity_of_cart_items_do_not_exceed_available_quantity() 0 27 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace spec\Sylius\Bundle\CoreBundle\Validator\Constraints;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use PhpSpec\ObjectBehavior;
18
use Prophecy\Argument;
19
use Sylius\Bundle\CoreBundle\Validator\Constraints\CartItemAvailability;
20
use Sylius\Bundle\InventoryBundle\Validator\Constraints\InStock;
21
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Core\Model\OrderItemInterface;
24
use Sylius\Component\Core\Model\ProductVariantInterface;
25
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
26
use Symfony\Component\Validator\ConstraintValidator;
27
use Symfony\Component\Validator\Context\ExecutionContextInterface;
28
29
final class CartItemAvailabilityValidatorSpec extends ObjectBehavior
30
{
31
    function let(ExecutionContextInterface $executionContext, AvailabilityCheckerInterface $availabilityChecker): void
32
    {
33
        $this->beConstructedWith($availabilityChecker);
34
35
        $this->initialize($executionContext);
36
    }
37
38
    function it_is_constraint_validator(): void
39
    {
40
        $this->shouldHaveType(ConstraintValidator::class);
41
    }
42
43
    function it_validates_only_add_cart_item_commands(OrderInterface $order): void
44
    {
45
        $cartItemAvailabilityConstraint = new CartItemAvailability();
46
47
        $this->shouldThrow(\InvalidArgumentException::class)->during('validate', [$order, $cartItemAvailabilityConstraint]);
48
    }
49
50
    function it_is_cart_item_availability_validator(AddToCartCommandInterface $addCartItemCommand): void
51
    {
52
        $inStockConstraint = new InStock();
53
54
        $this->shouldThrow(\InvalidArgumentException::class)->during('validate', [$addCartItemCommand, $inStockConstraint]);
55
    }
56
57
    function it_does_not_add_violation_if_requested_cart_item_is_available(
58
        ExecutionContextInterface $executionContext,
59
        AvailabilityCheckerInterface $availabilityChecker,
60
        AddToCartCommandInterface $addCartItemCommand,
61
        OrderInterface $order,
62
        OrderItemInterface $orderItem,
63
        ProductVariantInterface $productVariant
64
    ): void {
65
        $addCartItemCommand->getCart()->willReturn($order);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...r\Model\OrderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...del\OrderItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        $orderItem->getVariant()->willReturn($productVariant);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...roductVariantInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
        $orderItem->getQuantity()->willReturn(10);
69
        $order->getItems()->willReturn(new ArrayCollection([]));
70
71
        $availabilityChecker->isStockSufficient($productVariant, 10)->willReturn(true);
72
73
        $executionContext->addViolation(Argument::any(), Argument::any())->shouldNotBeCalled();
74
75
        $cartItemAvailabilityConstraint = new CartItemAvailability();
76
77
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
78
    }
79
80
    function it_adds_violation_if_requested_cart_item_is_not_available(
81
        ExecutionContextInterface $executionContext,
82
        AvailabilityCheckerInterface $availabilityChecker,
83
        AddToCartCommandInterface $addCartItemCommand,
84
        OrderInterface $order,
85
        OrderItemInterface $orderItem,
86
        ProductVariantInterface $productVariant
87
    ): void {
88
        $addCartItemCommand->getCart()->willReturn($order);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...r\Model\OrderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...del\OrderItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $orderItem->getVariant()->willReturn($productVariant);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...roductVariantInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        $orderItem->getQuantity()->willReturn(10);
92
        $order->getItems()->willReturn(new ArrayCollection([]));
93
        $productVariant->getInventoryName()->willReturn('Mug');
94
95
        $availabilityChecker->isStockSufficient($productVariant, 10)->willReturn(false);
96
97
        $executionContext->addViolation('Insufficient stock', ['%itemName%' => 'Mug'])->shouldBeCalled();
98
99
        $cartItemAvailabilityConstraint = new CartItemAvailability();
100
        $cartItemAvailabilityConstraint->message = 'Insufficient stock';
101
102
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
103
    }
104
105
    function it_adds_violation_if_total_quantity_of_cart_items_exceed_available_quantity(
106
        ExecutionContextInterface $executionContext,
107
        AvailabilityCheckerInterface $availabilityChecker,
108
        AddToCartCommandInterface $addCartItemCommand,
109
        OrderInterface $order,
110
        OrderItemInterface $orderItem,
111
        OrderItemInterface $existingOrderItem,
112
        ProductVariantInterface $productVariant
113
    ): void {
114
        $addCartItemCommand->getCart()->willReturn($order);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...r\Model\OrderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...del\OrderItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
        $orderItem->getVariant()->willReturn($productVariant);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...roductVariantInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
        $orderItem->getQuantity()->willReturn(10);
118
        $productVariant->getInventoryName()->willReturn('Mug');
119
120
        $order->getItems()->willReturn(new ArrayCollection([$existingOrderItem->getWrappedObject()]));
0 ignored issues
show
Bug introduced by
The method getWrappedObject() does not seem to exist on object<Sylius\Component\...del\OrderItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
        $existingOrderItem->getQuantity()->willReturn(10);
122
        $existingOrderItem->equals($orderItem)->willReturn(true);
123
124
        $availabilityChecker->isStockSufficient($productVariant, 20)->willReturn(false);
125
126
        $executionContext->addViolation('Insufficient stock', ['%itemName%' => 'Mug'])->shouldBeCalled();
127
128
        $cartItemAvailabilityConstraint = new CartItemAvailability();
129
        $cartItemAvailabilityConstraint->message = 'Insufficient stock';
130
131
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
132
    }
133
134
    function it_does_not_add_violation_if_total_quantity_of_cart_items_do_not_exceed_available_quantity(
135
        ExecutionContextInterface $executionContext,
136
        AvailabilityCheckerInterface $availabilityChecker,
137
        AddToCartCommandInterface $addCartItemCommand,
138
        OrderInterface $order,
139
        OrderItemInterface $orderItem,
140
        OrderItemInterface $existingOrderItem,
141
        ProductVariantInterface $productVariant
142
    ): void {
143
        $addCartItemCommand->getCart()->willReturn($order);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...r\Model\OrderInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...del\OrderItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
145
        $orderItem->getVariant()->willReturn($productVariant);
0 ignored issues
show
Bug introduced by
The method willReturn() does not seem to exist on object<Sylius\Component\...roductVariantInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
        $orderItem->getQuantity()->willReturn(10);
147
        $existingOrderItem->equals($orderItem)->willReturn(true);
148
149
        $order->getItems()->willReturn(new ArrayCollection([$existingOrderItem->getWrappedObject()]));
0 ignored issues
show
Bug introduced by
The method getWrappedObject() does not seem to exist on object<Sylius\Component\...del\OrderItemInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
        $existingOrderItem->getQuantity()->willReturn(10);
151
152
        $availabilityChecker->isStockSufficient($productVariant, 20)->willReturn(true);
153
154
        $executionContext->addViolation(Argument::any(), Argument::any())->shouldNotBeCalled();
155
156
        $cartItemAvailabilityConstraint = new CartItemAvailability();
157
        $cartItemAvailabilityConstraint->message = 'Insufficient stock';
158
159
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
160
    }
161
}
162