예제 #1
0
파일: FormTest.php 프로젝트: haoyanfei/zf2
 public function getOneToManyEntity()
 {
     $product = new TestAsset\Entity\Product();
     $product->setName('Chair');
     $product->setPrice(10);
     $firstCategory = new TestAsset\Entity\Category();
     $firstCategory->setName('Office');
     $secondCategory = new TestAsset\Entity\Category();
     $secondCategory->setName('Armchair');
     $product->setCategories(array($firstCategory, $secondCategory));
     return $product;
 }
예제 #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);
 }
예제 #3
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);
    }
예제 #4
0
 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);
 }
예제 #5
0
 public function testDoNotCreateExtraFieldsetOnMultipleBind()
 {
     $form = new \Zend\Form\Form();
     $this->productFieldset->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods());
     $form->add($this->productFieldset);
     $form->setHydrator(new \Zend\Stdlib\Hydrator\ObjectProperty());
     $product = new Product();
     $categories = array(new \ZendTest\Form\TestAsset\Entity\Category(), new \ZendTest\Form\TestAsset\Entity\Category());
     $product->setCategories($categories);
     $market = new \StdClass();
     $market->product = $product;
     // this will pass the test
     $form->bind($market);
     $this->assertSame(count($categories), iterator_count($form->get('product')->get('categories')->getIterator()));
     // this won't pass, but must
     $form->bind($market);
     $this->assertSame(count($categories), iterator_count($form->get('product')->get('categories')->getIterator()));
 }