Skip to content

Commit

Permalink
WIP: refactor behat enqueuing products test (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Aug 29, 2022
1 parent 906a4d4 commit e13e27a
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 62 deletions.
45 changes: 0 additions & 45 deletions features/enqueuing_generic_items.feature

This file was deleted.

63 changes: 63 additions & 0 deletions features/importing_generic_items.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
@importing_generic_items
Feature: Importing items
In order to import data from Akeneo
As an Administrator
I want to import items from the Akeneo PIM

Background:
Given I am logged in as an administrator
And the store has a product association type "Pack" with a code "PACK"

@cli @ui
Scenario: Importing items when no item is modified since the given date
When I import items for all importers modified since date "2020-01-20 01:00:00"
And I browse products
Then I should see 0 products in the list

@cli @ui
Scenario: Importing items without a since date
When I import items for all importers with no since date
Then I should be notified that a since date is required
When I browse products
Then I should see 0 products in the list

@cli @ui
Scenario: Importing items with an invalid since date
When I import items for all importers with invalid since date
Then I should be notified that the since date must be a valid date
When I browse products
Then I should see 0 products in the list

@cli @ui
Scenario: Importing items with a since date specified from a not existent file
When I import items with since date specified from a not existent file
Then I should be notified that the since date file does not exists
When I browse products
Then I should see 0 products in the list

@cli @ui
Scenario: Importing all items regardless last modified date
Given there is a product "1314976" updated at "2022-06-15" on Akeneo
And there is a product "10597353" updated at "2022-07-23" on Akeneo
And there is a product "11164822" updated at "2022-08-01" on Akeneo
When I import all items for all importers
And I browse products
Then I should see 3 products in the list
And the product with code "11164822" should have an association "Pack" with product "10597353"

@cli @ui
Scenario: Importing all items for one importer only
Given there is a product "1314976" updated at "2022-06-15" on Akeneo
And there is a product "10597353" updated at "2022-07-23" on Akeneo
And there is a product "11164822" updated at "2022-08-01" on Akeneo
When I import all items for the "Product" importer
And I browse products
Then I should see 3 products in the list
And the product with code "11164822" should not have an association "Pack" with product "10597353"

@cli @ui
Scenario: Enqueuing all items for a not existent importer
When I import all items for a not existent importer
Then I should be notified that the importer does not exists
When I browse products
Then I should see 0 products in the list
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@enqueuing_products
@importing_products
Feature: Enqueuing products
In order to import my products from Akeneo
As a Store Owner
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@enqueuing_products_associations
@importing_products_associations
Feature: Enqueuing products associations
In order to import my products associations from Akeneo
As a Store Owner
Expand Down
6 changes: 4 additions & 2 deletions src/Product/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ public function getIdentifiersModifiedSince(DateTime $sinceDate): array
$products = $this->apiClient->getProductApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$identifiers = [];
foreach ($products as $product) {
Assert::string($product['identifier']);
$identifiers[] = $product['identifier'];
Assert::keyExists($product, 'identifier');
$productIdentifier = (string) $product['identifier'];
Assert::stringNotEmpty($productIdentifier);
$identifiers[] = $productIdentifier;
}

return $identifiers;
Expand Down
5 changes: 4 additions & 1 deletion src/ProductAssociations/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ public function getIdentifiersModifiedSince(DateTime $sinceDate): array
$products = $this->apiClient->getProductApi()->all(50, ['search' => $searchBuilder->getFilters()]);
$identifiers = [];
foreach ($products as $product) {
$identifiers[] = $product['identifier'];
Assert::keyExists($product, 'identifier');
$productIdentifier = (string) $product['identifier'];
Assert::stringNotEmpty($productIdentifier);
$identifiers[] = $productIdentifier;
}

return $identifiers;
Expand Down
2 changes: 1 addition & 1 deletion tests/Application/config/packages/test/messenger.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
framework:
messenger:
transports:
main: 'in-memory://'
main: 'sync://'
8 changes: 0 additions & 8 deletions tests/Behat/Context/Messenger/MessengerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@
use Behat\Behat\Context\Context;
use InvalidArgumentException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\InMemoryTransport;
use Throwable;
use Webgriffe\SyliusAkeneoPlugin\Message\ItemImport;
use Webgriffe\SyliusAkeneoPlugin\MessageHandler\ItemImportHandler;
use Webmozart\Assert\Assert;

final class MessengerContext implements Context
{
private array $failedMessages = [];

public function __construct(
private InMemoryTransport $transport,
private ItemImportHandler $itemImportHandler,
) {
}

/**
* @Then the queue item with identifier :identifier for the :importer importer should not be in the Akeneo queue
*/
Expand Down
48 changes: 48 additions & 0 deletions tests/Behat/Context/Ui/Admin/ManagingProductsContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
use Behat\Mink\Element\NodeElement;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\Product\IndexPageInterface;
use Sylius\Behat\Page\Admin\Product\UpdateSimpleProductPageInterface;
use Sylius\Behat\Service\Helper\JavaScriptTestHelperInterface;
use Sylius\Behat\Service\NotificationCheckerInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
use Sylius\Component\Product\Model\ProductAssociationTypeInterface;
use Webmozart\Assert\Assert;

final class ManagingProductsContext implements Context
{
Expand All @@ -20,6 +24,8 @@ public function __construct(
private IndexPageInterface $indexPage,
private JavaScriptTestHelperInterface $testHelper,
private NotificationCheckerInterface $notificationChecker,
private UpdateSimpleProductPageInterface $updateSimpleProductPage,
private ProductRepositoryInterface $productRepository,
) {
}

Expand All @@ -45,4 +51,46 @@ public function iShouldBeNotifiedThatItHasBeenSuccessfullyEnqueued(): void
'Akeneo PIM product import has been successfully scheduled',
);
}

/**
* @Then the product with code :code should have an association :productAssociationType with product :productName
*/
public function theProductShouldHaveAnAssociationWithProducts(
string $code,
ProductAssociationTypeInterface $productAssociationType,
...$productsNames,
): void {
$product = $this->productRepository->findOneByCode($code);
$this->updateSimpleProductPage->open(['id' => $product->getId()]);
foreach ($productsNames as $productName) {
Assert::true(
$this->updateSimpleProductPage->hasAssociatedProduct($productName, $productAssociationType),
sprintf(
'This product should have an association %s with product %s.',
$productAssociationType->getName(),
$productName,
),
);
}
}

/**
* @Then the product with code :code should not have an association :productAssociationType with product :productName
*/
public function theProductShouldNotHaveAnAssociationWithProduct(
string $code,
ProductAssociationTypeInterface $productAssociationType,
string $productName,
): void {
$product = $this->productRepository->findOneByCode($code);
$this->updateSimpleProductPage->open(['id' => $product->getId()]);
Assert::false(
$this->updateSimpleProductPage->hasAssociatedProduct($productName, $productAssociationType),
sprintf(
'This product should have an association %s with product %s.',
$productAssociationType->getName(),
$productName,
),
);
}
}
2 changes: 2 additions & 0 deletions tests/Behat/Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<argument type="service" id="sylius.behat.page.admin.product.index" />
<argument type="service" id="sylius.behat.java_script_test_helper" />
<argument type="service" id="sylius.behat.notification_checker" />
<argument type="service" id="sylius.behat.page.admin.product.update_simple" />
<argument type="service" id="sylius.repository.product" />
</service>
</services>
</container>
10 changes: 7 additions & 3 deletions tests/Behat/Resources/suites.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,28 @@ default:
filters:
tags: "@importing_product_associations && @cli"

cli_enqueuing_generic_items:
cli_ui_importing_generic_items:
contexts:
- sylius.behat.context.hook.doctrine_orm

- sylius.behat.context.transform.date_time
- sylius.behat.context.transform.product_association_type

- sylius.behat.context.setup.admin_security
- sylius.behat.context.setup.product_association
- webgriffe_sylius_akeneo.behat.context.setup.akeneo
- webgriffe_sylius_akeneo.behat.context.setup.queue

- webgriffe_sylius_akeneo.behat.context.cli.import_command

- webgriffe_sylius_akeneo.behat.context.messenger
- sylius.behat.context.ui.admin.managing_products
- webgriffe_sylius_akeneo.behat.context.ui.admin.managing_products

- webgriffe_sylius_akeneo.behat.context.system.filesystem
- webgriffe_sylius_akeneo.behat.context.system.datetime

filters:
tags: "@enqueuing_generic_items && @cli"
tags: "@importing_generic_items && @cli && @ui"

cli_enqueuing_products:
contexts:
Expand Down
82 changes: 82 additions & 0 deletions tests/Integration/DataFixtures/ApiClientMock/Product/10597353.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"identifier": "10597353",
"enabled": true,
"family": "loudspeakers",
"categories": [
"audio_video_sales",
"loudspeakers",
"nec"
],
"groups": [],
"parent": null,
"values": {
"picture": [
{
"locale": null,
"scope": null,
"data": "6/5/8/8/65887ee600d9d66e829a75beedf374533b1ff8bc_10597353_1104.jpg",
"_links": {
"download": {
"href": "http://127.0.0.1:8081/api/rest/v1/media-files/6/5/8/8/65887ee600d9d66e829a75beedf374533b1ff8bc_10597353_1104.jpg/download"
}
}
}
],
"name": [
{
"locale": null,
"scope": null,
"data": "NEC SP-4046PV"
}
],
"description": [
{
"locale": "de_DE",
"scope": "print",
"data": "Dem edlen Design der Displays angepasst und mit wenigen Handgriffen montiert bzw. demontiert."
},
{
"locale": "en_US",
"scope": "print",
"data": "Enhance your Large-Screen Display Speakers without breaking your budget with NEC's SP-4046PV."
},
{
"locale": "fr_FR",
"scope": "print",
"data": "Adapté au design des moniteurs LCD NEC Public Display, montage et démontage faciles."
}
],
"release_date": [
{
"locale": null,
"scope": "ecommerce",
"data": "2011-08-07T00:00:00+00:00"
}
]
},
"created": "2022-04-14T13:14:05+00:00",
"updated": "2022-04-14T13:14:05+00:00",
"associations": {
"PACK": {
"products": [],
"product_models": [],
"groups": []
},
"UPSELL": {
"products": [],
"product_models": [],
"groups": []
},
"X_SELL": {
"products": [],
"product_models": [],
"groups": []
},
"SUBSTITUTION": {
"products": [],
"product_models": [],
"groups": []
}
},
"quantified_associations": {}
}
Loading

0 comments on commit e13e27a

Please sign in to comment.