Navigation Menu

Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/5890'
Browse files Browse the repository at this point in the history
Close #5890
  • Loading branch information
weierophinney committed Mar 4, 2014
2 parents 12d88a7 + fc8ae0a commit 71c0bbf
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 2 deletions.
72 changes: 72 additions & 0 deletions tests/ZendTest/Form/Element/CollectionTest.php
Expand Up @@ -23,6 +23,8 @@
use ZendTest\Form\TestAsset\CustomCollection;
use ZendTest\Form\TestAsset\Entity\Product;
use ZendTest\Form\TestAsset\ProductFieldset;
use ZendTest\Form\TestAsset\Entity\Address;
use ZendTest\Form\TestAsset\Entity\Phone;

class CollectionTest extends TestCase
{
Expand Down Expand Up @@ -712,4 +714,74 @@ public function testCanBindObjectMultipleNestedFieldsets()
}
};
}

public function testNestedCollections()
{
// @see https://github.com/zendframework/zf2/issues/5640
$addressesFieldeset = new \ZendTest\Form\TestAsset\AddressFieldset();
$addressesFieldeset->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());

$form = new Form();
$form->setHydrator(new ObjectPropertyHydrator());
$form->add(array(
'name' => 'addresses',
'type' => 'Collection',
'options' => array(
'target_element' => $addressesFieldeset,
'count' => 2
),
));

$data = array(
array('number' => '0000000001', 'street' => 'street1'),
array('number' => '0000000002', 'street' => 'street2'),
);

$phone1 = new Phone();
$phone1->setNumber($data[0]['number']);

$phone2 = new Phone();
$phone2->setNumber($data[1]['number']);

$address1 = new Address();
$address1->setStreet($data[0]['street']);
$address1->setPhones(array($phone1));

$address2 = new Address();
$address2->setStreet($data[1]['street']);
$address2->setPhones(array($phone2));

$customer = new stdClass();
$customer->addresses = array($address1, $address2);

$form->bind($customer);

//test for object binding
foreach ($form->get('addresses')->getFieldsets() as $_fieldset) {
$this->assertInstanceOf('ZendTest\Form\TestAsset\Entity\Address', $_fieldset->getObject());
foreach ($_fieldset->getFieldsets() as $_childFieldsetName => $_childFieldset) {
switch ($_childFieldsetName) {
case 'city':
$this->assertInstanceOf('ZendTest\Form\TestAsset\Entity\City', $_childFieldset->getObject());
break;
case 'phones':
foreach ($_childFieldset->getFieldsets() as $_phoneFieldset) {
$this->assertInstanceOf('ZendTest\Form\TestAsset\Entity\Phone', $_phoneFieldset->getObject());
}
break;
}
}
}

//test for correct extract and populate
$index = 0;
foreach ($form->get('addresses') as $_addresses) {
$this->assertEquals($data[$index]['street'], $_addresses->get('street')->getValue());
//assuming data has just 1 phone entry
foreach ($_addresses->get('phones') as $phone) {
$this->assertEquals($data[$index]['number'], $phone->get('number')->getValue());
}
$index++;
}
}
}
10 changes: 10 additions & 0 deletions tests/ZendTest/Form/TestAsset/AddressFieldset.php
Expand Up @@ -29,6 +29,16 @@ public function __construct()

$this->add($street);
$this->add($city);

$phones = new \Zend\Form\Element\Collection('phones');
$phones->setLabel('Phone numbers')
->setOptions(array(
'count' => 2,
'allow_add' => true,
'allow_remove' => true,
'target_element' => new PhoneFieldset(),
));
$this->add($phones);
}

/**
Expand Down
27 changes: 25 additions & 2 deletions tests/ZendTest/Form/TestAsset/Entity/Address.php
Expand Up @@ -21,10 +21,15 @@ class Address
*/
protected $city;

/**
* @var array
*/
protected $phones = array();


/**
* @param $street
* @return Address
* @return self
*/
public function setStreet($street)
{
Expand All @@ -42,7 +47,7 @@ public function getStreet()

/**
* @param City $city
* @return Address
* @return self
*/
public function setCity(City $city)
{
Expand All @@ -57,4 +62,22 @@ public function getCity()
{
return $this->city;
}

/**
* @param array $phones
* @return self
*/
public function setPhones(array $phones)
{
$this->phones = $phones;
return $this;
}

/**
* @return array
*/
public function getPhones()
{
return $this->phones;
}
}
36 changes: 36 additions & 0 deletions tests/ZendTest/Form/TestAsset/Entity/Phone.php
@@ -0,0 +1,36 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Form\TestAsset\Entity;

class Phone
{
/**
* @var string
*/
protected $number;

/**
* @param string $number
* @return self
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}

/**
* @return string
*/
public function getNumber()
{
return $this->number;
}
}
35 changes: 35 additions & 0 deletions tests/ZendTest/Form/TestAsset/PhoneFieldset.php
@@ -0,0 +1,35 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Form\TestAsset;


use Zend\Form\Fieldset;
use Zend\Form\Element;
use Zend\Stdlib\Hydrator\ClassMethods as ClassMethodsHydrator;
use ZendTest\Form\TestAsset\Entity\Phone;

class PhoneFieldset extends Fieldset
{
public function __construct()
{
parent::__construct('phones');

$this->setHydrator(new ClassMethodsHydrator)
->setObject(new Phone());

$id = new Element\Hidden('id');
$this->add($id);

$number = new Element\Text('number');
$number->setLabel('Number')
->setAttribute('class', 'form-control');
$this->add($number);
}
}

0 comments on commit 71c0bbf

Please sign in to comment.