Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform end-to-end tests into acceptance tests using Behat with in-memory repos #7209

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions app/config/config_in_memory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
imports:
Copy link
Contributor

@docteurklein docteurklein Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't see how you decide which kernel environment to use (I guess in your behat.yml ?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- { resource: config.yml }

framework:
test: ~
session:
storage_id: session.storage.mock_array

swiftmailer:
disable_delivery: true

assetic:
use_controller: false

pim_enrich:
record_mails: true

services:
session.storage.mock_array:
class: Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage
public: false

pim_catalog.saver.attribute:
class: Pim\Test\Common\Persistence\InMemoryAttributeRepository

pim_catalog.repository.attribute:
alias: pim_catalog.saver.attribute
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it looks weird but I didn't find another way to use the same instance for two different services :/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok to me

18 changes: 17 additions & 1 deletion behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ default:
page: [Context\Page]
factory:
page_parameters:
base_url: http://akeneo-pim-behat.local/
base_url: http://ce-20-behat.pim.local

acceptance:
suites:
acceptance:
paths: ['%paths.base%/tests/Acceptance/']
contexts:
- Pim\Test\Acceptance\Behat\AcceptanceContext:
- '@pim_catalog.factory.attribute'
- '@pim_catalog.saver.attribute'
- '@pim_catalog.builder.product'
- '@pim_catalog.validator.product'
extensions:
Behat\Symfony2Extension:
kernel:
env: in_memory
debug: false
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"classmap": [ "app/AppKernel.php", "app/AppCache.php", "tests/AppKernelTest.php" ]
},
"autoload-dev": {
"psr-4": {
"Pim\\Test\\": "tests/"
Copy link
Contributor

@docteurklein docteurklein Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if that was meant only for behat, you could move it in the autoload part of behat.yml (not really important I think)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be used by something else than Behat imho.

},
"files": [
"vendor/phpunit/phpunit/src/Framework/Assert/Functions.php"
]
Expand Down
46 changes: 2 additions & 44 deletions features/product/validation/validate_text_attributes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ Feature: Validate text attributes of a product
| code | label-en_US | type | scopable | unique | max_characters | validation_rule | validation_regexp | group |
| barcode | Barcode | pim_catalog_text | 0 | 1 | 8 | regexp | /^0\d*$/ | other |
| email | Email | pim_catalog_text | 0 | 1 | | email | | other |
| link | Link | pim_catalog_text | 0 | 0 | | url | | other |
| manufacturer_number | Manufacturer number | pim_catalog_text | 1 | 0 | 8 | regexp | /^0\d*$/ | other |
| recipient | Recipient | pim_catalog_text | 1 | 0 | | email | | other |
| references | References | pim_catalog_text | 1 | 0 | | url | | other |
| desc | Description | pim_catalog_text | 0 | 0 | | | | other |
And the following family:
| code | label-en_US | attributes | requirements-ecommerce | requirements-mobile |
| baz | Baz | sku,barcode,email,link,manufacturer_number,recipient,references,desc | sku | sku |
| code | label-en_US | attributes | requirements-ecommerce | requirements-mobile |
| baz | Baz | sku,barcode,email,manufacturer_number,desc | sku | sku |
And the following products:
| sku | family |
| foo | baz |
Expand Down Expand Up @@ -47,45 +44,6 @@ Feature: Validate text attributes of a product
Then I should see validation tooltip "This value is too long. It should have 8 characters or less."
And there should be 1 error in the "Other" tab

Scenario: Validate the email validation rule constraint of text attribute
Given I change the Email to "foo"
And I save the product
Then I should see validation tooltip "This value is not a valid email address."
And there should be 1 error in the "Other" tab

Scenario: Validate the email validation rule constraint of scopable text attribute
Given I switch the scope to "ecommerce"
And I change the Recipient to "foo"
And I save the product
Then I should see validation tooltip "This value is not a valid email address."
And there should be 1 error in the "Other" tab

Scenario: Validate the url validation rule constraint of text attribute
Given I change the Link to "bar"
And I save the product
Then I should see validation tooltip "This value is not a valid URL."
And there should be 1 error in the "Other" tab

Scenario: Validate the url validation rule constraint of scopable text attribute
Given I switch the scope to "ecommerce"
Given I change the References to "bar"
And I save the product
Then I should see validation tooltip "This value is not a valid URL."
And there should be 1 error in the "Other" tab

Scenario: Validate the regexp validation rule constraint of text attribute
Given I change the Barcode to "111111"
And I save the product
Then I should see validation tooltip "This value is not valid."
And there should be 1 error in the "Other" tab

Scenario: Validate the regexp validation rule constraint of scopable text attribute
Given I switch the scope to "ecommerce"
Given I change the "Manufacturer number" to "111111"
And I save the product
Then I should see validation tooltip "This value is not valid."
And there should be 1 error in the "Other" tab

@jira https://akeneo.atlassian.net/browse/PIM-3447
Scenario: Validate the max database value length of text attribute
Given I change the Description to an invalid value
Expand Down
128 changes: 128 additions & 0 deletions tests/Acceptance/Behat/AcceptanceContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types = 1);

namespace Pim\Test\Acceptance\Behat;

use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use Pim\Component\Catalog\AttributeTypes;
use Pim\Component\Catalog\Builder\ProductBuilderInterface;
use Pim\Component\Catalog\Factory\AttributeFactory;
use Pim\Test\Common\Persistence\InMemoryAttributeRepository;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* Behat context defining step implementations for acceptance tests.
*
* TODO: Split it in smaller contexts later.
*
* @author Yohan Blain <[email protected]>
* @copyright 2017 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
final class AcceptanceContext implements Context
{
/** @var AttributeFactory */
private $attributeFactory;

/** @var InMemoryAttributeRepository */
private $attributeRepository;

/** @var ProductBuilderInterface */
private $productBuilder;

/** @var ValidatorInterface */
private $productValidator;

/** @var ConstraintViolationListInterface|null */
private $lastProductErrors;

public function __construct(
AttributeFactory $attributeFactory,
InMemoryAttributeRepository $attributeRepository,
ProductBuilderInterface $productBuilder,
ValidatorInterface $productValidator
) {
$this->attributeFactory = $attributeFactory;
$this->attributeRepository = $attributeRepository;
$this->productBuilder = $productBuilder;
$this->productValidator = $productValidator;
}

/**
* @Given an identifier attribute has been created
*/
public function loadIdentifierAttribute(): void
{
$identifierAttribute = $this->attributeFactory->createAttribute(AttributeTypes::IDENTIFIER);
$identifierAttribute->setCode('sku');

$this->attributeRepository->save($identifierAttribute);
}

/**
* @Given a text attribute :attributeCode with a validation rule ":validationRule" has been created
*/
public function loadTextAttributeWithValidationRule(string $attributeCode, string $validationRule): void
{
$attribute = $this->attributeFactory->createAttribute(AttributeTypes::TEXT);
$attribute->setCode($attributeCode);
$attribute->setValidationRule($validationRule);

$this->attributeRepository->save($attribute);
}

/**
* @Given a text attribute :attributeCode with a validation rule "regexp" and a pattern ":pattern" has been created
*/
public function loadTextAttributeWithRegexValidationRule(string $attributeCode, string $pattern): void
{
$attribute = $this->attributeFactory->createAttribute(AttributeTypes::TEXT);
$attribute->setCode($attributeCode);
$attribute->setValidationRule('regexp');
$attribute->setValidationRegexp($pattern);

$this->attributeRepository->save($attribute);
}

/**
* @When I create a product containing the following values:
*
* @param TableNode $values
*/
public function createAProductContainingTheFollowingValues(TableNode $values): void
{
$product = $this->productBuilder->createProduct('new_product');

foreach ($values->getRows() as [$attributeCode, $value]) {
$attribute = $this->attributeRepository->findOneByIdentifier($attributeCode);
$this->productBuilder->addOrReplaceValue($product, $attribute, null, null, $value);
}

$this->lastProductErrors = $this->productValidator->validate($product);
}

/**
* @Then the :code value should be invalid with the message ":message"
*/
public function theValueShouldBeInvalid(string $code, string $message): void
{
if (null === $this->lastProductErrors || count($this->lastProductErrors) === 0) {
throw new \Exception('No validation error found.');
}

foreach ($this->lastProductErrors as $error) {
if (sprintf('values[%s-<all_channels>-<all_locales>].data', $code) === $error->getPropertyPath()
&& $message === $error->getMessage()
) {
return;
}
}

throw new \Exception(
sprintf('The value of "%s" should be invalid but is not, or the error message is wrong.', $code)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Apply validation rules on values
In order to guarantee the quality of my data
As a regular user
I need to be able to validate values using attribute validation rules and be properly informed by a message

Background:
Given an identifier attribute has been created

Scenario: Forbid incorrect email values
Given a text attribute "email_address" with a validation rule "email" has been created
When I create a product containing the following values:
| email_address | not a valid email address |
Then the email_address value should be invalid with the message "This value is not a valid email address."

Scenario: Forbid incorrect URL values
Given a text attribute "link" with a validation rule "url" has been created
When I create a product containing the following values:
| link | https:/www.akeneo.com |
Then the link value should be invalid with the message "This value is not a valid URL."

Scenario: Forbid values not matching a regular expression
Given a text attribute "custom_code" with a validation rule "regexp" and a pattern "/^[a-z]*$/" has been created
When I create a product containing the following values:
| custom_code | 42 |
Then the custom_code value should be invalid with the message "This value is not valid."
Loading