Ejemplo n.º 1
0
    public function testDoesNotCreateNewObjects()
    {
        $form = new \Zend\Form\Form();
        $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
        $this->productFieldset->setUseAsBaseFieldset(true);
        $form->add($this->productFieldset);

        $product = new Product();
        $product->setName("foo");
        $product->setPrice(42);
        $cat1 = new \ZendTest\Form\TestAsset\Entity\Category();
        $cat1->setName("bar");
        $cat2 = new \ZendTest\Form\TestAsset\Entity\Category();
        $cat2->setName("bar2");

        $product->setCategories(array($cat1,$cat2));

        $form->bind($product);

        $form->setData(
            array("product"=>
                array(
                    "name" => "franz",
                    "price" => 13,
                    "categories" => array(
                        array("name" => "sepp"),
                        array("name" => "herbert")
                    )
                )
            )
        );
        $form->isValid();

        $categories = $product->getCategories();
        $this->assertSame($categories[0], $cat1);
        $this->assertSame($categories[1], $cat2);
    }
Ejemplo n.º 2
0
 public function testExtractFromObjectDoesntTouchOriginalObject()
 {
     $form = new \Zend\Form\Form();
     $form->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
     $this->productFieldset->setUseAsBaseFieldset(true);
     $form->add($this->productFieldset);
     $originalObjectHash = spl_object_hash($this->productFieldset->get("categories")->getTargetElement()->getObject());
     $product = new Product();
     $product->setName("foo");
     $product->setPrice(42);
     $cat1 = new \ZendTest\Form\TestAsset\Entity\Category();
     $cat1->setName("bar");
     $cat2 = new \ZendTest\Form\TestAsset\Entity\Category();
     $cat2->setName("bar2");
     $product->setCategories(array($cat1, $cat2));
     $form->bind($product);
     $form->setData(array("product" => array("name" => "franz", "price" => 13, "categories" => array(array("name" => "sepp"), array("name" => "herbert")))));
     $objectAfterExtractHash = spl_object_hash($this->productFieldset->get("categories")->getTargetElement()->getObject());
     $this->assertSame($originalObjectHash, $objectAfterExtractHash);
 }
Ejemplo n.º 3
0
 public function testCollectionCanBindObjectAndPopulateAndExtractNestedFieldsets()
 {
     $productFieldset = new \ZendTest\Form\TestAsset\ProductFieldset();
     $productFieldset->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
     $mainFieldset = new Fieldset();
     $mainFieldset->setObject(new stdClass());
     $mainFieldset->setHydrator(new ObjectPropertyHydrator());
     $mainFieldset->add($productFieldset);
     $form = new Form();
     $form->setHydrator(new ObjectPropertyHydrator());
     $form->add(array('name' => 'collection', 'type' => 'Collection', 'options' => array('target_element' => $mainFieldset, 'count' => 2)));
     $market = new stdClass();
     $prices = array(100, 200);
     $categoryNames = array('electronics', 'furniture');
     $productCountries = array('Russia', 'Jamaica');
     $shop1 = new stdClass();
     $shop1->product = new Product();
     $shop1->product->setPrice($prices[0]);
     $category = new \ZendTest\Form\TestAsset\Entity\Category();
     $category->setName($categoryNames[0]);
     $shop1->product->setCategories(array($category));
     $country = new \ZendTest\Form\TestAsset\Entity\Country();
     $country->setName($productCountries[0]);
     $shop1->product->setMadeInCountry($country);
     $shop2 = new stdClass();
     $shop2->product = new Product();
     $shop2->product->setPrice($prices[1]);
     $category = new \ZendTest\Form\TestAsset\Entity\Category();
     $category->setName($categoryNames[1]);
     $shop2->product->setCategories(array($category));
     $country = new \ZendTest\Form\TestAsset\Entity\Country();
     $country->setName($productCountries[1]);
     $shop2->product->setMadeInCountry($country);
     $market->collection = array($shop1, $shop2);
     $form->bind($market);
     //test for object binding
     $_marketCollection = $form->get('collection');
     $this->assertInstanceOf('Zend\\Form\\Element\\Collection', $_marketCollection);
     foreach ($_marketCollection as $_shopFieldset) {
         $this->assertInstanceOf('Zend\\Form\\Fieldset', $_shopFieldset);
         $this->assertInstanceOf('stdClass', $_shopFieldset->getObject());
         // test for collection -> fieldset
         $_productFieldset = $_shopFieldset->get('product');
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\ProductFieldset', $_productFieldset);
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Product', $_productFieldset->getObject());
         // test for collection -> fieldset -> fieldset
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\CountryFieldset', $_productFieldset->get('made_in_country'));
         $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Country', $_productFieldset->get('made_in_country')->getObject());
         // test for collection -> fieldset -> collection
         $_productCategories = $_productFieldset->get('categories');
         $this->assertInstanceOf('Zend\\Form\\Element\\Collection', $_productCategories);
         // test for collection -> fieldset -> collection -> fieldset
         foreach ($_productCategories as $_category) {
             $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\CategoryFieldset', $_category);
             $this->assertInstanceOf('ZendTest\\Form\\TestAsset\\Entity\\Category', $_category->getObject());
         }
     }
     // test for correct extract and populate form values
     // test for collection -> fieldset -> field value
     foreach ($prices as $_k => $_price) {
         $this->assertEquals($_price, $form->get('collection')->get($_k)->get('product')->get('price')->getValue());
     }
     // test for collection -> fieldset -> fieldset ->field value
     foreach ($productCountries as $_k => $_countryName) {
         $this->assertEquals($_countryName, $form->get('collection')->get($_k)->get('product')->get('made_in_country')->get('name')->getValue());
     }
     // test collection -> fieldset -> collection -> fieldset -> field value
     foreach ($categoryNames as $_k => $_categoryName) {
         $this->assertEquals($_categoryName, $form->get('collection')->get($_k)->get('product')->get('categories')->get(0)->get('name')->getValue());
     }
 }