-
Notifications
You must be signed in to change notification settings - Fork 514
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
Changes from all commits
073bfeb
b9425df
054e3fd
0b2895e
fb017e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
imports: | ||
- { 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks ok to me |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ | |
"classmap": [ "app/AppKernel.php", "app/AppCache.php", "tests/AppKernelTest.php" ] | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"Pim\\Test\\": "tests/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
] | ||
|
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." |
There was a problem hiding this comment.
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 ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right ! I forgot to push the Behat conf. Here it is : https://github.com/akeneo/pim-community-dev/pull/7209/files#diff-7bde54db60a6e933518d8b61b929edceR121