Skip to content

Commit 45ae1bd

Browse files
Added: Simple product creation barcode duplication behat test
1 parent b75e974 commit 45ae1bd

6 files changed

Lines changed: 90 additions & 3 deletions

File tree

features/adding_product_with_barcode.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ Feature: Adding a new product with a barcode
1919
And I set its barcode to "4006381333931"
2020
And I add it
2121
Then I should be notified that it has been successfully created
22-
And the product "Dice Brewing" should appear in the store
22+
And the product "Dice Brewing" should appear in the store
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@managing_product_barcodes
2+
Feature: Adding a new product with duplicated barcode
3+
In order to prevent barcode duplicates
4+
As an Administrator
5+
I want to see validation error when trying to add product with non-unique barcode
6+
7+
Background:
8+
Given the store operates on a single channel in "United States"
9+
And the store has "Standard" shipping category
10+
And the store has a product "Stabilo Point 88-57 Azure"
11+
And this product has barcode "4006381333931"
12+
And I am logged in as an administrator
13+
14+
@ui
15+
Scenario: Adding a new simple product with barcode
16+
Given I want to create a new simple product
17+
When I specify its code as "BOARD_DICE_BREWING"
18+
And I name it "Dice Brewing" in "English (United States)"
19+
And I set its slug to "dice-brewing" in "English (United States)"
20+
And I set its price to "$10.00" for "United States" channel
21+
And I set its barcode to "4006381333931"
22+
And I try to add it
23+
Then I should be notified that product with this barcode already exists
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Loevgaard\SyliusBarcodePlugin\Behat\Context\Setup;
6+
7+
use Behat\Behat\Context\Context;
8+
use Doctrine\Common\Persistence\ObjectManager;
9+
use Loevgaard\SyliusBarcodePlugin\Model\ProductVariantInterface;
10+
use Sylius\Component\Core\Model\ProductInterface;
11+
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;
12+
13+
final class ProductContext implements Context
14+
{
15+
/** @var ObjectManager */
16+
private $objectManager;
17+
18+
/** @var ProductVariantResolverInterface */
19+
private $defaultVariantResolver;
20+
21+
public function __construct(
22+
ObjectManager $objectManager,
23+
ProductVariantResolverInterface $defaultVariantResolver
24+
) {
25+
$this->objectManager = $objectManager;
26+
$this->defaultVariantResolver = $defaultVariantResolver;
27+
}
28+
29+
/**
30+
* @Given :product has barcode :barcode
31+
* @Given /^(this product) has barcode "([^"]+)"$/
32+
* @Given :product has no barcode
33+
* @Given /^(this product) has no barcode$/
34+
*/
35+
public function productHasBarcode(ProductInterface $product, ?string $barcode = null)
36+
{
37+
/** @var ProductVariantInterface $productVariant */
38+
$productVariant = $this->defaultVariantResolver->getVariant($product);
39+
$productVariant->setBarcode($barcode);
40+
41+
$this->objectManager->flush();
42+
}
43+
}

tests/Behat/Context/Ui/Admin/ManagingProductsContext.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Behat\Behat\Context\Context;
88
use Tests\Loevgaard\SyliusBarcodePlugin\Behat\Page\Admin\Product\CreateSimpleProductPage;
99
use Tests\Loevgaard\SyliusBarcodePlugin\Behat\Page\Admin\Product\UpdateSimpleProductPage;
10+
use Webmozart\Assert\Assert;
1011

1112
final class ManagingProductsContext implements Context
1213
{
@@ -33,4 +34,15 @@ public function iSetItsBarcodeTo($barcode): void
3334
{
3435
$this->createSimpleProductPage->setBarcode($barcode);
3536
}
37+
38+
/**
39+
* @Then I should be notified that product with this barcode already exists
40+
*/
41+
public function iShouldBeNotifiedThatBarcodeAlreadyExists()
42+
{
43+
Assert::same(
44+
$this->createSimpleProductPage->getValidationMessage('barcode'),
45+
'Variant barcode must be unique.'
46+
);
47+
}
3648
}

tests/Behat/Resources/services.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
<services>
77
<defaults public="true" />
88

9+
<service id="loevgaard_sylius_barcode.behat.context.setup.product"
10+
class="Tests\Loevgaard\SyliusBarcodePlugin\Behat\Context\Setup\ProductContext">
11+
<argument type="service" id="doctrine.orm.entity_manager" />
12+
<argument type="service" id="sylius.product_variant_resolver.default" />
13+
</service>
14+
915
<service id="loevgaard_sylius_barcode.behat.context.ui.admin.managing_products"
1016
class="Tests\Loevgaard\SyliusBarcodePlugin\Behat\Context\Ui\Admin\ManagingProductsContext">
1117
<argument type="service" id="loevgaard_sylius_barcode.behat.page.admin.product.create_simple" />

tests/Behat/Resources/suites.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ default:
55
- sylius.behat.context.hook.doctrine_orm
66

77
- sylius.behat.context.transform.locale
8+
- sylius.behat.context.transform.product
9+
- sylius.behat.context.transform.shared_storage
810

911
- sylius.behat.context.setup.channel
1012
- sylius.behat.context.setup.admin_security
1113
- sylius.behat.context.setup.shipping_category
14+
- sylius.behat.context.setup.product
15+
- loevgaard_sylius_barcode.behat.context.setup.product
1216

1317
- sylius.behat.context.ui.admin.managing_products
14-
- sylius.behat.context.ui.admin.notification
15-
1618
- loevgaard_sylius_barcode.behat.context.ui.admin.managing_products
19+
- sylius.behat.context.ui.admin.notification
1720
filters:
1821
tags: "@managing_product_barcodes && @ui"

0 commit comments

Comments
 (0)