Skip to content

Commit

Permalink
PIM-6348: add new variant navigation component in the PEF
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Pétremann committed Sep 26, 2017
1 parent 7343153 commit 9c66c27
Show file tree
Hide file tree
Showing 35 changed files with 2,201 additions and 33 deletions.
2 changes: 2 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ default:
- 'Context\FeatureContext'
- '@pim_catalog.repository.product_model'
- '@doctrine.orm.entity_manager'
- Pim\Behat\Context\Domain\Enrich\VariantNavigationContext:
- 'Context\FeatureContext'
- Pim\Behat\Context\Domain\Spread\ExportBuilderContext:
- 'Context\FeatureContext'
- Pim\Behat\Context\Domain\Spread\ExportProfilesContext:
Expand Down
28 changes: 26 additions & 2 deletions features/Context/NavigationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Pim\Component\Catalog\Model\AttributeGroupInterface;
use Pim\Component\Catalog\Model\GroupTypeInterface;
use Pim\Component\Catalog\Model\Product;
use Pim\Component\Catalog\Model\ProductInterface;
use Pim\Component\Catalog\Model\ProductModel;
use Pim\Component\Catalog\Model\ProductModelInterface;

/**
* Context for navigating the website
Expand Down Expand Up @@ -322,11 +325,11 @@ public function iShouldBeOnTheProductsPage()
}

/**
* @param Product $product
* @param ProductInterface $product
*
* @Given /^I should be on the (product "([^"]*)") edit page$/
*/
public function iShouldBeOnTheProductEditPage(Product $product)
public function iShouldBeOnTheProductEditPage(ProductInterface $product)
{
$this->spin(function () use ($product) {
$expectedAddress = $this->getPage('Product edit')->getUrl(['id' => $product->getId()]);
Expand All @@ -342,6 +345,27 @@ public function iShouldBeOnTheProductEditPage(Product $product)
$this->currentPage = 'Product edit';
}

/**
* @param ProductModel $productModel
*
* @Given /^I should be on the (product model "([^"]*)") edit page$/
*/
public function iShouldBeOnTheProductModelEditPage(ProductModel $productModel)
{
$this->spin(function () use ($productModel) {
$expectedAddress = $this->getPage('ProductModel edit')->getUrl(['id' => $productModel->getId()]);
$this->assertAddress($expectedAddress);

return true;
}, sprintf('Cannot find product model "%s"', $productModel->getId()));

$this->getMainContext()->spin(function () {
return $this->getCurrentPage()->find('css', '.AknTitleContainer-title');
}, 'Can not find any product model label');

$this->currentPage = 'ProductModel edit';
}

/**
* @return FixturesContext
*/
Expand Down
16 changes: 16 additions & 0 deletions features/Context/Page/Base/ProductEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;
use Pim\Behat\Decorator\VariantNavigationDecorator;

/**
* Product Edit Form
Expand Down Expand Up @@ -34,6 +35,12 @@ public function __construct($session, $pageFactory, $parameters = [])
'Available attributes list' => ['css' => '.add-attribute .select2-results'],
'Available attributes search' => ['css' => '.add-attribute .select2-search input[type="text"]'],
'Select2 dropmask' => ['css' => '.select2-drop-mask'],
'Variant navigation' => [
'css' => '.AknVariantNavigation',
'decorators' => [
VariantNavigationDecorator::class
]
]
]
);
}
Expand Down Expand Up @@ -676,4 +683,13 @@ public function getRemoveLinkFor($field)
));
}, sprintf('Spinning to get remove link on product edit form for field "%s"', $field));
}


/**
* @return NodeElement
*/
public function getVariantNavigation()
{
return $this->getElement('Variant navigation');
}
}
21 changes: 21 additions & 0 deletions features/Context/Page/ProductModel/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Context\Page\ProductModel;

use Context\Page\Base\ProductEditForm;
use Pim\Behat\Decorator\ContextSwitcherDecorator;

/**
* Product model edit page
Expand All @@ -13,6 +14,26 @@
*/
class Edit extends ProductEditForm
{
/**
* {@inheritdoc}
*/
public function __construct($session, $pageFactory, $parameters = [])
{
parent::__construct($session, $pageFactory, $parameters);

$this->elements = array_merge(
$this->elements,
[
'Main context selector' => [
'css' => '.AknTitleContainer-context',
'decorators' => [
ContextSwitcherDecorator::class
]
]
]
);
}

/** @var string */
protected $path = '#/enrich/product-model/{id}';
}
13 changes: 13 additions & 0 deletions features/Context/TransformationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Pim\Behat\Context\PimContext;
use Pim\Component\Catalog\Model\GroupInterface;
use Pim\Component\Catalog\Model\ProductModel;

/**
* Context for data transformations
Expand All @@ -26,6 +27,18 @@ public function castProductSkuToProduct($sku)
return $this->getFixturesContext()->getProduct($sku);
}

/**
* @param string $code
*
* @Transform /^product model "([^"]*)"$/
*
* @return ProductModel
*/
public function castProductModelCodeToProductModel($code)
{
return $this->getFixturesContext()->getProductModel($code);
}

/**
* @param string $code
*
Expand Down
177 changes: 177 additions & 0 deletions features/Pim/Behat/Context/Domain/Enrich/VariantNavigationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

declare(strict_types=1);

namespace Pim\Behat\Context\Domain\Enrich;

use Behat\Mink\Exception\ExpectationException;
use Pim\Behat\Context\PimContext;

class VariantNavigationContext extends PimContext
{
/**
* @Then /^the variant navigation selected axis values for level (\d+) should be "([^"]*)"$/
*/
public function theVariantNavigationSelectedAxisValuesForLevelShouldBe(int $level, string $axisValues): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$element = $variantNavigation->getSelectedAxisValuesForLevel($level);
$axisValues = strtolower($axisValues);
$elementAxisValues = strtolower($element->getText());

if ($axisValues !== $elementAxisValues) {
throw $this->createExpectationException(sprintf(
'Selected axis values for level "%s" should be "%s", but "%s" found.',
$level,
$axisValues,
$elementAxisValues
));
}
}

/**
* @Given /^the variant navigation axis name for level (\d+) should be "([^"]*)"$/
*/
public function theVariantNavigationAxisNameForLevelShouldBe(int $level, string $axisName): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$element = $variantNavigation->getAxisNameForLevel($level);
$axisName = strtolower($axisName);
$elementAxisName = str_replace(':', '', strtolower($element->getText()));

if ($axisName !== $elementAxisName) {
throw $this->createExpectationException(sprintf(
'Axis name for level "%s" should be "%s", but "%s" found.',
$level,
$axisName,
$elementAxisName
));
}
}

/**
* @When /^I open the variant navigation children selector for level (\d+)$/
*/
public function iOpenTheVariantNavigationChildrenSelectorForLevel(int $level): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$selector = $variantNavigation->getChildrenSelectorForLevel($level);
$selector->open();
}

/**
* @Then /^I should( not)? see the "([^"]*)" element in the variant children selector for level (\d+)$/
*/
public function iShouldSeeTheElementInTheVariantChildrenSelectorForLevel($not, string $label, int $level): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$selector = $variantNavigation->getChildrenSelectorForLevel($level);
$availableValues = $selector->getAvailableValues();
$availableLabels = [];

// We just want the label, not the completeness numbers
foreach ($availableValues as $availableValue) {
$matches = [];
preg_match('#^(.*) ((\d+ \/ \d+)?(\d+\%)?)$#', $availableValue, $matches);
$availableLabels[] = $matches[1];
}

if (!in_array($label, $availableLabels) && !$not) {
throw $this->createExpectationException(sprintf(
'Could not find element "%s" in the variant children selector for level "%s"',
$label,
$level
));
}

if (in_array($label, $availableLabels) && $not) {
throw $this->createExpectationException(sprintf(
'Element "%s" in the variant children selector for level "%s" should not be visible',
$label,
$level
));
}
}

/**
* @When /^I filter the variant navigation children selector for level (\d+) with text "([^"]*)"$/
*/
public function iFilterTheVariantNavigationChildrenSelectorForLevelWithText(int $level, string $text): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$selector = $variantNavigation->getChildrenSelectorForLevel($level);
$selector->search($text);
}

/**
* @When /^I select the child "([^"]*)" for level (\d+)$/
*/
public function iSelectTheChildForLevel(string $label, int $level): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$selector = $variantNavigation->getChildrenSelectorForLevel($level);
$selector->setValue($label);
}

/**
* @When /^I navigate to the selected element for level (\d+)$/
*/
public function iNavigateToTheSelectedElementForLevel(int $level): void
{
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$element = $variantNavigation->getSelectedAxisValuesForLevel($level);
$element->click();
}

/**
* @Then /^completeness for element "([^"]*)" in the variant children selector for level (\d+) should be "([^"]*)"$/
*/
public function completenessForElementInTheVariantChildrenSelectorForLevelShouldBe(
string $label,
int $level,
string $completeness
): void {
$variantNavigation = $this->getCurrentPage()->getVariantNavigation();

$selector = $variantNavigation->getChildrenSelectorForLevel($level);
$availableValues = $selector->getAvailableValues();

foreach ($availableValues as $availableValue) {
$matches = [];
preg_match('#^(.*) ((\d+ \/ \d+)?(\d+\%)?)$#', $availableValue, $matches);
$itemLabel = $matches[1];
$itemCompleteness = $matches[2];

if ($label === $itemLabel && $completeness !== $itemCompleteness) {
throw $this->createExpectationException(sprintf(
'Item "%s" for level "%s" should have "%s" for completeness, "%s" found',
$label,
$level,
$completeness,
$itemCompleteness
));

break;
}
}
}

/**
* Create an expectation exception
*
* @param string $message
*
* @return ExpectationException
*/
protected function createExpectationException(string $message): ExpectationException
{
return $this->getMainContext()->createExpectationException($message);
}
}
Loading

0 comments on commit 9c66c27

Please sign in to comment.