Completed
Push — master ( cb017b...970228 )
by Paweł
30:33 queued 20:21
created

thisVariantShouldHaveAItemCurrentlyInStock()   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 Sylius\Behat\Context\Ui\Admin;
13
14
use Behat\Behat\Context\Context;
15
use Sylius\Behat\Page\Admin\ProductVariant\IndexPageInterface;
16
use Sylius\Component\Core\Model\ProductInterface;
17
use Sylius\Component\Core\Model\ProductVariantInterface;
18
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
19
use Webmozart\Assert\Assert;
20
21
/**
22
 * @author Kamil Kokot <[email protected]>
23
 */
24
final class BrowsingProductVariantsContext implements Context
25
{
26
    /**
27
     * @var IndexPageInterface
28
     */
29
    private $indexPage;
30
31
    /**
32
     * @var ProductVariantResolverInterface
33
     */
34
    private $defaultProductVariantResolver;
35
36
    /**
37
     * @param IndexPageInterface $indexPage
38
     * @param ProductVariantResolverInterface $defaultProductVariantResolver
39
     */
40
    public function __construct(
41
        IndexPageInterface $indexPage,
42
        ProductVariantResolverInterface $defaultProductVariantResolver
43
    ) {
44
        $this->indexPage = $indexPage;
45
        $this->defaultProductVariantResolver = $defaultProductVariantResolver;
46
    }
47
48
    /**
49
     * @When I start sorting variants by :field
50
     */
51
    public function iSortProductsBy($field)
52
    {
53
        $this->indexPage->sortBy($field);
54
    }
55
56
    /**
57
     * @Then the :productVariantCode variant of the :product product should appear in the store
58
     */
59
    public function theProductVariantShouldAppearInTheShop($productVariantCode, ProductInterface $product)
60
    {
61
        $this->indexPage->open(['productId' => $product->getId()]);
62
63
        Assert::true($this->indexPage->isSingleResourceOnPage(['code' => $productVariantCode]));
64
    }
65
66
    /**
67
     * @Then the :productVariantCode variant of the :product product should not appear in the store
68
     */
69
    public function theProductVariantShouldNotAppearInTheShop($productVariantCode, ProductInterface $product)
70
    {
71
        $this->indexPage->open(['productId' => $product->getId()]);
72
73
        Assert::false($this->indexPage->isSingleResourceOnPage(['code' => $productVariantCode]));
74
    }
75
76
    /**
77
     * @Then the :product product should have no variants
78
     */
79
    public function theProductShouldHaveNoVariants(ProductInterface $product)
80
    {
81
        $this->indexPage->open(['productId' => $product->getId()]);
82
83
        $this->assertNumberOfVariantsOnProductPage(0);
84
    }
85
86
    /**
87
     * @Then the :product product should have only one variant
88
     */
89
    public function theProductShouldHaveOnlyOneVariant(ProductInterface $product)
90
    {
91
        $this->indexPage->open(['productId' => $product->getId()]);
92
93
        $this->assertNumberOfVariantsOnProductPage(1);
94
    }
95
96
    /**
97
     * @When /^I (?:|want to )view all variants of (this product)$/
98
     * @When /^I view(?:| all) variants of the (product "[^"]+")$/
99
     */
100
    public function iWantToViewAllVariantsOfThisProduct(ProductInterface $product)
101
    {
102
        $this->indexPage->open(['productId' => $product->getId()]);
103
    }
104
105
    /**
106
     * @Then I should see :numberOfProductVariants variants in the list
107
     * @Then I should see :numberOfProductVariants variant in the list
108
     * @Then I should not see any variants in the list
109
     */
110
    public function iShouldSeeProductVariantsInTheList($numberOfProductVariants = 0)
111
    {
112
        Assert::same($this->indexPage->countItems(), (int) $numberOfProductVariants);
113
    }
114
115
    /**
116
     * @Then /^(this variant) should not exist in the product catalog$/
117
     */
118
    public function productVariantShouldNotExist(ProductVariantInterface $productVariant)
119
    {
120
        $this->indexPage->open(['productId' => $productVariant->getProduct()->getId()]);
121
122
        Assert::false($this->indexPage->isSingleResourceOnPage(['name' => $productVariant->getName()]));
123
    }
124
125
    /**
126
     * @Then /^(this variant) should still exist in the product catalog$/
127
     */
128
    public function productShouldExistInTheProductCatalog(ProductVariantInterface $productVariant)
129
    {
130
        $this->theProductVariantShouldAppearInTheShop($productVariant->getCode(), $productVariant->getProduct());
0 ignored issues
show
Compatibility introduced by
$productVariant->getProduct() of type object<Sylius\Component\...Model\ProductInterface> is not a sub-type of object<Sylius\Component\...Model\ProductInterface>. It seems like you assume a child interface of the interface Sylius\Component\Product\Model\ProductInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
131
    }
132
133
    /**
134
     * @Then /^the variant "([^"]+)" should have (\d+) items on hand$/
135
     */
136
    public function thisVariantShouldHaveItemsOnHand($productVariantName, $quantity)
137
    {
138
        Assert::true($this->indexPage->isSingleResourceWithSpecificElementOnPage(
139
            ['name' => $productVariantName],
140
            sprintf('td > div.ui.label:contains("%s")', $quantity)
141
        ));
142
    }
143
144
    /**
145
     * @Then /^the "([^"]+)" variant of ("[^"]+" product) should have (\d+) items on hand$/
146
     */
147
    public function theVariantOfProductShouldHaveItemsOnHand($productVariantName, ProductInterface $product, $quantity)
148
    {
149
        $this->indexPage->open(['productId' => $product->getId()]);
150
151
        Assert::true($this->indexPage->isSingleResourceWithSpecificElementOnPage(
152
            ['name' => $productVariantName],
153
            sprintf('td > div.ui.label:contains("%s")', $quantity)
154
        ));
155
    }
156
157
    /**
158
     * @Then /^I should see that the ("([^"]+)" variant) is not tracked$/
159
     */
160
    public function iShouldSeeThatIsNotTracked(ProductVariantInterface $productVariant)
161
    {
162
        Assert::true($this->indexPage->isSingleResourceOnPage([
163
            'name' => $productVariant->getName(),
164
            'inventory' => 'Not tracked',
165
        ]));
166
    }
167
168
    /**
169
     * @Then /^I should see that the ("[^"]+" variant) has zero on hand quantity$/
170
     */
171
    public function iShouldSeeThatTheVariantHasZeroOnHandQuantity(ProductVariantInterface $productVariant)
172
    {
173
        Assert::true($this->indexPage->isSingleResourceOnPage([
174
            'name' => $productVariant->getName(),
175
            'inventory' => '0 Available on hand',
176
        ]));
177
    }
178
179
    /**
180
     * @Then /^(\d+) units of (this product) should be on hold$/
181
     */
182
    public function unitsOfThisProductShouldBeOnHold($quantity, ProductInterface $product)
183
    {
184
        /** @var ProductVariantInterface $variant */
185
        $variant = $this->defaultProductVariantResolver->getVariant($product);
186
187
        $this->assertOnHoldQuantityOfVariant($quantity, $variant);
188
    }
189
190
    /**
191
     * @Then /^(\d+) units of (this product) should be on hand$/
192
     */
193
    public function unitsOfThisProductShouldBeOnHand($quantity, ProductInterface $product)
194
    {
195
        /** @var ProductVariantInterface $variant */
196
        $variant = $this->defaultProductVariantResolver->getVariant($product);
197
198
        Assert::same($this->indexPage->getOnHandQuantityFor($variant), (int) $quantity);
199
    }
200
201
    /**
202
     * @Then /^there should be no units of (this product) on hold$/
203
     */
204
    public function thereShouldBeNoUnitsOfThisProductOnHold(ProductInterface $product)
205
    {
206
        /** @var ProductVariantInterface $variant */
207
        $variant = $this->defaultProductVariantResolver->getVariant($product);
208
209
        $this->assertOnHoldQuantityOfVariant(0, $variant);
210
    }
211
212
    /**
213
     * @Then the :variant variant should have :amount items on hold
214
     */
215
    public function thisVariantShouldHaveItemsOnHold(ProductVariantInterface $variant, $amount)
216
    {
217
        $this->assertOnHoldQuantityOfVariant((int) $amount, $variant);
218
    }
219
220
    /**
221
     * @Then the :variant variant of :product product should have :amount items on hold
222
     */
223
    public function theVariantOfProductShouldHaveItemsOnHold(ProductVariantInterface $variant, ProductInterface $product, $amount)
224
    {
225
        $this->indexPage->open(['productId' => $product->getId()]);
226
227
        $this->assertOnHoldQuantityOfVariant((int) $amount, $variant);
228
    }
229
230
    /**
231
     * @Then the first variant in the list should have :field :value
232
     */
233
    public function theFirstVariantInTheListShouldHave($field, $value)
234
    {
235
        Assert::same($this->indexPage->getColumnFields($field)[0], $value);
236
    }
237
238
    /**
239
     * @Then the last variant in the list should have :field :value
240
     */
241
    public function theLastVariantInTheListShouldHave($field, $value)
242
    {
243
        $values = $this->indexPage->getColumnFields($field);
244
245
        Assert::same(end($values), $value);
246
    }
247
248
    /**
249
     * @Then /^(this variant) should have a (\d+) item currently in stock$/
250
     */
251
    public function thisVariantShouldHaveAItemCurrentlyInStock(ProductVariantInterface $productVariant, $amountInStock)
252
    {
253
        $this->indexPage->open(['productId' => $productVariant->getProduct()->getId()]);
254
255
        Assert::same($this->indexPage->getOnHandQuantityFor($productVariant), (int) $amountInStock);
256
    }
257
258
    /**
259
     * @param int $expectedAmount
260
     * @param ProductVariantInterface $variant
261
     *
262
     * @throws \InvalidArgumentException
263
     */
264
    private function assertOnHoldQuantityOfVariant($expectedAmount, $variant)
265
    {
266
        $actualAmount = $this->indexPage->getOnHoldQuantityFor($variant);
267
268
        Assert::same(
269
            $actualAmount,
270
            (int) $expectedAmount,
271
            sprintf(
272
                'Unexpected on hold quantity for "%s" variant. It should be "%s" but is "%s"',
273
                $variant->getName(),
274
                $expectedAmount,
275
                $actualAmount
276
            )
277
        );
278
    }
279
280
    /**
281
     * @param int $amount
282
     */
283
    private function assertNumberOfVariantsOnProductPage($amount)
284
    {
285
        Assert::same((int) $this->indexPage->countItems(), $amount, 'Product has %d variants, but should have %d');
286
    }
287
}
288