Skip to content

Commit

Permalink
Cleanup behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rokaszygmantas committed Sep 3, 2019
1 parent 02dec40 commit 6b97489
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class CartRuleFeatureContext extends AbstractDomainFeatureContext
use StringToBooleanTransform;
use CurrencyTransform;

private $cartRuleStorageProperty = 'add_cart_rule';

/**
* @Transform /^active from "([^"]+)"$/
* @Transform /^active until "([^"]+)"$/
Expand All @@ -81,25 +83,30 @@ public function transformDiscountApplicationType(string $type): string
}

/**
* @When /^I want to create a cart rule named "([^"]+)"$/
* @When /^I want to create a new cart rule$/
*/
public function createCartRuleWithName(string $name)
public function prepareCartRuleCreation()
{
$key = sprintf('cart_rule_%s', $name);
$this->setCartRuleProperties([]);
}

SharedStorage::getStorage()->set($key, [
'name' => $name,
]);
/**
* @When /^I specify (its) name in default language as "([^"]+)"$/
*/
public function createCartRuleWithName(array $properties, string $nameInDefaultLanguage)
{
$properties['name'] = $nameInDefaultLanguage;
$this->setCartRuleProperties($properties);
}

/**
* @Given /^I specify (its) "([^"]+)" as "([^"]+)"$/
* @Given /^I specify (cart rule|its) "([^"]+)" as "([^"]+)"$/
* @Given /^I specify that (its) "([^"]+)" is "([^"]+)"$/
*/
public function specifyCartRuleProperty(array $properties, string $property, string $value)
{
$properties[$property] = $value;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -108,7 +115,7 @@ public function specifyCartRuleProperty(array $properties, string $property, str
public function specifyCartRuleActiveFromOrUntil(array $properties, DateTime $date, string $property)
{
$properties[$property] = $date;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -117,7 +124,7 @@ public function specifyCartRuleActiveFromOrUntil(array $properties, DateTime $da
public function specifyCartRulePartialUse(bool $isPartialUseEnabled, array $properties)
{
$properties['partial_use'] = $isPartialUseEnabled;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -126,7 +133,7 @@ public function specifyCartRulePartialUse(bool $isPartialUseEnabled, array $prop
public function specifyCartRuleStatus(array $properties, bool $isEnabled)
{
$properties['active'] = $isEnabled;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -135,7 +142,7 @@ public function specifyCartRuleStatus(array $properties, bool $isEnabled)
public function specifyCartRuleHighlightedInCart(array $properties, bool $highlightInCart)
{
$properties['highlight'] = $highlightInCart;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -145,7 +152,7 @@ public function specifyMinimumPurchaseAmountInCurrency(array $properties, Curren
{
$properties['minimum_amount'] = (float) $amount;
$properties['minimum_amount_currency'] = $currency->id;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -154,7 +161,7 @@ public function specifyMinimumPurchaseAmountInCurrency(array $properties, Curren
public function specifyIfMinimumPurchaseAmountIsTaxIncluded(array $properties, bool $isTaxIncluded)
{
$properties['minimum_amount_tax'] = $isTaxIncluded;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -163,7 +170,7 @@ public function specifyIfMinimumPurchaseAmountIsTaxIncluded(array $properties, b
public function specifyIfMinimumPurchaseAmountIsShippingIncluded(array $properties, bool $isShippingIncluded)
{
$properties['minimum_amount_shipping'] = $isShippingIncluded;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -172,7 +179,7 @@ public function specifyIfMinimumPurchaseAmountIsShippingIncluded(array $properti
public function specifyCartRuleGivesFreeShipping(array $properties)
{
$properties['free_shipping'] = true;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -189,7 +196,7 @@ public function specifyCartRuleGivesAmountReduction(
$properties['reduction_currency'] = $currency->id;
$properties['reduction_tax'] = $isTaxIncluded;
$properties['discount_application_type'] = $discountApplicationType;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand All @@ -204,7 +211,7 @@ public function specifyCartRuleGivesPercentageReduction(
$properties['reduction_percentage'] = (float) $percentage;
$properties['reduction_applies_to_discounted_products'] = $includesDiscountedProducts;
$properties['discount_application_type'] = $discountApplicationType;
SharedStorage::getStorage()->setLatestResource($properties);
$this->setCartRuleProperties($properties);
}

/**
Expand Down Expand Up @@ -252,6 +259,8 @@ public function saveCartRule(array $properties)
);

ObjectModel::enableCache();

SharedStorage::getStorage()->clear($this->cartRuleStorageProperty);
}

/**
Expand Down Expand Up @@ -642,4 +651,14 @@ private function areNumbersEqual($number1, $number2): bool

return $number1->equals(new Number((string) $number2));
}

/**
* Sets given properties into shared storage under common key.
*
* @param array $properties
*/
private function setCartRuleProperties(array $properties): void
{
SharedStorage::getStorage()->set($this->cartRuleStorageProperty, $properties);
}
}
14 changes: 12 additions & 2 deletions tests/Integration/Behaviour/Features/Context/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static function getStorage()
*/
public function get($key)
{
if (!isset($this->storage[$key])) {
if (!$this->exists($key)) {
throw new RuntimeException(sprintf('Item with key "%s" does not exist', $key));
}

Expand Down Expand Up @@ -101,12 +101,22 @@ public function set($key, $resource)
$this->latestKey = $key;
}

/**
* @param $key
*
* @return bool
*/
public function exists($key): bool
{
return isset($this->storage[$key]);
}

/**
* @param string $key
*/
public function clear($key)
{
if (isset($this->storage[$key])) {
if ($this->exists($key)) {
unset($this->storage[$key]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Feature: Add cart rule
Given currency "currency1" is the default one

Scenario: Create a cart rule with amount discount
When I want to create a cart rule named "Promotion"
When I want to create a new cart rule
And I specify its name in default language as "Promotion"
And I specify its "description" as "Promotion for holidays"
And I specify that its active from "2019-01-01 11:05:00"
And I specify that its active until "2019-12-01 00:00:00"
Expand Down Expand Up @@ -47,7 +48,8 @@ Feature: Add cart rule
And it should give a reduction of "15" in currency "USD" which is tax included and applies to order without shipping

Scenario: Create a cart rule with percentage discount
When I want to create a cart rule named "50% off promo"
When I want to create a new cart rule
And I specify its name in default language as "50% off promo"
And I specify its "description" as "Discount for whole catalog for one hour"
And I specify that its active from "2019-01-01 11:00:00"
And I specify that its active until "2019-01-01 12:00:00"
Expand Down

0 comments on commit 6b97489

Please sign in to comment.