Skip to content

Commit

Permalink
Adjust behat (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Jun 20, 2022
1 parent 38efc40 commit de02424
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
7 changes: 0 additions & 7 deletions features/enqueuing_generic_items.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ Feature: Enqueuing items
When I enqueue items with since date specified from a not existent file
Then I should be notified that the since date file does not exists

@cli
Scenario: Avoiding to enqueue two times the same item if it has not been imported yet
Given there is a product "product-1" updated at "2020-01-20 22:23:13" on Akeneo
And there is one item to import with identifier "product-1" for the "Product" importer in the Akeneo queue
When I enqueue items for all importers modified since date "2020-01-20 01:00:00"
Then there should be only one queue item with identifier "product-1" for the "Product" importer in the Akeneo queue

@cli
Scenario: Enqueuing all items regardless last modified date
Given there are 3 products on Akeneo
Expand Down
5 changes: 3 additions & 2 deletions tests/Behat/Context/Cli/EnqueueCommandContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Tests\Webgriffe\SyliusAkeneoPlugin\Behat\Context\Cli;

use Behat\Behat\Context\Context;
use DateTime;
use org\bovigo\vfs\vfsStream;
use Sylius\Behat\Service\SharedStorageInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
Expand All @@ -26,12 +27,12 @@ public function __construct(
/**
* @When I enqueue items for all importers modified since date :date
*/
public function iRunEnqueueCommandWithSinceDate(string $date): void
public function iRunEnqueueCommandWithSinceDate(DateTime $date): void
{
$commandTester = $this->getCommandTester();

try {
$commandTester->execute(['command' => 'webgriffe:akeneo:enqueue', '--since' => $date]);
$commandTester->execute(['command' => 'webgriffe:akeneo:enqueue', '--since' => $date->format('Y-m-d H:i:s')]);
} catch (Throwable $t) {
$this->sharedStorage->set('command_exception', $t);
}
Expand Down
58 changes: 40 additions & 18 deletions tests/Behat/Context/Messenger/MessengerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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;
Expand All @@ -32,42 +33,39 @@ public function theQueueItemHasAnErrorMessageContaining(string $identifier, stri
}

/**
* @Then /^the queue item with identifier "([^"]*)" for the "([^"]*)" importer should not be in the Akeneo queue$/
* @Then the queue item with identifier :identifier for the :importer importer should not be in the Akeneo queue
*/
public function theProductShouldNotBeInTheAkeneoQueue(string $identifier, string $importer): void
public function theQueueItemWithIdentifierForTheImporterShouldNotBeInTheAkeneoQueue(string $identifier, string $importer): void
{
Assert::null(
$this->queueItemRepository->findOneBy(['akeneoEntity' => $importer, 'akeneoIdentifier' => $identifier]),
Assert::null($this->getEnvelopeByImporterAndIdentifier($importer, $identifier),
);
}

/**
* @Then /^the queue item with identifier "([^"]*)" for the "([^"]*)" importer should be in the Akeneo queue$/
* @Then the queue item with identifier :identifier for the :importer importer should be in the Akeneo queue
*/
public function theProductShouldBeInTheAkeneoQueue(string $identifier, string $importer): void
public function theQueueItemWithIdentifierForTheImporterShouldBeInTheAkeneoQueue(string $identifier, string $importer): void
{
Assert::isInstanceOf(
$this->queueItemRepository->findOneBy(
['akeneoEntity' => $importer, 'akeneoIdentifier' => $identifier],
),
QueueItemInterface::class,
$this->getEnvelopeByImporterAndIdentifier($importer, $identifier)->getMessage(),
ItemImport::class
);
}

/**
* @Then /^there should be no item in the queue for the "([^"]*)" importer/
* @Then there should be no item in the queue for the :importer importer
*/
public function thereShouldBeNoProductInTheAkeneoQueue(string $importer): void
{
Assert::isEmpty($this->queueItemRepository->findBy(['akeneoEntity' => $importer]));
Assert::isEmpty($this->getEnvelopesByImporter($importer));
}

/**
* @Then /^there should be no item in the Akeneo queue$/
* @Then there should be no item in the Akeneo queue
*/
public function thereShouldBeNoItemInTheAkeneoQueue(): void
{
Assert::isEmpty($this->queueItemRepository->findAll());
Assert::isEmpty($this->transport->get());
}

/**
Expand All @@ -86,17 +84,17 @@ public function thereShouldBeOnlyOneProductQueueItemForInTheAkeneoQueue(string $
*/
public function thereShouldBeItemsForTheImporterInTheAkeneoQueue(int $count, string $importer): void
{
$items = $this->queueItemRepository->findBy(['akeneoEntity' => $importer]);
$items = $this->getEnvelopesByImporter($importer);
Assert::count($items, $count);
}

/**
* @Then /^there should be items for the "([^"]*)" importer only in the Akeneo queue$/
* @Then there should be items for the :importer importer only in the Akeneo queue
*/
public function thereShouldBeItemsForTheImporterOnlyInTheAkeneoQueue(string $importer): void
{
$importerItems = $this->queueItemRepository->findBy(['akeneoEntity' => $importer]);
Assert::count($this->queueItemRepository->findAll(), count($importerItems));
$importerItems = $this->getEnvelopesByImporter($importer);
Assert::count($this->transport->get(), count($importerItems));
}

private function getQueueItemByImporterAndIdentifier(string $importer, string $identifier): ItemImport
Expand Down Expand Up @@ -143,4 +141,28 @@ public function theItemImportMessageForProductShouldHaveFailed(string $identifie
});
Assert::count($failedMessages, 1);
}

private function getEnvelopesByImporter(string $importer): array
{
return array_filter($this->transport->get(), static function (Envelope $envelope) use ($importer): bool {
/** @var ItemImport|mixed $message */
$message = $envelope->getMessage();
return $message instanceof ItemImport && $message->getAkeneoEntity() === $importer;
});
}

private function getEnvelopeByImporterAndIdentifier(string $importer, string $identifier): ?Envelope
{
$envelopes = array_filter($this->transport->get(), static function (Envelope $envelope) use ($importer, $identifier): bool {
/** @var ItemImport|mixed $message */
$message = $envelope->getMessage();
return $message instanceof ItemImport && $message->getAkeneoEntity() === $importer && $message->getAkeneoIdentifier() === $identifier;
});
$envelope = reset($envelopes);
if (!$envelope instanceof Envelope) {
return null;
}

return $envelope;
}
}

0 comments on commit de02424

Please sign in to comment.