Permalink
Browse files

Add test case for verifying that a Collection preserves original enti…

…ty objects upon form validation
  • Loading branch information...
1 parent 2bb68d7 commit c51865c0fa7fe77241b8182e82f2f28aa9e54ee4 @sjuvonen sjuvonen committed Mar 19, 2013
@@ -1357,4 +1357,47 @@ public function testGetValidationGroupReturnsNullWhenNoneSet()
{
$this->assertNull($this->form->getValidationGroup());
}
+
+ public function testPreserveEntitiesBoundToCollectionAfterValidation() {
+ $this->form->setInputFilter(new \Zend\InputFilter\InputFilter());
+ $fieldset = new TestAsset\ProductCategoriesFieldset();
+ $fieldset->setUseAsBaseFieldset(true);
+
+ $product = new Entity\Product();
+ $product->setName('Foobar');
+ $product->setPrice(100);
+
+ $c1 = new Entity\Category();
+ $c1->setId(1);
+ $c1->setName('First Category');
+
+ $c2 = new Entity\Category();
+ $c2->setId(2);
+ $c2->setName('Second Category');
+
+ $product->setCategories(array($c1, $c2));
+
+ $this->form->add($fieldset);
+ $this->form->bind($product);
+
+ $data = array(
+ 'product' => array(
+ 'name' => 'Barbar',
+ 'price' => 200,
+ 'categories' => array(
+ array('name' => 'Something else'),
+ array('name' => 'Totally different'),
+ ),
+ ),
+ );
+
+ $hash1 = spl_object_hash($this->form->getObject()->getCategory(0));
+ $this->form->setData($data);
+ $this->form->isValid();
+ $hash2 = spl_object_hash($this->form->getObject()->getCategory(0));
+
+ // Returned object has to be the same as when binding or properties
+ // will be lost. (For example entity IDs.)
+ $this->assertTrue($hash1 == $hash2);
+ }
}
@@ -13,6 +13,11 @@
class Category
{
/**
+ * @var int
+ */
+ protected $id;
+
+ /**
* @var string
*/
protected $name;
@@ -34,4 +39,21 @@ public function getName()
{
return $this->name;
}
+
+ /**
+ * @param int $id
+ * @return Product
+ */
+ public function setId($id)
+ {
+ $this->id = $id;
+ return $this;
+ }
+
+ /**
+ * @return int
+ */
+ public function getId() {
+ return $this->id;
+ }
}
@@ -80,4 +80,24 @@ public function getPrice()
{
return $this->price;
}
+
+ /**
+ * Return category from index
+ *
+ * @param int $i
+ */
+ public function getCategory($i)
+ {
+ return $this->categories[$i];
+ }
+
+ /**
+ * Required when binding to a form
+ *
+ * @return array
+ */
+ public function getArrayCopy()
+ {
+ return get_object_vars($this);
+ }
}

0 comments on commit c51865c

Please sign in to comment.