[Core][Admin] Choose channels on shipping method form

This commit is contained in:
Mateusz Zalewski 2016-11-15 13:51:40 +01:00
parent c815b5586d
commit eecf6b6824
No known key found for this signature in database
GPG key ID: CFC1E4176165876B
14 changed files with 186 additions and 2 deletions

View file

@ -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"

View file

@ -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)
);
}

View file

@ -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
*/

View file

@ -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',

View file

@ -54,4 +54,9 @@ interface CreatePageInterface extends BaseCreatePageInterface
* @param string $name
*/
public function chooseCalculator($name);
/**
* @return string $channel
*/
public function checkChannel($channel);
}

View file

@ -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',

View file

@ -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();

View file

@ -6,6 +6,7 @@
{{ form_row(form.code) }}
{{ form_row(form.position) }}
{{ form_row(form.zone) }}
{{ form_row(form.channels) }}
</div>
{{ form_row(form.enabled) }}
<h4 class="ui dividing header">{{ 'sylius.ui.category_requirements'|trans }}</h4>

View file

@ -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',
])
;
}
}

View file

@ -24,6 +24,17 @@
<many-to-one field="taxCategory" target-entity="Sylius\Component\Taxation\Model\TaxCategoryInterface">
<join-column name="tax_category_id" referenced-column-name="id" nullable="true" on-delete="SET NULL" />
</many-to-one>
<many-to-many field="channels" target-entity="Sylius\Component\Channel\Model\ChannelInterface">
<join-table name="sylius_shipping_method_channels">
<join-columns>
<join-column name="shipping_method_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
</join-columns>
<inverse-join-columns>
<join-column name="channel_id" referenced-column-name="id" nullable="false" on-delete="CASCADE" />
</inverse-join-columns>
</join-table>
</many-to-many>
</mapped-superclass>
</doctrine-mapping>

View file

@ -55,6 +55,7 @@ sylius:
calculator: Calculator
category: Category
category_requirement: Category requirement
channels: Channels
configuration: Configuration
description: Description
enabled: Enabled

View file

@ -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}
*/

View file

@ -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 <pawel@sylius.org>
*/
interface ShippingMethodInterface extends BaseShippingMethodInterface, TaxableInterface
interface ShippingMethodInterface extends BaseShippingMethodInterface, TaxableInterface, ChannelsAwareInterface
{
/**
* @return ZoneInterface

View file

@ -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;
},
];
}
}