Skip to content

Commit

Permalink
Merge pull request Sylius#2218 from aRn0D/form-country
Browse files Browse the repository at this point in the history
[AddressingBundle] The collection form type uses the new CollectionExtension
  • Loading branch information
Paweł Jędrzejewski committed Dec 11, 2014
2 parents 454dea0 + 68c2def commit 6f7c9e9
Show file tree
Hide file tree
Showing 16 changed files with 422 additions and 127 deletions.
1 change: 1 addition & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ default:
contexts:
- Behat\MinkExtension\Context\MinkContext
- Sylius\Bundle\AddressingBundle\Behat\AddressingContext
- Sylius\Bundle\AddressingBundle\Behat\FormContext
- Sylius\Bundle\CoreBundle\Behat\CoreContext
- Sylius\Bundle\ResourceBundle\Behat\BaseContext
- Sylius\Bundle\WebBundle\Behat\WebContext
Expand Down
19 changes: 16 additions & 3 deletions features/backend/countries.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ Feature: Countries and provinces
When I fill in "Name" with "Poland"
And I fill in "ISO name" with "PL"
And I click "Add province"
And I fill in province name with "Łódź"
And I click "Add province"
And I fill in the 1st province with "Lubusz"
And I fill in the 2nd province with "Łódź"
And I press "Create"
Then I should be on the page of country "Poland"
And I should see "Country has been successfully created."
Then I should see "Country has been successfully created."
And "Łódź" should appear on the page
And "Lubusz" should appear on the page

Scenario: Created countries appear in the list
Given I created country "Poland"
Expand All @@ -100,6 +102,17 @@ Feature: Countries and provinces
Then I should be on the page of country "Russia"
And "Russia" should appear on the page

@javascript
Scenario: Updating the country and removing province
Given I am editing country "Ukraine"
When I fill in "Name" with "Russia"
And I fill in "ISO name" with "RU"
And I remove all the provinces
And I press "Save changes"
Then I should see "Country has been successfully updated."
And "Russia" should appear on the page
But I should not see "Provinces"

@javascript
Scenario: Deleting country via the list
Given I am on the country index page
Expand Down
31 changes: 28 additions & 3 deletions features/backend/zones.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,33 @@ Feature: Zones
Then I should still be on the zone creation page
And I should see "Please add at least 1 zone member."

@javascript
Scenario: Updating the collection form type prototype
Given I am on the zone creation page
And I click "Add member"
And "Country" should appear on the page
And I select "Province" from "Type"
And I click "Add member"
And "Province" should appear on the page
And I select "Zone" from "Type"
And I click "Add member"
And "Zone" should appear on the page

@javascript
Scenario: Creating new zone built from countries
Given I am on the zone creation page
And I fill in "Name" with "EU"
And I select "Country" from "Type"
And I click "Add member"
And I select "Estonia" from "Country"
And I select "shipping" from "Scope"
And I click "Add member"
And I click "Add member"
And I select "Estonia" from the 1st country
And I select "France" from the 2nd country
When I press "Create"
Then I should be on the page of zone "EU"
And I should see "Zone has been successfully created."
And "Estonia" should appear on the page
And "France" should appear on the page
And "shipping" should appear on the page

Scenario: Created zones appear in the list
Expand All @@ -74,7 +89,17 @@ Feature: Zones
When I click "edit" near "USA GMT-8"
Then I should be editing zone "USA GMT-8"

Scenario: Updating the zone
@javascript
Scenario: Updating the zone
Given I am editing zone "USA GMT-8"
When I fill in "Name" with "USA GMT-9"
And I remove the first country
And I press "Save changes"
Then I should be on the page of zone "USA GMT-9"
And I should see "Zone has been successfully updated."
And "Washington" should not appear on the page

Scenario: Updating the zone
Given I am editing zone "USA GMT-8"
When I fill in "Name" with "USA GMT-9"
And I press "Save changes"
Expand Down
60 changes: 60 additions & 0 deletions src/Sylius/Bundle/AddressingBundle/Behat/FormContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\AddressingBundle\Behat;

use Sylius\Bundle\ResourceBundle\Behat\FormContext as BaseFormContext;

/**
* @author Arnaud Langlade <arn0d.dev@gmail.com>
*/
class FormContext extends BaseFormContext
{
/**
* @Given /^I remove all the provinces$/
*/
public function iRemoveAProvince()
{
$items = count($this->getSession()->getPage()->findAll(
'xpath',
'//div[@id="sylius_country_provinces"]//div[@data-form-collection="item"]'
));

while (0 !== $items) {
$this->deleteCollectionItem($items);
$items--;
}
}

/**
* @Given /^I fill in the (\d+)(st|nd|th) province with "([^""]*)"$/
*/
public function fillProvinceName($position, $fake, $value)
{
$this->fillInField('sylius_country[provinces][' . ($position - 1) . '][name]', $value);
}

/**
* @Given /^I select "([^""]*)" from the (\d+)(st|nd|th) country$/
*/
public function fillCountryMember($value, $position, $fake)
{
$this->fillInField('sylius_country[members][' . ($position - 1) . '][country]', $value);
}

/**
* @Given /^I remove the first country$/
*/
public function iRemoveTheFirstCountryMember()
{
$this->deleteCollectionItem(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,13 @@ class ResizeZoneMemberCollectionListener extends ResizeFormListener
*/
protected $classMap = array();

public function __construct(FormFactoryInterface $factory, array $prototypes, array $options = array(), $allowAdd = false, $allowDelete = false)
{
public function __construct(
FormFactoryInterface $factory,
array $prototypes,
array $options = array(),
$allowAdd = false,
$allowDelete = false
) {
$this->factory = $factory;

foreach ($prototypes as $prototype) {
Expand Down Expand Up @@ -100,7 +105,7 @@ public function preSetData(FormEvent $event)
*
* @throws UnexpectedTypeException
*/
public function preBind(FormEvent $event)
public function preSubmit(FormEvent $event)
{
$data = $event->getData();
if (null === $data || '' === $data) {
Expand Down Expand Up @@ -145,6 +150,7 @@ protected function getTypeForObject($object)
* @param array $data
*
* @return string|FormTypeInterface
*
* @throws \InvalidArgumentException when _type is not present or is invalid
*/
protected function getTypeForData(array $data)
Expand All @@ -169,6 +175,9 @@ private function createFormField(FormInterface $form, $type, $name)
), $this->options)));
}

/**
* @param FormInterface $form
*/
private function removeFormFields(FormInterface $form)
{
if ($this->allowDelete) {
Expand All @@ -180,6 +189,10 @@ private function removeFormFields(FormInterface $form)
}
}

/**
* @param FormInterface $form
* @param array $data
*/
private function createFormFields(FormInterface $form, $data)
{
if ($this->allowAdd) {
Expand Down
6 changes: 3 additions & 3 deletions src/Sylius/Bundle/AddressingBundle/Form/Type/CountryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'label' => 'sylius.form.country.iso_name'
))
->add('provinces', 'collection', array(
'type' => 'sylius_province',
'allow_add' => true,
'type' => 'sylius_province',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'sylius.form.country.provinces'
'button_add_label' => 'sylius.country.add_province'
))
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
*
* @author Tim Nagel <t.nagel@infinite.net.au>
* @author Saša Stamenković <umpirsky@gmail.com>
* @author Arnaud Langlade <arn0d.dev@gmail.com>
*/
class ZoneMemberCollectionType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options = array())
{
$prototypes = $this->buildPrototypes($builder, $options);

Expand All @@ -49,55 +50,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder->addEventSubscriber($resizeListener);
}

/**
* Builds prototypes for each of the form types used for the collection.
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return array
*/
protected function buildPrototypes(FormBuilderInterface $builder, array $options)
{
$prototypes = array();
$types = array(
'sylius_zone_member_country',
'sylius_zone_member_province',
'sylius_zone_member_zone',
);
foreach ($types as $type) {
$prototype = $this->buildPrototype($builder, $options['prototype_name'], $type, $options['options'])->getForm();
$prototypes[$type] = $prototype;
}

return $prototypes;
}

/**
* Builds an individual prototype.
*
* @param FormBuilderInterface $builder
* @param string $name
* @param string|FormTypeInterface $type
* @param array $options
*
* @return FormBuilderInterface
*/
protected function buildPrototype(FormBuilderInterface $builder, $name, $type, array $options)
{
return $builder->create($name, $type, array_replace(array(
'label' => $name . 'label__',
), $options));
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['allow_add'] = $options['allow_add'];
$view->vars['allow_delete'] = $options['allow_delete'];

if ($form->getConfig()->hasAttribute('prototypes')) {
$view->vars['prototypes'] = array_map(function (FormInterface $prototype) use ($view) {
return $prototype->createView($view);
Expand All @@ -111,13 +68,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
'prototype_name' => '__name__',
'type_name' => '_type',
'options' => array(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
));
}

Expand All @@ -128,4 +81,36 @@ public function getName()
{
return 'sylius_zone_member_collection';
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'collection';
}

/**
* Builds prototypes for each of the form types used for the collection.
*
* @param FormBuilderInterface $builder
* @param array $options
*
* @return array
*/
protected function buildPrototypes(FormBuilderInterface $builder, array $options)
{
$types = array(
'sylius_zone_member_country',
'sylius_zone_member_province',
'sylius_zone_member_zone',
);

$prototypes = array();
foreach ($types as $type) {
$prototypes[$type] = $builder->create($options['prototype_name'], $type, $options['options'])->getForm();
}

return $prototypes;
}
}
3 changes: 2 additions & 1 deletion src/Sylius/Bundle/AddressingBundle/Form/Type/ZoneType.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
))
->add('type', 'sylius_zone_type_choice')
->add('members', 'sylius_zone_member_collection', array(
'label' => 'sylius.form.zone.members',
'label' => false,
'button_add_label' => 'sylius.zone.add_member',
))
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

<services>
<service id="sylius.form.type.zone_member_collection" class="%sylius.form.type.zone_member_collection.class%">
<argument>%sylius.model.zone.class%</argument>
<tag name="form.type" alias="sylius_zone_member_collection" />
</service>
<service id="sylius.form.type.address" class="%sylius.form.type.address.class%">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,8 @@
'use strict';

$(document).ready(function() {
var typeSelect = $('#sylius_zone_type');

$('form.form-horizontal').on('submit', function(e) {
$('div[id^="sylius-zone-members-"]').not('[id$="'+ typeSelect.val() +'"]').each(function () {
$(this).remove();
});
});

typeSelect.on('change', function() {
var value = $(this).val();
$('div[id^="sylius-zone-members-"]').hide();
$('#sylius-zone-members-' + value).show();
$('a[data-collection-button="add"]')
.data('collection', 'sylius-zone-members-' + value)
.data('prototype', 'sylius-zone-members-' + value)
;
}).trigger('change');
if (0 == $('[data-form-collection="item"]').length) {
$('#sylius_zone_type').trigger('change');
}
});
})( jQuery );
Loading

0 comments on commit 6f7c9e9

Please sign in to comment.