Skip to content

Commit

Permalink
Merge pull request #14145 from zuk3975/behat/manufacturer
Browse files Browse the repository at this point in the history
Behavioral tests for Manufacturer commands
  • Loading branch information
matks committed Aug 26, 2019
2 parents fbe37cb + 7811f79 commit 33e39a2
Show file tree
Hide file tree
Showing 3 changed files with 401 additions and 0 deletions.
@@ -0,0 +1,317 @@
<?php
/**
* 2007-2019 PrestaShop and Contributors
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2019 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/

namespace Tests\Integration\Behaviour\Features\Context\Domain;

use Behat\Gherkin\Node\TableNode;
use Manufacturer;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Command\AddManufacturerCommand;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Command\BulkDeleteManufacturerCommand;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Command\BulkToggleManufacturerStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Command\DeleteManufacturerCommand;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Command\EditManufacturerCommand;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Command\ToggleManufacturerStatusCommand;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Exception\ManufacturerNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\Query\GetManufacturerForEditing;
use PrestaShop\PrestaShop\Core\Domain\Manufacturer\ValueObject\ManufacturerId;
use RuntimeException;
use Tests\Integration\Behaviour\Features\Context\CommonFeatureContext;
use Tests\Integration\Behaviour\Features\Context\SharedStorage;
use Tests\Integration\Behaviour\Features\Context\Util\NoExceptionAlthoughExpectedException;
use Tests\Integration\Behaviour\Features\Context\Util\PrimitiveUtils;

class ManufacturerFeatureContext extends AbstractDomainFeatureContext
{
/**
* @var int default language id from configs
*/
private $defaultLangId;

/**
* @var int default shop id from configs
*/
private $defaultShopId;

public function __construct()
{
$configuration = CommonFeatureContext::getContainer()->get('prestashop.adapter.legacy.configuration');
$this->defaultLangId = $configuration->get('PS_LANG_DEFAULT');
$this->defaultShopId = $configuration->get('PS_SHOP_DEFAULT');
}

/**
* @When I add new manufacturer :reference with following properties:
*/
public function createManufacturerWithDefaultLang($reference, TableNode $node)
{
$data = $node->getRowsHash();

$this->createManufacturerUsingCommand($reference, $data);
}

/**
* @When I edit manufacturer :reference with following properties:
*/
public function editManufacturerWithDefaultLang($reference, TableNode $node)
{
/** @var Manufacturer $manufacturer */
$manufacturer = SharedStorage::getStorage()->get($reference);

$manufacturerId = (int) $manufacturer->id;
$data = $node->getRowsHash();
$command = new EditManufacturerCommand($manufacturerId);

if (isset($data['name'])) {
$command->setName($data['name']);
}
if (isset($data['enabled'])) {
$command->setEnabled(PrimitiveUtils::castStringBooleanIntoBoolean($data['enabled']));
}
if (isset($data['short_description'])) {
[$this->defaultLangId => $command->setLocalizedShortDescriptions($data['short_description'])];
}
if (isset($data['description'])) {
[$this->defaultLangId => $command->setLocalizedDescriptions($data['description'])];
}
if (isset($data['meta_title'])) {
[$this->defaultLangId => $command->setLocalizedMetaTitles($data['meta_title'])];
}
if (isset($data['meta_description'])) {
[$this->defaultLangId => $command->setLocalizedMetaDescriptions($data['meta_description'])];
}
if (isset($data['meta_keywords'])) {
[$this->defaultLangId => $command->setLocalizedMetaKeywords($data['meta_keywords'])];
}

$this->getCommandBus()->handle($command);

SharedStorage::getStorage()->set($reference, new Manufacturer($manufacturerId));
}

/**
* @When I delete manufacturer :manufacturerReference
*/
public function deleteManufacturer($manufacturerReference)
{
/** @var Manufacturer $manufacturer */
$manufacturer = SharedStorage::getStorage()->get($manufacturerReference);

$this->getCommandBus()->handle(new DeleteManufacturerCommand((int) $manufacturer->id));
}

/**
* @When I delete manufacturers: :manufacturerReferences using bulk action
*/
public function bulkDeleteManufacturers($manufacturerReferences)
{
$manufacturerIds = [];
foreach (PrimitiveUtils::castStringArrayIntoArray($manufacturerReferences) as $manufacturerReference) {
$manufacturerIds[] = (int) SharedStorage::getStorage()->get($manufacturerReference)->id;
}

$this->getCommandBus()->handle(new BulkDeleteManufacturerCommand($manufacturerIds));
}

/**
* @Then manufacturers: :manufacturerReferences should be deleted
*/
public function assertManufacturersAreDeleted($manufacturerReferences)
{
foreach (PrimitiveUtils::castStringArrayIntoArray($manufacturerReferences) as $manufacturerReference) {
$this->assertManufacturerIsDeleted($manufacturerReference);
}
}

/**
* @Then manufacturer :reference name should be :name
*/
public function assertManufacturerName($reference, $name)
{
$manufacturer = SharedStorage::getStorage()->get($reference);

if ($manufacturer->name !== $name) {
throw new RuntimeException(sprintf(
'Manufacturer "%s" has "%s" name, but "%s" was expected.',
$reference,
$manufacturer->name,
$name
));
}
}

/**
* @Then manufacturer :reference :field in default language should be :value
*/
public function assertFieldValue($reference, $field, $value)
{
/** @var Manufacturer $manufacturer */
$manufacturer = SharedStorage::getStorage()->get($reference);

if ($manufacturer->$field[$this->defaultLangId] !== $value) {
throw new RuntimeException(sprintf(
'Manufacturer "%s" has "%s" %s, but "%s" was expected.',
$reference,
$manufacturer->$field[$this->defaultLangId],
$field,
$value
));
}
}

/**
* @Then manufacturer :reference :field field in default language should be empty
*/
public function assertFieldIsEmpty($reference, $field)
{
$manufacturer = SharedStorage::getStorage()->get($reference);

if ($manufacturer->$field[$this->defaultLangId] !== '') {
throw new RuntimeException(sprintf(
'Manufacturer "%s" has "%s" %s, but it was expected to be empty',
$reference,
$manufacturer->$field[$this->defaultLangId],
$field
));
}
}

/**
* @When /^I (enable|disable)? manufacturer "(.*)"$/
*/
public function toggleStatus($action, $reference)
{
$expectedStatus = 'enable' === $action;

/** @var Manufacturer $manufacturer */
$manufacturer = SharedStorage::getStorage()->get($reference);
$manufacturerId = (int) $manufacturer->id;

$this->getCommandBus()->handle(new ToggleManufacturerStatusCommand($manufacturerId, $expectedStatus));

SharedStorage::getStorage()->set($reference, new Manufacturer($manufacturerId));
}

/**
* @When /^I (enable|disable) multiple manufacturers: "(.+)" using bulk action$/
*/
public function bulkToggleStatus($action, $manufacturerReferences)
{
$expectedStatus = 'enable' === $action;
$manufacturerIdsByReference = [];

foreach (PrimitiveUtils::castStringArrayIntoArray($manufacturerReferences) as $manufacturerReference) {
$manufacturer = SharedStorage::getStorage()->get($manufacturerReference);
$manufacturerIdsByReference[$manufacturerReference] = (int) $manufacturer->id;
}

$this->getQueryBus()->handle(new BulkToggleManufacturerStatusCommand(
$manufacturerIdsByReference,
$expectedStatus
));

foreach ($manufacturerIdsByReference as $reference => $id) {
SharedStorage::getStorage()->set($reference, new Manufacturer($id));
}
}

/**
* @Given /^manufacturers: "(.+)" should be (enabled|disabled)$/
*/
public function assertMultipleManufacturersStatus($manufacturerReferences, $expectedStatus)
{
foreach (PrimitiveUtils::castStringArrayIntoArray($manufacturerReferences) as $manufacturerReference) {
$this->assertStatus($manufacturerReference, $expectedStatus);
}
}

/**
* @Given /^manufacturer "(.*)" is (enabled|disabled)?$/
* @Then /^manufacturer "(.*)" should be (enabled|disabled)?$/
*/
public function assertStatus($manufacturerReference, $expectedStatus)
{
/** @var Manufacturer $manufacturer */
$manufacturer = SharedStorage::getStorage()->get($manufacturerReference);

$isEnabled = 'enabled' === $expectedStatus;
$actualStatus = (bool) $manufacturer->active;

if ($actualStatus !== $isEnabled) {
throw new RuntimeException(sprintf(
'Manufacturer "%s" is %s, but it was expected to be %s',
$manufacturerReference,
$actualStatus ? 'enabled' : 'disabled',
$expectedStatus
));
}
}

/**
* @Then manufacturer :manufacturerReference should be deleted
*/
public function assertManufacturerIsDeleted($manufacturerReference)
{
/** @var Manufacturer $manufacturer */
$manufacturer = SharedStorage::getStorage()->get($manufacturerReference);

try {
$query = new GetManufacturerForEditing((int) $manufacturer->id);
$this->getQueryBus()->handle($query);

throw new NoExceptionAlthoughExpectedException(sprintf(
'Manufacturer %s exists, but it was expected to be deleted',
$manufacturerReference
));
} catch (ManufacturerNotFoundException $e) {
SharedStorage::getStorage()->clear($manufacturerReference);
}
}

/**
* @param $reference
* @param array $data
*/
private function createManufacturerUsingCommand($reference, array $data)
{
$command = new AddManufacturerCommand(
$data['name'],
PrimitiveUtils::castStringBooleanIntoBoolean($data['enabled']),
[$this->defaultLangId => $data['short_description']],
[$this->defaultLangId => $data['description']],
[$this->defaultLangId => $data['meta_title']],
[$this->defaultLangId => $data['meta_description']],
[$this->defaultLangId => $data['meta_keywords']],
[$this->defaultShopId]
);

/**
* @var ManufacturerId
*/
$manufacturerId = $this->getCommandBus()->handle($command);

SharedStorage::getStorage()->set($reference, new Manufacturer($manufacturerId->getValue()));
}
}
@@ -0,0 +1,78 @@
@reset-database-before-feature
#./vendor/bin/behat -c tests/Integration/Behaviour/behat.yml -s manufacturer
Feature: Manufacturer management
As an employee
I must be able to add, edit and delete manufacturers

Scenario: Adding new manufacturer
When I add new manufacturer "shoeman" with following properties:
| name | best-shoes |
| short_description| Makes best shoes in Europe |
| description | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at nulla id mi gravida blandit a non erat. Mauris nec lorem vel odio sagittis ornare.|
| meta_title | Perfect quality shoes |
| meta_description | |
| meta_keywords | Boots, shoes, slippers |
| enabled | true |
Then manufacturer "shoeman" name should be "best-shoes"
And manufacturer "shoeman" "short_description" in default language should be "Makes best shoes in Europe"
And manufacturer "shoeman" "description" in default language should be "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi at nulla id mi gravida blandit a non erat. Mauris nec lorem vel odio sagittis ornare."
And manufacturer "shoeman" "meta_title" in default language should be "Perfect quality shoes"
And manufacturer "shoeman" "meta_description" field in default language should be empty
And manufacturer "shoeman" "meta_keywords" in default language should be "Boots, shoes, slippers"
And manufacturer "shoeman" should be enabled

Scenario: Editing manufacturer
When I edit manufacturer "shoeman" with following properties:
| name | worst-shoes |
| short_description| Worst slippers in EU |
| meta_title | Worst quality shoes |
| description | |
| meta_description | You'd better walk bare foot |
| enabled | false |
Then manufacturer "shoeman" name should be "worst-shoes"
And manufacturer "shoeman" "short_description" in default language should be "Worst slippers in EU"
And manufacturer "shoeman" "description" field in default language should be empty
And manufacturer "shoeman" "meta_title" in default language should be "Worst quality shoes"
And manufacturer "shoeman" "meta_description" in default language should be "You'd better walk bare foot"
And manufacturer "shoeman" "meta_keywords" in default language should be "Boots, shoes, slippers"
And manufacturer "shoeman" should be disabled

Scenario: Enable and disable manufacturer status
Given manufacturer "shoeman" is disabled
When I enable manufacturer "shoeman"
Then manufacturer "shoeman" should be enabled
When I disable manufacturer "shoeman"
Then manufacturer "shoeman" should be disabled

Scenario: Enabling and disabling multiple manufacturers in bulk action
When I add new manufacturer "baller" with following properties:
| name | Baller |
| short_description| Makes big balls |
| description | consectetur adipiscing elit. Morbi at nulla id mi gravida blandit a non erat. Mauris nec lorem vel odio sagittis ornare.|
| meta_title | Have some balls |
| meta_description | |
| meta_keywords | Balls, basketball, football |
| enabled | false |
And I add new manufacturer "rocket" with following properties:
| name | Rocket |
| short_description| Cigarettes manufacturer |
| description | Morbi at nulla id mi gravida blandit a non erat. Mauris nec lorem vel odio sagittis ornare.|
| meta_title | You smoke - you die! |
| meta_description | The sun is shining and the weather is sweet|
| meta_keywords | Cigarettes, smoke |
| enabled | true |
When I enable multiple manufacturers: "baller, rocket" using bulk action
Then manufacturers: "baller, rocket" should be enabled
When I disable multiple manufacturers: "baller, rocket" using bulk action
Then manufacturers: "baller, rocket" should be disabled

Scenario: Deleting manufacturer right after changing its name
When I edit manufacturer "shoeman" with following properties:
| name | Shoeman |
Then manufacturer "shoeman" name should be "Shoeman"
When I delete manufacturer "shoeman"
Then manufacturer "shoeman" should be deleted

Scenario: Deleting multiple manufacturers in bulk action
When I delete manufacturers: "baller, rocket" using bulk action
Then manufacturers: "baller, rocket" should be deleted

0 comments on commit 33e39a2

Please sign in to comment.