From eecf6b6824afebbcd1f0f16e2623f49aebeec316 Mon Sep 17 00:00:00 2001 From: Mateusz Zalewski Date: Tue, 15 Nov 2016 13:51:40 +0100 Subject: [PATCH] [Core][Admin] Choose channels on shipping method form --- .../adding_shipping_method.feature | 15 ++++++ .../Admin/ManagingPaymentMethodsContext.php | 2 +- .../Admin/ManagingShippingMethodsContext.php | 23 +++++++++ .../Page/Admin/ShippingMethod/CreatePage.php | 16 ++++++ .../ShippingMethod/CreatePageInterface.php | 5 ++ .../Page/Admin/ShippingMethod/UpdatePage.php | 9 ++++ .../ShippingMethod/UpdatePageInterface.php | 7 +++ .../views/ShippingMethod/_form.html.twig | 1 + .../Form/Type/ShippingMethodType.php | 5 ++ .../doctrine/model/ShippingMethod.orm.xml | 11 ++++ .../Resources/translations/messages.en.yml | 1 + .../Component/Core/Model/ShippingMethod.php | 51 +++++++++++++++++++ .../Core/Model/ShippingMethodInterface.php | 3 +- .../Core/spec/Model/ShippingMethodSpec.php | 39 ++++++++++++++ 14 files changed, 186 insertions(+), 2 deletions(-) diff --git a/features/shipping/managing_shipping_methods/adding_shipping_method.feature b/features/shipping/managing_shipping_methods/adding_shipping_method.feature index 9fedacab4c..256d18acf9 100644 --- a/features/shipping/managing_shipping_methods/adding_shipping_method.feature +++ b/features/shipping/managing_shipping_methods/adding_shipping_method.feature @@ -46,3 +46,18 @@ Feature: Adding a new shipping method And I add it Then I should be notified that it has been successfully created And the shipping method "FedEx Carrier" should appear in the registry + + @ui @javascript + Scenario: Adding a new shipping method for channel + Given I want to create a new shipping method + When I specify its code as "FED_EX_CARRIER" + And I name it "FedEx Carrier" in "English (United States)" + And I describe it as "FedEx Carrier shipping method for United States" in "English (United States)" + And I define it for the "United States" zone + And I make it available in channel "United States" + And I choose "Flat rate per unit" calculator + And I specify its amount as 20 + And I add it + Then I should be notified that it has been successfully created + And the shipment method "FedEx Carrier" should appear in the registry + And the shipment method "FedEx Carrier" should be available in channel "United States" diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php index 34ff72e0b5..a4fdc0fab9 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingPaymentMethodsContext.php @@ -367,7 +367,7 @@ final class ManagingPaymentMethodsContext implements Context Assert::true( $this->updatePage->isAvailableInChannel($channelName), - sprintf('Payment should be available in channel "%s" but it does not.', $channelName) + sprintf('Payment method should be available in channel "%s" but it does not.', $channelName) ); } diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingShippingMethodsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingShippingMethodsContext.php index 70c7d49373..745c8e35ac 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingShippingMethodsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingShippingMethodsContext.php @@ -141,6 +141,14 @@ final class ManagingShippingMethodsContext implements Context $this->createPage->specifyAmount($amount); } + /** + * @Given I make it available in channel :channel + */ + public function iMakeItAvaialbelInChannel($channel) + { + $this->createPage->checkChannel($channel); + } + /** * @When I add it * @When I try to add it @@ -181,6 +189,21 @@ final class ManagingShippingMethodsContext implements Context $this->theShipmentMethodShouldAppearInTheRegistry($shippingMethod->getName()); } + /** + * @Given the shipment method :shippingMethod should be available in channel :channelName + */ + public function theShippingMethodShouldBeAvailableInChannel( + ShippingMethodInterface $shippingMethod, + $channelName + ) { + $this->iWantToModifyAShippingMethod($shippingMethod); + + Assert::true( + $this->updatePage->isAvailableInChannel($channelName), + sprintf('Shipping method should be available in channel "%s" but it does not.', $channelName) + ); + } + /** * @Then I should be notified that shipping method with this code already exists */ diff --git a/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePage.php b/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePage.php index 8fdbd0b231..fdcfab3120 100644 --- a/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePage.php +++ b/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePage.php @@ -11,6 +11,7 @@ namespace Sylius\Behat\Page\Admin\ShippingMethod; +use Behat\Mink\Driver\Selenium2Driver; use Sylius\Behat\Behaviour\ChoosesCalculator; use Sylius\Behat\Behaviour\SpecifiesItsAmount; use Sylius\Behat\Behaviour\SpecifiesItsCode; @@ -73,6 +74,20 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface $this->getDocument()->selectFieldOption('Calculator', $name); } + /** + * {@inheritdoc} + */ + public function checkChannel($channel) + { + if ($this->getDriver() instanceof Selenium2Driver) { + $this->getElement('channel', ['%channel%' => $channel])->click(); + + return; + } + + $this->getDocument()->checkField($channel); + } + /** * {@inheritdoc} */ @@ -80,6 +95,7 @@ class CreatePage extends BaseCreatePage implements CreatePageInterface { return array_merge(parent::getDefinedElements(), [ 'amount' => '#sylius_shipping_method_configuration_amount', + 'channel' => '#sylius_shipping_method_channels .ui.checkbox:contains("%channel%")', 'calculator' => '#sylius_shipping_method_calculator', 'code' => '#sylius_shipping_method_code', 'name' => '#sylius_shipping_method_translations_en_US_name', diff --git a/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePageInterface.php b/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePageInterface.php index 15463b3180..314f549d42 100644 --- a/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePageInterface.php +++ b/src/Sylius/Behat/Page/Admin/ShippingMethod/CreatePageInterface.php @@ -54,4 +54,9 @@ interface CreatePageInterface extends BaseCreatePageInterface * @param string $name */ public function chooseCalculator($name); + + /** + * @return string $channel + */ + public function checkChannel($channel); } diff --git a/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePage.php b/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePage.php index d02d2a07bf..095b2778b0 100644 --- a/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePage.php +++ b/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePage.php @@ -23,6 +23,14 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface use ChecksCodeImmutability; use Toggles; + /** + * {@inheritdoc} + */ + public function isAvailableInChannel($channelName) + { + return $this->getElement('channel', ['%channel%' => $channelName])->hasAttribute('checked'); + } + /** * {@inheritdoc} */ @@ -45,6 +53,7 @@ class UpdatePage extends BaseUpdatePage implements UpdatePageInterface protected function getDefinedElements() { return array_merge(parent::getDefinedElements(), [ + 'channel' => '.checkbox:contains("%channel%") input', 'code' => '#sylius_shipping_method_code', 'enabled' => '#sylius_shipping_method_enabled', 'name' => '#sylius_shipping_method_translations_en_US_name', diff --git a/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePageInterface.php b/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePageInterface.php index fee15f3a1f..4d444ff071 100644 --- a/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePageInterface.php +++ b/src/Sylius/Behat/Page/Admin/ShippingMethod/UpdatePageInterface.php @@ -23,6 +23,13 @@ interface UpdatePageInterface extends BaseUpdatePageInterface */ public function isCodeDisabled(); + /** + * @param string $channelName + * + * @return bool + */ + public function isAvailableInChannel($channelName); + public function enable(); public function disable(); diff --git a/src/Sylius/Bundle/AdminBundle/Resources/views/ShippingMethod/_form.html.twig b/src/Sylius/Bundle/AdminBundle/Resources/views/ShippingMethod/_form.html.twig index 1ea52870ac..2f8c4cf725 100644 --- a/src/Sylius/Bundle/AdminBundle/Resources/views/ShippingMethod/_form.html.twig +++ b/src/Sylius/Bundle/AdminBundle/Resources/views/ShippingMethod/_form.html.twig @@ -6,6 +6,7 @@ {{ form_row(form.code) }} {{ form_row(form.position) }} {{ form_row(form.zone) }} + {{ form_row(form.channels) }} {{ form_row(form.enabled) }}

{{ 'sylius.ui.category_requirements'|trans }}

diff --git a/src/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php b/src/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php index 35dd289aaf..9de07f1d3c 100644 --- a/src/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php +++ b/src/Sylius/Bundle/CoreBundle/Form/Type/ShippingMethodType.php @@ -35,6 +35,11 @@ class ShippingMethodType extends BaseShippingMethodType 'empty_value' => '---', 'label' => 'sylius.form.shipping_method.tax_category', ]) + ->add('channels', 'sylius_channel_choice', [ + 'multiple' => true, + 'expanded' => true, + 'label' => 'sylius.form.shipping_method.channels', + ]) ; } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ShippingMethod.orm.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ShippingMethod.orm.xml index f4e15aa948..1d099dbc62 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ShippingMethod.orm.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ShippingMethod.orm.xml @@ -24,6 +24,17 @@ + + + + + + + + + + + diff --git a/src/Sylius/Bundle/ShippingBundle/Resources/translations/messages.en.yml b/src/Sylius/Bundle/ShippingBundle/Resources/translations/messages.en.yml index 8a6d3bce60..b87aab9991 100644 --- a/src/Sylius/Bundle/ShippingBundle/Resources/translations/messages.en.yml +++ b/src/Sylius/Bundle/ShippingBundle/Resources/translations/messages.en.yml @@ -55,6 +55,7 @@ sylius: calculator: Calculator category: Category category_requirement: Category requirement + channels: Channels configuration: Configuration description: Description enabled: Enabled diff --git a/src/Sylius/Component/Core/Model/ShippingMethod.php b/src/Sylius/Component/Core/Model/ShippingMethod.php index bd64c59c3d..c32a2ce7a4 100644 --- a/src/Sylius/Component/Core/Model/ShippingMethod.php +++ b/src/Sylius/Component/Core/Model/ShippingMethod.php @@ -11,7 +11,10 @@ namespace Sylius\Component\Core\Model; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Sylius\Component\Addressing\Model\ZoneInterface; +use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface; use Sylius\Component\Shipping\Model\ShippingMethod as BaseShippingMethod; use Sylius\Component\Shipping\Model\ShippingMethodTranslation; use Sylius\Component\Taxation\Model\TaxCategoryInterface; @@ -32,6 +35,18 @@ class ShippingMethod extends BaseShippingMethod implements ShippingMethodInterfa */ protected $taxCategory; + /** + * @var Collection + */ + private $channels; + + public function __construct() + { + parent::__construct(); + + $this->channels = new ArrayCollection(); + } + /** * {@inheritdoc} */ @@ -64,6 +79,42 @@ class ShippingMethod extends BaseShippingMethod implements ShippingMethodInterfa $this->taxCategory = $category; } + /** + * {@inheritdoc} + */ + public function getChannels() + { + return $this->channels; + } + + /** + * {@inheritdoc} + */ + public function hasChannel(BaseChannelInterface $channel) + { + return $this->channels->contains($channel); + } + + /** + * {@inheritdoc} + */ + public function addChannel(BaseChannelInterface $channel) + { + if (!$this->hasChannel($channel)) { + $this->channels->add($channel); + } + } + + /** + * {@inheritdoc} + */ + public function removeChannel(BaseChannelInterface $channel) + { + if ($this->hasChannel($channel)) { + $this->channels->removeElement($channel); + } + } + /** * {@inheritdoc} */ diff --git a/src/Sylius/Component/Core/Model/ShippingMethodInterface.php b/src/Sylius/Component/Core/Model/ShippingMethodInterface.php index 64f71dfa75..cdccb00ccb 100644 --- a/src/Sylius/Component/Core/Model/ShippingMethodInterface.php +++ b/src/Sylius/Component/Core/Model/ShippingMethodInterface.php @@ -12,6 +12,7 @@ namespace Sylius\Component\Core\Model; use Sylius\Component\Addressing\Model\ZoneInterface; +use Sylius\Component\Channel\Model\ChannelsAwareInterface; use Sylius\Component\Shipping\Model\ShippingMethodInterface as BaseShippingMethodInterface; use Sylius\Component\Taxation\Model\TaxableInterface; use Sylius\Component\Taxation\Model\TaxCategoryInterface; @@ -19,7 +20,7 @@ use Sylius\Component\Taxation\Model\TaxCategoryInterface; /** * @author Paweł Jędrzejewski */ -interface ShippingMethodInterface extends BaseShippingMethodInterface, TaxableInterface +interface ShippingMethodInterface extends BaseShippingMethodInterface, TaxableInterface, ChannelsAwareInterface { /** * @return ZoneInterface diff --git a/src/Sylius/Component/Core/spec/Model/ShippingMethodSpec.php b/src/Sylius/Component/Core/spec/Model/ShippingMethodSpec.php index e99d63fe22..07500e2e44 100644 --- a/src/Sylius/Component/Core/spec/Model/ShippingMethodSpec.php +++ b/src/Sylius/Component/Core/spec/Model/ShippingMethodSpec.php @@ -11,8 +11,11 @@ namespace spec\Sylius\Component\Core\Model; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use PhpSpec\ObjectBehavior; use Sylius\Component\Addressing\Model\ZoneInterface; +use Sylius\Component\Core\Model\ChannelInterface; use Sylius\Component\Core\Model\ShippingMethod; use Sylius\Component\Core\Model\ShippingMethodInterface; use Sylius\Component\Shipping\Model\ShippingMethod as BaseShippingMethod; @@ -51,4 +54,40 @@ final class ShippingMethodSpec extends ObjectBehavior $this->setTaxCategory($category); $this->getTaxCategory()->shouldReturn($category); } + + function it_has_channels_collection(ChannelInterface $firstChannel, ChannelInterface $secondChannel) + { + $this->addChannel($firstChannel); + $this->addChannel($secondChannel); + + $this->getChannels()->shouldBeSameAs(new ArrayCollection([$firstChannel, $secondChannel])); + } + + function it_can_add_and_remove_channels(ChannelInterface $channel) + { + $this->addChannel($channel); + $this->hasChannel($channel)->shouldReturn(true); + + $this->removeChannel($channel); + $this->hasChannel($channel)->shouldReturn(false); + } + + public function getMatchers() + { + return [ + 'beSameAs' => function ($subject, $key) { + if (!$subject instanceof Collection || !$key instanceof Collection) { + return false; + } + + for ($i = 0; $i < $subject->count(); $i++) { + if ($subject->get($i) !== $key->get($i)->getWrappedObject()) { + return false; + } + } + + return true; + }, + ]; + } }