Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

CartItemAvailabilityValidatorSpec::let()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
namespace spec\Sylius\Bundle\CoreBundle\Validator\Constraints;
13
14
use PhpSpec\ObjectBehavior;
15
use Prophecy\Argument;
16
use Sylius\Bundle\CoreBundle\Validator\Constraints\CartItemAvailability;
17
use Sylius\Bundle\CoreBundle\Validator\Constraints\CartItemAvailabilityValidator;
18
use Sylius\Bundle\InventoryBundle\Validator\Constraints\InStock;
19
use Sylius\Bundle\OrderBundle\Controller\AddToCartCommandInterface;
20
use Sylius\Component\Core\Model\OrderInterface;
21
use Sylius\Component\Core\Model\OrderItemInterface;
22
use Sylius\Component\Core\Model\ProductVariantInterface;
23
use Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface;
24
use Symfony\Component\Validator\ConstraintValidator;
25
use Symfony\Component\Validator\Context\ExecutionContextInterface;
26
27
/**
28
 * @author Arkadiusz Krakowiak <[email protected]>
29
 */
30
final class CartItemAvailabilityValidatorSpec extends ObjectBehavior
31
{
32
    function let(ExecutionContextInterface $executionContext, AvailabilityCheckerInterface $availabilityChecker)
33
    {
34
        $this->beConstructedWith($availabilityChecker);
35
36
        $this->initialize($executionContext);
37
    }
38
39
    function it_is_initializable()
40
    {
41
        $this->shouldHaveType(CartItemAvailabilityValidator::class);
42
    }
43
44
    function it_is_constraint_validator()
45
    {
46
        $this->shouldHaveType(ConstraintValidator::class);
47
    }
48
49
    function it_validates_only_add_cart_item_commands(OrderInterface $order)
50
    {
51
        $cartItemAvailabilityConstraint = new CartItemAvailability();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $cartItemAvailabilityConstraint exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
52
53
        $this->shouldThrow(\InvalidArgumentException::class)->during('validate', [$order, $cartItemAvailabilityConstraint]);
54
    }
55
56
    function it_is_cart_item_availability_validator(AddToCartCommandInterface $addCartItemCommand)
57
    {
58
        $inStockConstraint = new InStock();
59
60
        $this->shouldThrow(\InvalidArgumentException::class)->during('validate', [$addCartItemCommand, $inStockConstraint]);
61
    }
62
63
    function it_does_not_add_violation_if_requested_cart_item_is_available(
64
        ExecutionContextInterface $executionContext,
65
        AvailabilityCheckerInterface $availabilityChecker,
66
        AddToCartCommandInterface $addCartItemCommand,
67
        OrderInterface $order,
68
        OrderItemInterface $orderItem,
69
        ProductVariantInterface $productVariant
70
    ) {
71
        $addCartItemCommand->getCart()->willReturn($order);
72
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
73
        $orderItem->getVariant()->willReturn($productVariant);
74
        $orderItem->getQuantity()->willReturn(10);
75
        $order->getItems()->willReturn([]);
76
77
        $availabilityChecker->isStockSufficient($productVariant, 10)->willReturn(true);
78
79
        $executionContext->addViolation(Argument::any(), Argument::any())->shouldNotBeCalled();
80
81
        $cartItemAvailabilityConstraint = new CartItemAvailability();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $cartItemAvailabilityConstraint exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
82
83
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
84
    }
85
86
    function it_adds_violation_if_requested_cart_item_is_not_available(
87
        ExecutionContextInterface $executionContext,
88
        AvailabilityCheckerInterface $availabilityChecker,
89
        AddToCartCommandInterface $addCartItemCommand,
90
        OrderInterface $order,
91
        OrderItemInterface $orderItem,
92
        ProductVariantInterface $productVariant
93
    ) {
94
        $addCartItemCommand->getCart()->willReturn($order);
95
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
96
        $orderItem->getVariant()->willReturn($productVariant);
97
        $orderItem->getQuantity()->willReturn(10);
98
        $order->getItems()->willReturn([]);
99
        $productVariant->getInventoryName()->willReturn('Mug');
100
101
        $availabilityChecker->isStockSufficient($productVariant, 10)->willReturn(false);
102
103
        $executionContext->addViolation('Insufficient stock', ['%itemName%' => 'Mug'])->shouldBeCalled();
104
105
        $cartItemAvailabilityConstraint = new CartItemAvailability();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $cartItemAvailabilityConstraint exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
106
        $cartItemAvailabilityConstraint->message = 'Insufficient stock';
107
108
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
109
    }
110
111
    function it_adds_violation_if_total_quantity_of_cart_items_exceed_available_quantity(
112
        ExecutionContextInterface $executionContext,
113
        AvailabilityCheckerInterface $availabilityChecker,
114
        AddToCartCommandInterface $addCartItemCommand,
115
        OrderInterface $order,
116
        OrderItemInterface $orderItem,
117
        OrderItemInterface $existingOrderItem,
118
        ProductVariantInterface $productVariant
119
    ) {
120
        $addCartItemCommand->getCart()->willReturn($order);
121
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
122
        $orderItem->getVariant()->willReturn($productVariant);
123
        $orderItem->getQuantity()->willReturn(10);
124
        $productVariant->getInventoryName()->willReturn('Mug');
125
126
        $order->getItems()->willReturn([$existingOrderItem]);
127
        $existingOrderItem->getQuantity()->willReturn(10);
128
        $existingOrderItem->equals($orderItem)->willReturn(true);
129
130
        $availabilityChecker->isStockSufficient($productVariant, 20)->willReturn(false);
131
132
        $executionContext->addViolation('Insufficient stock', ['%itemName%' => 'Mug'])->shouldBeCalled();
133
134
        $cartItemAvailabilityConstraint = new CartItemAvailability();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $cartItemAvailabilityConstraint exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
135
        $cartItemAvailabilityConstraint->message = 'Insufficient stock';
136
137
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
138
    }
139
140
    function it_does_not_add_violation_if_total_quantity_of_cart_items_do_not_exceed_available_quantity(
141
        ExecutionContextInterface $executionContext,
142
        AvailabilityCheckerInterface $availabilityChecker,
143
        AddToCartCommandInterface $addCartItemCommand,
144
        OrderInterface $order,
145
        OrderItemInterface $orderItem,
146
        OrderItemInterface $existingOrderItem,
147
        ProductVariantInterface $productVariant
148
    ) {
149
        $addCartItemCommand->getCart()->willReturn($order);
150
        $addCartItemCommand->getCartItem()->willReturn($orderItem);
151
        $orderItem->getVariant()->willReturn($productVariant);
152
        $orderItem->getQuantity()->willReturn(10);
153
        $existingOrderItem->equals($orderItem)->willReturn(true);
154
155
        $order->getItems()->willReturn([$existingOrderItem]);
156
        $existingOrderItem->getQuantity()->willReturn(10);
157
158
        $availabilityChecker->isStockSufficient($productVariant, 20)->willReturn(true);
159
160
        $executionContext->addViolation(Argument::any(), Argument::any())->shouldNotBeCalled();
161
162
        $cartItemAvailabilityConstraint = new CartItemAvailability();
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $cartItemAvailabilityConstraint exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
163
        $cartItemAvailabilityConstraint->message = 'Insufficient stock';
164
165
        $this->validate($addCartItemCommand, $cartItemAvailabilityConstraint);
166
    }
167
}
168