Development

Changeset 7051

You must first sign up to be able to contribute.

Changeset 7051

Show
Ignore:
Timestamp:
01/14/08 15:57:32 (10 months ago)
Author:
fabien
Message:

fixed cloning sfWidgetFormSchema, sfValidatorSchema and sfForm objects

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/form/sfForm.class.php

    r7047 r7051  
    735735    return $str; 
    736736  } 
     737 
     738  public function __clone() 
     739  { 
     740    $this->widgetSchema    = clone $this->widgetSchema; 
     741    $this->validatorSchema = clone $this->validatorSchema; 
     742 
     743    // we rebind the cloned form because Exceptions are not clonable 
     744    $this->bind($this->taintedValues, $this->taintedFiles); 
     745  } 
    737746} 
  • branches/1.1/lib/validator/sfValidatorSchema.class.php

    r6991 r7051  
    345345    throw new Exception('Unable to convert a sfValidatorSchema to string.'); 
    346346  } 
     347 
     348  public function __clone() 
     349  { 
     350    foreach ($this->fields as $name => $field) 
     351    { 
     352      $this->fields[$name] = clone $field; 
     353    } 
     354  } 
    347355} 
  • branches/1.1/lib/widget/sfWidgetFormSchema.class.php

    r6220 r7051  
    591591    } 
    592592  } 
     593 
     594  public function __clone() 
     595  { 
     596    foreach ($this->fields as $name => $field) 
     597    { 
     598      $this->fields[$name] = clone $field; 
     599    } 
     600  } 
    593601} 
  • branches/1.1/test/unit/form/sfFormTest.php

    r6969 r7051  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(76, new lime_output_color()); 
     13$t = new lime_test(82, new lime_output_color()); 
    1414 
    1515class FormTest extends sfForm 
     
    240240$d = $article->getDefaults(); 
    241241 
    242 $t->is($v['author']['first_name'], $author_validator_schema['first_name'], '->embedForm() embeds the validator schema'); 
    243 $t->is($w['author']['first_name'], $author_widget_schema['first_name'], '->embedForm() embeds the widget schema'); 
     242$t->ok($v['author']['first_name'] == $author_validator_schema['first_name'], '->embedForm() embeds the validator schema'); 
     243$t->ok($w['author']['first_name'] == $author_widget_schema['first_name'], '->embedForm() embeds the widget schema'); 
    244244$t->is($d['author']['first_name'], 'Fabien', '->embedForm() merges default values from the embedded form'); 
    245245$t->is($v['author'][sfForm::getCSRFFieldName()], null, '->embedForm() removes the CSRF token for the embedded form'); 
     
    255255for ($i = 0; $i < 2; $i++) 
    256256{ 
    257   $t->is($v['authors'][$i]['first_name'], $author_validator_schema['first_name'], '->embedFormForEach() embeds the validator schema'); 
    258   $t->is($w['authors'][$i]['first_name'], $author_widget_schema['first_name'], '->embedFormForEach() embeds the widget schema'); 
     257  $t->ok($v['authors'][$i]['first_name'] == $author_validator_schema['first_name'], '->embedFormForEach() embeds the validator schema'); 
     258  $t->ok($w['authors'][$i]['first_name'] == $author_widget_schema['first_name'], '->embedFormForEach() embeds the widget schema'); 
    259259  $t->is($d['authors'][$i]['first_name'], 'Fabien', '->embedFormForEach() merges default values from the embedded forms'); 
    260260  $t->is($v['authors'][$i][sfForm::getCSRFFieldName()], null, '->embedFormForEach() removes the CSRF token for the embedded forms'); 
     
    424424$t->is_deeply(sfForm::convertFileInformation($input), $expected, '::convertFileInformation() converts $_FILES to be coherent with $_GET and $_POST naming convention'); 
    425425$t->is_deeply(sfForm::convertFileInformation($expected), $expected, '::convertFileInformation() converts $_FILES to be coherent with $_GET and $_POST naming convention'); 
     426 
     427// __clone() 
     428$t->diag('__clone()'); 
     429$a = new FormTest(); 
     430$a->setValidatorSchema(new sfValidatorSchema(array( 
     431  'first_name' => new sfValidatorString(array('min_length' => 2)), 
     432))); 
     433$a->bind(array('first_name' => 'F')); 
     434$a1 = clone $a; 
     435 
     436$t->ok($a1->getValidatorSchema() !== $a->getValidatorSchema(), '__clone() clones the validator schema'); 
     437$t->ok($a1->getValidatorSchema() == $a->getValidatorSchema(), '__clone() clones the validator schema'); 
     438 
     439$t->ok($a1->getWidgetSchema() !== $a->getWidgetSchema(), '__clone() clones the widget schema'); 
     440$t->ok($a1->getWidgetSchema() == $a->getWidgetSchema(), '__clone() clones the widget schema'); 
     441 
     442$t->ok($a1->getErrorSchema() !== $a->getErrorSchema(), '__clone() clones the error schema'); 
     443$t->ok($a1->getErrorSchema()->getMessage() == $a->getErrorSchema()->getMessage(), '__clone() clones the error schema'); 
  • branches/1.1/test/unit/validator/sfValidatorSchemaTest.php

    r6963 r7051  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(73, new lime_output_color()); 
     13$t = new lime_test(78, new lime_output_color()); 
    1414 
    1515class PreValidator extends sfValidator 
     
    368368  $t->is($e->getCode(), 'invalid user [invalid password [invalid]] password [invalid]', '->clean() throws an exception with all error messages'); 
    369369} 
     370 
     371// __clone() 
     372$t->diag('__clone()'); 
     373$v = new sfValidatorSchema(array('v1' => $v1, 'v2' => $v2)); 
     374$v1 = clone $v; 
     375$f1 = $v1->getFields(); 
     376$f = $v->getFields(); 
     377$t->is(array_keys($f1), array_keys($f), '__clone() clones embedded validators'); 
     378foreach ($f1 as $name => $validator) 
     379{ 
     380  $t->ok($validator !== $f[$name], '__clone() clones embedded validators'); 
     381  $t->ok($validator == $f[$name], '__clone() clones embedded validators'); 
     382} 
  • branches/1.1/test/unit/widget/sfWidgetFormSchemaDecoratorTest.php

    r7049 r7051  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(16, new lime_output_color()); 
     13$t = new lime_test(17, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(); 
     
    7272$w1 = clone $w; 
    7373$t->ok($w1->getWidget() !== $w->getWidget(), '__clone() clones the embedded widget'); 
     74$t->ok($w1->getWidget() == $w->getWidget(), '__clone() clones the embedded widget'); 
  • branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php

    r6197 r7051  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(50, new lime_output_color()); 
     13$t = new lime_test(55, new lime_output_color()); 
    1414 
    1515$w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); 
     
    270270$rendered = $w->render(null, array('w1' => 'Fabien', 'w2' => 'Potencier'), array(), array('first_name' => 'Too short', 'Global error message', 'id' => 'Required')); 
    271271$t->is($rendered, $expected, '->render() renders a schema to HTML'); 
     272 
     273// __clone() 
     274$t->diag('__clone()'); 
     275$w = new sfWidgetFormSchema(array('w1' => $w1, 'w2' => $w2)); 
     276$w1 = clone $w; 
     277$f1 = $w1->getFields(); 
     278$f = $w->getFields(); 
     279$t->is(array_keys($f1), array_keys($f), '__clone() clones embedded widgets'); 
     280foreach ($f1 as $name => $widget) 
     281{ 
     282  $t->ok($widget !== $f[$name], '__clone() clones embedded widgets'); 
     283  $t->ok($widget == $f[$name], '__clone() clones embedded widgets'); 
     284}