[Payum][Admin][Behat] Validation and paypal improvements

This commit is contained in:
Mateusz Zalewski 2017-01-11 13:40:31 +01:00
parent 03d9c25685
commit 76da7cbeb3
No known key found for this signature in database
GPG key ID: BE101BD4D91463A4
10 changed files with 252 additions and 12 deletions

View file

@ -11,34 +11,42 @@ Feature: Payment method validation
@ui @ui
Scenario: Trying to add a new payment method without specifying its code Scenario: Trying to add a new payment method without specifying its code
Given I want to create a new payment method Given I want to create a new payment method with "Paypal Express Checkout" gateway factory
When I name it "Paypal Express Checkout" in "English (United States)" When I name it "Paypal Express Checkout" in "English (United States)"
But I do not specify its code But I do not specify its code
And I choose "Paypal Express Checkout" gateway
And I add it And I add it
Then I should be notified that code is required Then I should be notified that code is required
And the payment method with name "Paypal Express Checkout" should not be added And the payment method with name "Paypal Express Checkout" should not be added
@ui @ui
Scenario: Trying to add a new payment method without specifying its name Scenario: Trying to add a new payment method without specifying its name
Given I want to create a new payment method Given I want to create a new payment method with "Paypal Express Checkout" gateway factory
When I specify its code as "PEC" When I specify its code as "PEC"
And I choose "Paypal Express Checkout" gateway
But I do not name it But I do not name it
And I add it And I add it
Then I should be notified that name is required Then I should be notified that name is required
And the payment method with code "PEC" should not be added And the payment method with code "PEC" should not be added
@ui @ui
Scenario: Trying to add a new payment method without specifying required configuration Scenario: Trying to add a new paypal payment method without specifying required configuration
Given I want to create a new payment method Given I want to create a new payment method with "Paypal Express Checkout" gateway factory
When I name it "Paypal Express Checkout" in "English (United States)" When I name it "Paypal Express Checkout" in "English (United States)"
And I specify its code as "PEC" And I specify its code as "PEC"
And I choose "Paypal Express Checkout" gateway
And I configure it for username "TEST" with "TEST" signature And I configure it for username "TEST" with "TEST" signature
But I do not specify configuration password But I do not specify configuration password
And I add it And I add it
Then I should be notified that password is required Then I should be notified that I must specify paypal password
And the payment method with code "PEC" should not be added
@ui
Scenario: Trying to add a new paypal payment method with wrong name
Given I want to create a new payment method with "Paypal Express Checkout" gateway factory
When I name it "Paypal Express Checkout" in "English (United States)"
And I specify its code as "PEC"
And I configure it for username "TEST", with "TEST" password and "TEST" signature
And I name the gateway "Paypal express checkout"
And I add it
Then I should be notified that gateway name should contain only letters and underscores
And the payment method with code "PEC" should not be added And the payment method with code "PEC" should not be added
@ui @ui

View file

@ -124,12 +124,18 @@ final class PaymentContext implements Context
$password, $password,
$signature $signature
) { ) {
$paymentMethod->getGatewayConfig()->setConfig([ $config = [
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,
'signature' => $signature, 'signature' => $signature,
'sandbox' => true, 'sandbox' => true,
]); ];
if ('paypal_express_checkout' === $paymentMethod->getGatewayConfig()->getFactoryName()) {
$config = array_merge($config, ['payum.http_client' => '@sylius.payum.http_client']);
}
$paymentMethod->getGatewayConfig()->setConfig($config);
$this->objectManager->flush(); $this->objectManager->flush();
} }
@ -196,9 +202,11 @@ final class PaymentContext implements Context
$addForCurrentChannel = true, $addForCurrentChannel = true,
$position = null $position = null
) { ) {
$gatewayFactory = array_search($gatewayFactory, $this->gatewayFactories);
/** @var PaymentMethodInterface $paymentMethod */ /** @var PaymentMethodInterface $paymentMethod */
$paymentMethod = $this->paymentMethodFactory->createWithGateway(array_search($gatewayFactory, $this->gatewayFactories)); $paymentMethod = $this->paymentMethodFactory->createWithGateway($gatewayFactory);
$paymentMethod->getGatewayConfig()->setGatewayName(array_search($gatewayFactory, $this->gatewayFactories)); $paymentMethod->getGatewayConfig()->setGatewayName($gatewayFactory);
$paymentMethod->setName(ucfirst($name)); $paymentMethod->setName(ucfirst($name));
$paymentMethod->setCode($code); $paymentMethod->setCode($code);
$paymentMethod->setPosition($position); $paymentMethod->setPosition($position);

View file

@ -295,6 +295,28 @@ final class ManagingPaymentMethodsContext implements Context
$this->assertFieldValidationMessage($element, sprintf('Please enter payment method %s.', $element)); $this->assertFieldValidationMessage($element, sprintf('Please enter payment method %s.', $element));
} }
/**
* @Then I should be notified that I must specify paypal :element
*/
public function iShouldBeNotifiedThatIMustSpecifyPaypal($element)
{
Assert::same(
$this->createPage->getValidationMessage('paypal_'.$element),
sprintf('Please enter paypal %s.', $element)
);
}
/**
* @Then I should be notified that gateway name should contain only letters and underscores
*/
public function iShouldBeNotifiedThatGatewayNameShouldContainOnlyLettersAndUnderscores()
{
Assert::same(
$this->createPage->getValidationMessage('gateway_name'),
'Gateway name should contain only letters and underscores.'
);
}
/** /**
* @Then the payment method with :element :value should not be added * @Then the payment method with :element :value should not be added
*/ */

View file

@ -128,7 +128,9 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface
'code' => '#sylius_payment_method_code', 'code' => '#sylius_payment_method_code',
'enabled' => '#sylius_payment_method_enabled', 'enabled' => '#sylius_payment_method_enabled',
'gateway' => '#sylius_payment_method_gateway', 'gateway' => '#sylius_payment_method_gateway',
'gateway_name' => '#sylius_payment_method_gatewayConfig_gatewayName',
'name' => '#sylius_payment_method_translations_en_US_name', 'name' => '#sylius_payment_method_translations_en_US_name',
'paypal_password' => '#sylius_payment_method_gatewayConfig_config_password',
]); ]);
} }
} }

View file

@ -0,0 +1,46 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sylius\Bundle\PayumBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
final class PaypalGatewayConfigurationTransformer implements DataTransformerInterface
{
/**
* {@inheritdoc}
*/
public function transform($value)
{
if (empty($value)) {
return $value;
}
$value['payum_http_client'] = $value['payum.http_client'];
unset($value['payum.http_client']);
return $value;
}
/**
* {@inheritdoc}
*/
public function reverseTransform($value)
{
$value['payum.http_client'] = $value['payum_http_client'];
unset($value['payum_http_client']);
return $value;
}
}

View file

@ -11,10 +11,13 @@
namespace Sylius\Bundle\PayumBundle\Form\Type; namespace Sylius\Bundle\PayumBundle\Form\Type;
use Sylius\Bundle\PayumBundle\Form\DataTransformer\PaypalGatewayConfigurationTransformer;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
/** /**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com> * @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
@ -29,13 +32,37 @@ final class PaypalGatewayConfigurationType extends AbstractType implements Gatew
$builder $builder
->add('username', TextType::class, [ ->add('username', TextType::class, [
'label' => 'sylius.form.gateway_configuration.paypal.username', 'label' => 'sylius.form.gateway_configuration.paypal.username',
'constraints' => [
new NotBlank([
'message' => 'sylius.gateway_config.paypal.username.not_blank',
'groups' => 'sylius',
])
],
]) ])
->add('password', PasswordType::class, [ ->add('password', PasswordType::class, [
'label' => 'sylius.form.gateway_configuration.paypal.password', 'label' => 'sylius.form.gateway_configuration.paypal.password',
'constraints' => [
new NotBlank([
'message' => 'sylius.gateway_config.paypal.password.not_blank',
'groups' => 'sylius',
])
],
]) ])
->add('signature', TextType::class, [ ->add('signature', TextType::class, [
'label' => 'sylius.form.gateway_configuration.paypal.signature', 'label' => 'sylius.form.gateway_configuration.paypal.signature',
'constraints' => [
new NotBlank([
'message' => 'sylius.gateway_config.paypal.signature.not_blank',
'groups' => 'sylius',
])
],
]) ])
->add('payum_http_client', HiddenType::class, [
'label' => false,
// Hardcoded, as its the only payum client used now in sylius, could be extended to support many clients
'data' => '@sylius.payum.http_client',
])
->addModelTransformer(new PaypalGatewayConfigurationTransformer())
; ;
} }
} }

View file

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This file is part of the Sylius package.
(c) Paweł Jędrzejewski
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Bundle\PayumBundle\Model\GatewayConfig">
<property name="gatewayName">
<constraint name="NotBlank">
<option name="message">sylius.gateway_config.gateway_name.not_blank</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Regex">
<option name="message">sylius.gateway_config.gateway_name.regex</option>
<option name="pattern">/^[a-zA-Z_]*$/</option>
<option name="groups">sylius</option>
</constraint>
</property>
</class>
</constraint-mapping>

View file

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This file is part of the Sylius package.
(c) Paweł Jędrzejewski
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
-->
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Sylius\Bundle\PayumBundle\Model\PaymentMethod">
<property name="gatewayConfig">
<constraint name="Valid" />
</property>
</class>
</constraint-mapping>

View file

@ -0,0 +1,12 @@
sylius:
gateway_config:
gateway_name:
not_blank: Please enter gateway name.
regex: Gateway name should contain only letters and underscores.
paypal:
password:
not_blank: Please enter paypal password.
signature:
not_blank: Please enter paypal signature.
username:
not_blank: Please enter paypal username.

View file

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace spec\Sylius\Bundle\PayumBundle\Form\DataTransformer;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\PayumBundle\Form\DataTransformer\PaypalGatewayConfigurationTransformer;
use Symfony\Component\Form\DataTransformerInterface;
/**
* @author Mateusz Zalewski <mateusz.zalewski@lakion.com>
*/
final class PaypalGatewayConfigurationTransformerSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(PaypalGatewayConfigurationTransformer::class);
}
function it_implements_data_transformer_interface()
{
$this->shouldImplement(DataTransformerInterface::class);
}
function it_changes_paypal_gateway_configuration_to_be_handled_well_by_payum()
{
$this->reverseTransform([
'username' => 'TEST',
'password' => 'TEST',
'signature' => 'TEST',
'payum_http_client' => '@sylius.payum.http_client'
])->shouldReturn([
'username' => 'TEST',
'password' => 'TEST',
'signature' => 'TEST',
'payum.http_client' => '@sylius.payum.http_client'
]);
}
function it_changes_stored_configuration_to_be_properly_handled_by_form()
{
$this->transform([
'username' => 'TEST',
'password' => 'TEST',
'signature' => 'TEST',
'payum.http_client' => '@sylius.payum.http_client'
])->shouldReturn([
'username' => 'TEST',
'password' => 'TEST',
'signature' => 'TEST',
'payum_http_client' => '@sylius.payum.http_client'
]);
}
function it_does_not_transform_empty_array()
{
$this->transform([])->shouldReturn([]);
}
}