$t->ok($f->getValidator('name') == $v3, '->setValidator() sets a validator for a field'); // ->setWidgets() ->setWidgetSchema() ->getWidgetSchema() ->getWidget() ->setWidget() $t->diag('->setWidgets() ->setWidgetSchema() ->getWidgetSchema()'); $f = new FormTest(); $widgets = array('first_name' => new sfWidgetFormInputText(), 'last_name' => new sfWidgetFormInputText()); $widgetSchema = new sfWidgetFormSchema($widgets); $f->setWidgetSchema($widgetSchema); $t->ok($f->getWidgetSchema() == $widgetSchema, '->setWidgetSchema() sets the current widget schema'); $f->setWidgets($widgets); $schema = $f->getWidgetSchema(); $widgets['first_name']->setParent($schema); $widgets['last_name']->setParent($schema); $t->ok($schema['first_name'] == $widgets['first_name'], '->setWidgets() sets field widgets'); $t->ok($schema['last_name'] == $widgets['last_name'], '->setWidgets() sets field widgets'); $f->setWidget('name', $w3 = new sfWidgetFormInputText()); $w3->setParent($schema); $t->ok($f->getWidget('name') == $w3, '->setWidget() sets a widget for a field'); // ArrayAccess interface $t->diag('ArrayAccess interface'); $f = new FormTest(); $f->setWidgetSchema(new sfWidgetFormSchema(array('first_name' => new sfWidgetFormInputText(array('default' => 'Fabien')), 'last_name' => new sfWidgetFormInputText(), 'image' => new sfWidgetFormInputFile()))); $f->setValidatorSchema(new sfValidatorSchema(array('first_name' => new sfValidatorPass(), 'last_name' => new sfValidatorPass(), 'image' => new sfValidatorPass()))); $f->setDefaults(array('image' => 'default.gif')); $f->embedForm('embedded', new sfForm()); $t->ok($f['first_name'] instanceof sfFormField, '"sfForm" implements the ArrayAccess interface'); $t->is($f['first_name']->render(), '<input type="text" name="first_name" value="Fabien" id="first_name" />', '"sfForm" implements the ArrayAccess interface'); try { $f['image'] = 'image'; $t->fail('"sfForm" ArrayAccess implementation does not permit to set a form field'); } catch (LogicException $e) { $t->pass('"sfForm" ArrayAccess implementation does not permit to set a form field');
/* * This file is part of the symfony package. * (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new lime_test(31); // widgets $authorSchema = new sfWidgetFormSchema(array('id' => new sfWidgetFormInputHidden(), 'name' => $nameWidget = new sfWidgetFormInputText())); $authorSchema->setNameFormat('article[author][%s]'); $schema = new sfWidgetFormSchema(array('title' => $titleWidget = new sfWidgetFormInputText(), 'author' => $authorSchema)); $schema->setNameFormat('article[%s]'); $titleWidget->setParent($schema); // errors $authorErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'name error'), 'name'); $articleErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $articleErrorSchema->addError($titleError = new sfValidatorError(new sfValidatorString(), 'title error'), 'title'); $articleErrorSchema->addError($authorErrorSchema, 'author'); $parent = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); $f = $parent['title']; $child = $parent['author']; // ->getValue() ->getWidget() ->getParent() ->getError() ->hasError() $t->diag('->getValue() ->getName() ->getWidget() ->getParent() ->getError() ->hasError()'); $t->ok($f->getWidget() == $titleWidget, '->getWidget() returns the form field widget'); $t->is($f->getName(), 'title', '->getName() returns the form field name'); $t->is($f->getValue(), 'symfony', '->getValue() returns the form field value'); $t->is($f->getParent(), $parent, '->getParent() returns the form field parent');
* (c) Fabien Potencier <*****@*****.**> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ require_once dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new lime_test(94); $w1 = new sfWidgetFormInputText(array(), array('class' => 'foo1')); $w2 = new sfWidgetFormInputText(); // __construct() $t->diag('__construct()'); $w = new sfWidgetFormSchema(); $t->is($w->getFields(), array(), '__construct() can take no argument'); $w = new sfWidgetFormSchema(array('w1' => $w1, 'w2' => $w2)); $w1->setParent($w); $w2->setParent($w); $t->ok($w->getFields() == array('w1' => $w1, 'w2' => $w2), '__construct() can take an array of named sfWidget objects'); try { $w = new sfWidgetFormSchema('string'); $t->fail('__construct() throws a exception when passing a non supported first argument'); } catch (InvalidArgumentException $e) { $t->pass('__construct() throws an exception when passing a non supported first argument'); } $t->is($w->getFormFormatterName(), 'table', '__construct() sets "form_formatter" option to "table" by default'); $w = new sfWidgetFormSchema(array(), array('form_formatter' => 'list')); $t->is($w->getFormFormatterName(), 'list', '__construct() can override the default value for the "form_formatter" option'); $t->is($w->getNameFormat(), '%s', '__construct() sets "name_format" option to "table" by default'); $w = new sfWidgetFormSchema(array(), array('name_format' => 'name_%s')); $t->is($w->getNameFormat(), 'name_%s', '__construct() can override the default value for the "name_format" option'); // implements ArrayAccess $t->diag('implements ArrayAccess');
// ->render() $t->diag('->render()'); $output = <<<EOF <table> <tr> <th><label for="w1">W1</label></th> <td><input type="text" name="w1" id="w1" /></td> </tr> </table> EOF; $t->is($w->render(null), fix_linebreaks($output), '->render() decorates the widget'); // implements ArrayAccess $t->diag('implements ArrayAccess'); $w['w2'] = $w2; $w1->setParent($ws); $w2->setParent($ws); $t->ok($w->getFields() == array('w1' => $w1, 'w2' => $w2), 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); $t->ok($ws->getFields() == array('w1' => $w1, 'w2' => $w2), 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); try { $w['w1'] = 'string'; $t->fail('sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); } catch (LogicException $e) { $t->pass('sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); } $w = new sfWidgetFormSchemaDecorator($ws, "<table>\n%content%</table>"); $t->is(isset($w['w1']), true, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); $t->is(isset($w['w2']), true, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); $t->is(isset($ws['w1']), true, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); $t->is(isset($ws['w2']), true, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); $w = new sfWidgetFormSchemaDecorator($ws, "<table>\n%content%</table>"); $t->ok($w['w1'] == $w1, 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields');