Completed
Push — master ( b9c485...94fed1 )
by Michał
13:51
created

src/Sylius/Behat/Context/Setup/ProductContext.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Setup;
13
14
use Behat\Behat\Context\Context;
15
use Behat\Gherkin\Node\TableNode;
16
use Doctrine\Common\Persistence\ObjectManager;
17
use Sylius\Component\Attribute\Factory\AttributeFactoryInterface;
18
use Sylius\Component\Core\Formatter\StringInflector;
19
use Sylius\Component\Core\Model\ChannelInterface;
20
use Sylius\Component\Core\Model\ProductInterface;
21
use Sylius\Component\Core\Model\ProductVariantInterface;
22
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
23
use Sylius\Behat\Service\SharedStorageInterface;
24
use Sylius\Component\Product\Factory\ProductFactoryInterface;
25
use Sylius\Component\Product\Model\AttributeInterface;
26
use Sylius\Component\Product\Model\AttributeValueInterface;
27
use Sylius\Component\Product\Model\OptionInterface;
28
use Sylius\Component\Product\Model\OptionValueInterface;
29
use Sylius\Component\Resource\Factory\FactoryInterface;
30
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
31
use Sylius\Component\Variation\Resolver\VariantResolverInterface;
32
33
/**
34
 * @author Arkadiusz Krakowiak <[email protected]>
35
 * @author Mateusz Zalewski <[email protected]>
36
 * @author Magdalena Banasiak <[email protected]>
37
 */
38
final class ProductContext implements Context
39
{
40
    /**
41
     * @var SharedStorageInterface
42
     */
43
    private $sharedStorage;
44
45
    /**
46
     * @var ProductRepositoryInterface
47
     */
48
    private $productRepository;
49
50
    /**
51
     * @var ProductFactoryInterface
52
     */
53
    private $productFactory;
54
55
    /**
56
     * @var AttributeFactoryInterface
57
     */
58
    private $productAttributeFactory;
59
60
    /**
61
     * @var FactoryInterface
62
     */
63
    private $productVariantFactory;
64
65
    /**
66
     * @var FactoryInterface
67
     */
68
    private $attributeValueFactory;
69
70
    /**
71
     * @var FactoryInterface
72
     */
73
    private $productOptionFactory;
74
75
    /**
76
     * @var FactoryInterface
77
     */
78
    private $productOptionValueFactory;
79
80
    /**
81
     * @var ObjectManager
82
     */
83
    private $objectManager;
84
85
    /**
86
     * @var VariantResolverInterface
87
     */
88
    private $defaultVariantResolver;
89
90
    /**
91
     * @param SharedStorageInterface $sharedStorage
92
     * @param ProductRepositoryInterface $productRepository
93
     * @param ProductFactoryInterface $productFactory
94
     * @param AttributeFactoryInterface $productAttributeFactory
95
     * @param FactoryInterface $productVariantFactory
96
     * @param FactoryInterface $attributeValueFactory
97
     * @param FactoryInterface $productOptionFactory
98
     * @param FactoryInterface $productOptionValueFactory
99
     * @param ObjectManager $objectManager
100
     * @param VariantResolverInterface $defaultVariantResolver
101
     */
102
    public function __construct(
103
        SharedStorageInterface $sharedStorage,
104
        ProductRepositoryInterface $productRepository,
105
        ProductFactoryInterface $productFactory,
106
        AttributeFactoryInterface $productAttributeFactory,
107
        FactoryInterface $attributeValueFactory,
108
        FactoryInterface $productVariantFactory,
109
        FactoryInterface $productOptionFactory,
110
        FactoryInterface $productOptionValueFactory,
111
        ObjectManager $objectManager,
112
        VariantResolverInterface $defaultVariantResolver
113
    ) {
114
        $this->sharedStorage = $sharedStorage;
115
        $this->productRepository = $productRepository;
116
        $this->productFactory = $productFactory;
117
        $this->productAttributeFactory = $productAttributeFactory;
118
        $this->attributeValueFactory = $attributeValueFactory;
119
        $this->productVariantFactory = $productVariantFactory;
120
        $this->productOptionFactory = $productOptionFactory;
121
        $this->productOptionValueFactory = $productOptionValueFactory;
122
        $this->objectManager = $objectManager;
123
        $this->defaultVariantResolver = $defaultVariantResolver;
124
    }
125
126
    /**
127
     * @Given the store has a product :productName
128
     * @Given the store has a :productName product
129
     * @Given /^the store has a product "([^"]+)" priced at ("[^"]+")$/
130
     */
131
    public function storeHasAProductPricedAt($productName, $price = 0)
132
    {
133
        $product = $this->createProduct($productName, $price);
134
135
        $product->setDescription('Awesome '.$productName);
136
137
        if ($this->sharedStorage->has('channel')) {
138
            $channel = $this->sharedStorage->get('channel');
139
            $product->addChannel($channel);
140
        }
141
142
        $this->saveProduct($product);
143
    }
144
145
    /**
146
     * @Given the store has a :productName configurable product
147
     */
148
    public function storeHasAConfigurableProduct($productName)
149
    {
150
        $product = $this->productFactory->createNew();
151
152
        $product->setName($productName);
153
        $product->setCode($this->convertToCode($productName));
154
        $product->setDescription('Awesome '.$productName);
155
156
        $this->saveProduct($product);
157
    }
158
159
    /**
160
     * @Given the store has :firstProductName and :secondProductName products
161
     */
162
    public function theStoreHasAProductAnd($firstProductName, $secondProductName)
163
    {
164
        $this->saveProduct($this->createProduct($firstProductName));
165
        $this->saveProduct($this->createProduct($secondProductName));
166
    }
167
168
    /**
169
     * @Given /^the (product "[^"]+") has "([^"]+)" variant priced at ("[^"]+")$/
170
     * @Given /^(this product) has "([^"]+)" variant priced at ("[^"]+")$/
171
     */
172
    public function theProductHasVariantPricedAt(ProductInterface $product, $productVariantName, $price)
173
    {
174
        /** @var ProductVariantInterface $variant */
175
        $variant = $this->productVariantFactory->createNew();
176
177
        $variant->setName($productVariantName);
178
        $variant->setCode($this->convertToCode($productVariantName));
179
        $variant->setPrice($price);
180
        $variant->setProduct($product);
181
        $product->addVariant($variant);
182
183
        $this->objectManager->flush();
184
185
        $this->sharedStorage->set('variant', $variant);
186
    }
187
188
    /**
189
     * @Given /^there is product "([^"]+)" available in ((?:this|that|"[^"]+") channel)$/
190
     * @Given /^the store has a product "([^"]+)" available in ("([^"]+)" channel)$/
191
     */
192
    public function thereIsProductAvailableInGivenChannel($productName, ChannelInterface $channel)
193
    {
194
        $product = $this->createProduct($productName);
195
196
        $product->setDescription('Awesome ' . $productName);
197
        $product->addChannel($channel);
198
199
        $this->saveProduct($product);
200
    }
201
202
    /**
203
     * @Given /^([^"]+) belongs to ("[^"]+" tax category)$/
204
     */
205
    public function productBelongsToTaxCategory(ProductInterface $product, TaxCategoryInterface $taxCategory)
206
    {
207
        $variant = $this->defaultVariantResolver->getVariant($product);
208
        $variant->setTaxCategory($taxCategory);
209
        $this->objectManager->flush();
210
    }
211
212
    /**
213
     * @Given /^(it) comes in the following variations:$/
214
     */
215
    public function itComesInTheFollowingVariations(ProductInterface $product, TableNode $table)
216
    {
217
        foreach ($table->getHash() as $variantHash) {
218
            /** @var ProductVariantInterface $variant */
219
            $variant = $this->productVariantFactory->createNew();
220
221
            $variant->setName($variantHash['name']);
222
            $variant->setCode($this->convertToCode($variantHash['name']));
223
            $variant->setPrice($this->getPriceFromString(str_replace(['$', '€', '£'], '', $variantHash['price'])));
224
            $variant->setProduct($product);
225
            $product->addVariant($variant);
226
        }
227
228
        $this->objectManager->flush();
229
    }
230
231
    /**
232
     * @Given /^("[^"]+" variant of product "[^"]+") belongs to ("[^"]+" tax category)$/
233
     */
234
    public function productVariantBelongsToTaxCategory(
235
        ProductVariantInterface $productVariant,
236
        TaxCategoryInterface $taxCategory
237
    ) {
238
        $productVariant->setTaxCategory($taxCategory);
239
        $this->objectManager->flush($productVariant);
240
    }
241
242
    /**
243
     * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with value "([^"]+)"$/
244
     */
245
    public function thisProductHasAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value)
246
    {
247
        $attribute = $this->createProductAttribute($productAttributeType,$productAttributeName);
248
        $attributeValue = $this->createProductAttributeValue($value, $attribute);
249
        $product->addAttribute($attributeValue);
250
251
        $this->objectManager->flush();
252
    }
253
254
    /**
255
     * @Given /^(this product) has percent attribute "([^"]+)" with value ([^"]+)%$/
256
     */
257
    public function thisProductHasPercentAttributeWithValue(ProductInterface $product, $productAttributeName, $value)
258
    {
259
        $attribute = $this->createProductAttribute('percent',$productAttributeName);
260
        $attributeValue = $this->createProductAttributeValue($value/100, $attribute);
261
        $product->addAttribute($attributeValue);
262
263
        $this->objectManager->flush();
264
    }
265
266
    /**
267
     * @Given /^(this product) has ([^"]+) attribute "([^"]+)" set to "([^"]+)"$/
268
     */
269
    public function thisProductHasCheckboxAttributeWithValue(ProductInterface $product, $productAttributeType, $productAttributeName, $value)
270
    {
271
        $attribute = $this->createProductAttribute($productAttributeType, $productAttributeName);
272
        $booleanValue = ('Yes' === $value);
273
        $attributeValue = $this->createProductAttributeValue($booleanValue, $attribute);
0 ignored issues
show
$booleanValue is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
274
        $product->addAttribute($attributeValue);
275
276
        $this->objectManager->flush();
277
    }
278
279
    /**
280
     * @Given /^(this product) has ([^"]+) attribute "([^"]+)" with date "([^"]+)"$/
281
     */
282
    public function thisProductHasDateTimeAttributeWithDate(ProductInterface $product, $productAttributeType, $productAttributeName, $date)
283
    {
284
        $attribute = $this->createProductAttribute($productAttributeType, $productAttributeName);
285
        $attributeValue = $this->createProductAttributeValue(new \DateTime($date), $attribute);
0 ignored issues
show
new \DateTime($date) is of type object<DateTime>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
286
287
        $product->addAttribute($attributeValue);
288
289
        $this->objectManager->flush();
290
    }
291
292
    /**
293
     * @Given /^(this product) has option "([^"]+)" with values "([^"]+)" and "([^"]+)"$/
294
     */
295
    public function thisProductHasOptionWithValues(ProductInterface $product, $optionName, $firstValue, $secondValue)
296
    {
297
        /** @var OptionInterface $variant */
298
        $option = $this->productOptionFactory->createNew();
299
300
        $option->setName($optionName);
301
        $option->setCode('PO1');
302
303
        /** @var OptionValueInterface $optionValue */
304
        $firstOptionValue = $this->productOptionValueFactory->createNew();
305
306
        $firstOptionValue->setValue($firstValue);
307
        $firstOptionValue->setCode('POV1');
308
        $firstOptionValue->setOption($option);
309
310
        /** @var OptionValueInterface $optionValue */
311
        $secondOptionValue = $this->productOptionValueFactory->createNew();
312
313
        $secondOptionValue->setValue($secondValue);
314
        $secondOptionValue->setCode('POV2');
315
        $secondOptionValue->setOption($option);
316
317
        $option->addValue($firstOptionValue);
318
        $option->addValue($secondOptionValue);
319
320
        $product->addOption($option);
321
        $product->setVariantSelectionMethod(ProductInterface::VARIANT_SELECTION_MATCH);
322
323
        $this->sharedStorage->set(sprintf('%s_option',$optionName), $option);
324
        $this->sharedStorage->set(sprintf('%s_option_value',$firstValue), $firstOptionValue);
325
        $this->sharedStorage->set(sprintf('%s_option_value',$secondValue), $secondOptionValue);
326
327
        $this->objectManager->persist($option);
328
        $this->objectManager->persist($firstOptionValue);
329
        $this->objectManager->persist($secondOptionValue);
330
        $this->objectManager->flush();
331
    }
332
333
    /**
334
     * @Given /^there (?:is|are) (\d+) item(?:s) of (product "([^"]+)") available in the inventory$/
335
     */
336
    public function thereIsQuantityOfProducts($quantity, ProductInterface $product)
337
    {
338
        $this->setProductsQuantity($product, $quantity);
339
    }
340
341
    /**
342
     * @Given /^the (product "([^"]+)") is out of stock$/
343
     */
344
    public function theProductIsNotAvailable(ProductInterface $product)
345
    {
346
        $product->getFirstVariant()->setTracked(true);
347
348
        $this->setProductsQuantity($product, 0);
349
    }
350
351
    /**
352
     * @Given /^(this product) is available in "([^"]+)" size priced at ("[^"]+")$/
353
     */
354
    public function thisProductIsAvailableInSize(ProductInterface $product, $optionValueName, $price)
355
    {
356
        /** @var ProductVariantInterface $variant */
357
        $variant = $this->productVariantFactory->createNew();
358
359
        $optionValue = $this->sharedStorage->get(sprintf('%s_option_value',$optionValueName));
360
361
        $variant->addOption($optionValue);
362
        $variant->setPrice($price);
363
        $variant->setCode(sprintf("%s_%s", $product->getCode(), $optionValueName));
364
365
        $product->addVariant($variant);
366
        $this->objectManager->flush();
367
    }
368
369
    /**
370
     * @Given /^(this product) has (this product option)$/
371
     * @Given /^(this product) has a ("[^"]+" option)$/
372
     * @Given /^(this product) has an ("[^"]+" option)$/
373
     */
374
    public function thisProductHasThisProductOption(ProductInterface $product, OptionInterface $option)
375
    {
376
        $product->addOption($option);
377
378
        $this->objectManager->flush();
379
    }
380
381
    /**
382
     * @param string $type
383
     * @param string $name
384
     * @param string $code
385
     *
386
     * @return AttributeInterface
387
     */
388
    private function createProductAttribute($type, $name, $code = 'PA112')
389
    {
390
        $productAttribute = $this->productAttributeFactory->createTyped($type);
391
        $productAttribute->setCode($code);
392
        $productAttribute->setName($name);
393
394
        $this->objectManager->persist($productAttribute);
395
396
        return $productAttribute;
397
    }
398
399
    /**
400
     * @param string $value
401
     *
402
     * @return AttributeValueInterface
403
     */
404
    private function createProductAttributeValue($value, AttributeInterface $attribute)
405
    {
406
        /** @var AttributeValueInterface $attributeValue */
407
        $attributeValue = $this->attributeValueFactory->createNew();
408
        $attributeValue->setAttribute($attribute);
409
        $attributeValue->setValue($value);
410
411
        $this->objectManager->persist($attributeValue);
412
413
        return $attributeValue;
414
    }
415
416
    /**
417
     * @param string $price
418
     *
419
     * @return int
420
     */
421
    private function getPriceFromString($price)
422
    {
423
        return (int) round(($price * 100), 2);
424
    }
425
426
    /**
427
     * @param string $productName
428
     * @param int $price
429
     *
430
     * @return ProductInterface
431
     */
432
    private function createProduct($productName, $price = 0)
433
    {
434
        /** @var ProductInterface $product */
435
        $product = $this->productFactory->createWithVariant();
436
437
        $product->setName($productName);
438
        $product->setCode($this->convertToCode($productName));
439
440
        $variant = $this->defaultVariantResolver->getVariant($product);
441
        $variant->setPrice($price);
442
        $variant->setCode($product->getCode());
443
444
        return $product;
445
    }
446
447
    /**
448
     * @param ProductInterface $product
449
     * @param int $quantity
450
     */
451
    private function setProductsQuantity(ProductInterface $product, $quantity)
452
    {
453
        $product->getFirstVariant()->setOnHand($quantity);
454
455
        $this->saveProduct($product);
456
    }
457
458
    /**
459
     * @param ProductInterface $product
460
     */
461
    private function saveProduct(ProductInterface $product)
462
    {
463
        $this->productRepository->add($product);
464
        $this->sharedStorage->set('product', $product);
465
    }
466
467
    /**
468
     * @param string $productName
469
     *
470
     * @return string
471
     */
472
    private function convertToCode($productName)
473
    {
474
        return StringInflector::nameToUpercaseCode($productName);
475
    }
476
}
477