Skip to content

Commit

Permalink
Added: Simple product creation barcode duplication behat test
Browse files Browse the repository at this point in the history
  • Loading branch information
igormukhingmailcom committed Sep 21, 2019
1 parent b75e974 commit 45ae1bd
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion features/adding_product_with_barcode.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ Feature: Adding a new product with a barcode
And I set its barcode to "4006381333931"
And I add it
Then I should be notified that it has been successfully created
And the product "Dice Brewing" should appear in the store
And the product "Dice Brewing" should appear in the store
23 changes: 23 additions & 0 deletions features/adding_product_with_duplicated_barcode.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@managing_product_barcodes
Feature: Adding a new product with duplicated barcode
In order to prevent barcode duplicates
As an Administrator
I want to see validation error when trying to add product with non-unique barcode

Background:
Given the store operates on a single channel in "United States"
And the store has "Standard" shipping category
And the store has a product "Stabilo Point 88-57 Azure"
And this product has barcode "4006381333931"
And I am logged in as an administrator

@ui
Scenario: Adding a new simple product with barcode
Given I want to create a new simple product
When I specify its code as "BOARD_DICE_BREWING"
And I name it "Dice Brewing" in "English (United States)"
And I set its slug to "dice-brewing" in "English (United States)"
And I set its price to "$10.00" for "United States" channel
And I set its barcode to "4006381333931"
And I try to add it
Then I should be notified that product with this barcode already exists
43 changes: 43 additions & 0 deletions tests/Behat/Context/Setup/ProductContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Tests\Loevgaard\SyliusBarcodePlugin\Behat\Context\Setup;

use Behat\Behat\Context\Context;
use Doctrine\Common\Persistence\ObjectManager;
use Loevgaard\SyliusBarcodePlugin\Model\ProductVariantInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Product\Resolver\ProductVariantResolverInterface;

final class ProductContext implements Context
{
/** @var ObjectManager */
private $objectManager;

/** @var ProductVariantResolverInterface */
private $defaultVariantResolver;

public function __construct(
ObjectManager $objectManager,
ProductVariantResolverInterface $defaultVariantResolver
) {
$this->objectManager = $objectManager;
$this->defaultVariantResolver = $defaultVariantResolver;
}

/**
* @Given :product has barcode :barcode
* @Given /^(this product) has barcode "([^"]+)"$/
* @Given :product has no barcode
* @Given /^(this product) has no barcode$/
*/
public function productHasBarcode(ProductInterface $product, ?string $barcode = null)
{
/** @var ProductVariantInterface $productVariant */
$productVariant = $this->defaultVariantResolver->getVariant($product);
$productVariant->setBarcode($barcode);

$this->objectManager->flush();
}
}
12 changes: 12 additions & 0 deletions tests/Behat/Context/Ui/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Behat\Behat\Context\Context;
use Tests\Loevgaard\SyliusBarcodePlugin\Behat\Page\Admin\Product\CreateSimpleProductPage;
use Tests\Loevgaard\SyliusBarcodePlugin\Behat\Page\Admin\Product\UpdateSimpleProductPage;
use Webmozart\Assert\Assert;

final class ManagingProductsContext implements Context
{
Expand All @@ -33,4 +34,15 @@ public function iSetItsBarcodeTo($barcode): void
{
$this->createSimpleProductPage->setBarcode($barcode);
}

/**
* @Then I should be notified that product with this barcode already exists
*/
public function iShouldBeNotifiedThatBarcodeAlreadyExists()
{
Assert::same(
$this->createSimpleProductPage->getValidationMessage('barcode'),
'Variant barcode must be unique.'
);
}
}
6 changes: 6 additions & 0 deletions tests/Behat/Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
<services>
<defaults public="true" />

<service id="loevgaard_sylius_barcode.behat.context.setup.product"
class="Tests\Loevgaard\SyliusBarcodePlugin\Behat\Context\Setup\ProductContext">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="sylius.product_variant_resolver.default" />
</service>

<service id="loevgaard_sylius_barcode.behat.context.ui.admin.managing_products"
class="Tests\Loevgaard\SyliusBarcodePlugin\Behat\Context\Ui\Admin\ManagingProductsContext">
<argument type="service" id="loevgaard_sylius_barcode.behat.page.admin.product.create_simple" />
Expand Down
7 changes: 5 additions & 2 deletions tests/Behat/Resources/suites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ default:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.locale
- sylius.behat.context.transform.product
- sylius.behat.context.transform.shared_storage

- sylius.behat.context.setup.channel
- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.shipping_category
- sylius.behat.context.setup.product
- loevgaard_sylius_barcode.behat.context.setup.product

- sylius.behat.context.ui.admin.managing_products
- sylius.behat.context.ui.admin.notification

- loevgaard_sylius_barcode.behat.context.ui.admin.managing_products
- sylius.behat.context.ui.admin.notification
filters:
tags: "@managing_product_barcodes && @ui"

0 comments on commit 45ae1bd

Please sign in to comment.