// renderUsing() $t->diag('->renderUsing()'); $f = new sfForm(); $f->setWidgets(array('name' => new sfWidgetFormInputText())); $output = <<<EOF <li> <label for="name">Name</label> <input type="text" name="name" id="name" /> </li> EOF; $t->is($f->renderUsing('list'), fix_linebreaks($output), 'renderUsing() renders the widget schema using the given form formatter'); $t->is($f->getWidgetSchema()->getFormFormatterName(), 'table', 'renderUsing() does not persist form formatter name for the current form instance'); $w = $f->getWidgetSchema(); $w->addFormFormatter('custom', new sfWidgetFormSchemaFormatterList($w)); $t->is($f->renderUsing('custom'), fix_linebreaks($output), 'renderUsing() renders a custom form formatter'); try { $f->renderUsing('nonexistant'); $t->fail('renderUsing() throws an exception if formatter name does not exist'); } catch (InvalidArgumentException $e) { $t->pass('renderUsing() throws an exception if formatter name does not exist'); } // renderHiddenFields() $t->diag('->renderHiddenFields()'); $f = new sfForm(); $f->setWidgets(array('id' => new sfWidgetFormInputHidden(), 'name' => new sfWidgetFormInputText(), 'is_admin' => new sfWidgetFormInputHidden())); $output = '<input type="hidden" name="id" id="id" /><input type="hidden" name="is_admin" id="is_admin" />'; $t->is($f->renderHiddenFields(), $output, 'renderHiddenFields() renders all hidden fields, no visible fields'); $t->is(count($f->getFormFieldSchema()), 3, 'renderHiddenFields() does not modify the form fields'); $author = new sfForm(); $author->setWidgets(array('id' => new sfWidgetFormInputHidden(), 'name' => new sfWidgetFormInputText()));
$t = new lime_test(11); sfConfig::set('sf_charset', 'UTF-8'); // esc_entities() $t->diag('esc_entities()'); $t->is(esc_entities(10), 10, 'esc_entities() does not escape integers'); $t->is(esc_entities(false), false, 'esc_entities() does not escape booleans'); $t->is(esc_entities('foo bar'), 'foo bar', 'esc_entities() only escapes strings'); $t->is(esc_entities('<b>foo</b> bar'), '<b>foo</b> bar', 'esc_entities() only escapes strings'); // esc_raw() $t->diag('esc_raw()'); $t->is(esc_raw('foo'), 'foo', 'esc_raw() returns the first argument as is'); // esc_js() $t->diag('esc_js()'); $t->is(esc_js('alert(\'foo\' + "bar")'), 'alert('foo' + "bar")', 'esc_js() escapes javascripts'); // esc_js_no_entities() $t->diag('esc_js_no_entities()'); $t->is(esc_js_no_entities('alert(\'foo\' + "bar")'), 'alert(\\\'foo\\\' + \\"bar\\")', 'esc_js_no_entities() escapes javascripts'); $t->is(esc_js_no_entities('alert("hi\\there")'), 'alert(\\"hi\\\\there\\")', 'esc_js_no_entities() handles slashes correctly'); $t->is(esc_js_no_entities('alert("été")'), 'alert(\\"été\\")', 'esc_js_no_entities() preserves utf-8'); $output = <<<EOF alert('hello world') EOF; $t->is(esc_js_no_entities(fix_linebreaks($output)), 'alert(\\\'hello\\nworld\\\')', 'esc_js_no_entities() handles linebreaks correctly'); $t->is(esc_js_no_entities("alert('hello\nworld')"), 'alert(\\\'hello\\nworld\\\')', 'esc_js_no_entities() handles linebreaks correctly');
*/ require_once dirname(__FILE__) . '/../bootstrap.php'; use Bundle\sfFormBundle\Widget\InputText; use Bundle\sfFormBundle\Widget\Schema; use Bundle\sfFormBundle\Widget\SchemaFormatterTable; $t = new lime_test(2); $f = new SchemaFormatterTable(new Schema()); // ->formatRow() $t->diag('->formatRow()'); $output = <<<EOF <tr> <th>label</th> <td><input /><br />help</td> </tr> EOF; $t->is($f->formatRow('label', '<input />', array(), 'help', ''), fix_linebreaks($output), '->formatRow() formats a field in a row'); // ->formatErrorRow() $t->diag('->formatErrorRow()'); $output = <<<EOF <tr><td colspan="2"> <ul class="error_list"> <li>Global error</li> <li>id: required</li> <li>1 > sub_id: required</li> </ul> </td></tr> EOF; $t->is($f->formatErrorRow(array('Global error', 'id' => 'required', array('sub_id' => 'required'))), fix_linebreaks($output), '->formatErrorRow() formats an array of errors in a row');
$output = <<<EOF <ul class="radio_list"><li><input name="myname" type="radio" value="0" id="myname_0" /> <label for="myname_0">bar</label></li> <li><input name="myname" type="radio" value="1" id="myname_1" checked="checked" /> <label for="myname_1">foo</label></li></ul> EOF; $t->is($w->render('myname', true), fix_linebreaks($output), '->render() considers true to be an integer 1'); $w = new sfWidgetFormSelectRadio(array('choices' => array())); $t->is($w->render('myname', array()), '', '->render() returns an empty HTML string if no choices'); // group support $t->diag('group support'); $w = new sfWidgetFormSelectRadio(array('choices' => array('foo' => array('foo' => 'bar', 'bar' => 'foo'), 'bar' => array('foobar' => 'barfoo')))); $output = <<<EOF foo <ul class="radio_list"><li><input name="foo" type="radio" value="foo" id="foo_foo" checked="checked" /> <label for="foo_foo">bar</label></li> <li><input name="foo" type="radio" value="bar" id="foo_bar" /> <label for="foo_bar">foo</label></li></ul> bar <ul class="radio_list"><li><input name="foo" type="radio" value="foobar" id="foo_foobar" /> <label for="foo_foobar">barfoo</label></li></ul> EOF; $t->is($w->render('foo', 'foo'), fix_linebreaks($output), '->render() has support for groups'); try { $w = new sfWidgetFormSelectRadio(); $t->fail('__construct() throws an RuntimeException if you don\'t pass a choices option'); } catch (RuntimeException $e) { $t->pass('__construct() throws an RuntimeException if you don\'t pass a choices option'); } // choices as a callable $t->diag('choices as a callable'); function choice_callable() { return array(1, 2, 3); } $w = new sfWidgetFormSelectRadio(array('choices' => new sfCallable('choice_callable'))); $dom->loadHTML($w->render('foo')); $css = new sfDomCssSelector($dom);
EOF; $t->is($child['name']->renderError(), fix_linebreaks($output), '->renderRow() renders errors as HTML when the widget has a parent'); try { $parent->renderError(); $t->fail('->renderError() throws an LogicException if the form field has no parent'); } catch (LogicException $e) { $t->pass('->renderError() throws an LogicException if the form field has no parent'); } // global errors $authorErrorSchema = new sfValidatorErrorSchema(new sfValidatorString()); $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'name error'), 'name'); $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'non existent field error'), 'non_existent_field'); $authorErrorSchema->addError(new sfValidatorError(new sfValidatorString(), 'hidden field error'), 'id'); $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); $child = $parent['author']; $output = <<<EOF <ul class="error_list"> <li>non existent field error</li> <li>Id: hidden field error</li> </ul> EOF; $t->is($child->renderError(), fix_linebreaks($output), '->renderError() renders global errors as expected (global errors, hidden field errors, non existent field errors)'); // id format $schema->setIdFormat('%s_id_format_test'); $parent = new sfFormFieldSchema($schema, null, 'article', array('title' => 'symfony', 'author' => array('name' => 'Fabien')), $articleErrorSchema); $t->like($parent['author']->render(), '/_id_format_test/', '->render() uses the parent id format');
EOF; $t->is($e->extract($content), array('foo', 'bar', 'foobar', 'foo %a% bar'), '->extract() extracts strings from \'\' and "" quoted strings'); $content = <<<EOF <?php echo __ ( 'foo' ); echo __ ( 'bar' ); ?> EOF; $t->is($e->extract($content), array('foo', 'bar'), '->extract() does not care if you add some whitespaces'); $content = <<<EOF <?php echo __(<<<EOD foo EOD ); echo __(<<<EOD bar EOD ); EOF; $t->is(fix_linebreaks($e->extract($content)), array("foo\n", "bar\n"), '->extract() extracts strings from HEREDOC quoted strings');
$expected = <<<EOF <tr> <th>W4</th> <td> <table> <tr> <th><label for="w4_w3">W3</label></th> <td><input type="text" name="w4[w3]" id="w4_w3" /></td> </tr> </table> <input type="hidden" name="w1" id="w1" /> </td> </tr> EOF; $t->is(str_replace("\n", '', preg_replace('/^ +/m', '', $w->render(null))), str_replace("\n", '', preg_replace('/^ +/m', '', fix_linebreaks($expected))), '->render() is able to render widget schema that only contains hidden fields when the last field is a form'); // __clone() $t->diag('__clone()'); $w = new sfWidgetFormSchema(array('w1' => $w1, 'w2' => $w2)); $w1->setParent($w); $w2->setParent($w); $format1 = new sfWidgetFormSchemaFormatterList($w); $format1->setTranslationCatalogue('english'); $w->addFormFormatter('testFormatter', $format1); $w1 = clone $w; $f1 = $w1->getFields(); $f = $w->getFields(); $t->is(array_keys($f1), array_keys($f), '__clone() clones embedded widgets'); foreach ($f1 as $name => $widget) { $t->ok($widget !== $f[$name], '__clone() clones embedded widgets'); $t->ok($widget->getParent() === $w1, 'The parents hafe been changed');
$t->is($w->render('myname', array()), '', '->render() returns an empty HTML string if no choices'); // group support $t->diag('group support'); $w = new sfWidgetFormSelectCheckbox(array('choices' => array('foo' => array('foo' => 'bar', 'bar' => 'foo'), 'bar' => array('foobar' => 'barfoo')))); $output = <<<EOF foo <ul class="checkbox_list"><li><input name="foo[]" type="checkbox" value="foo" id="foo_foo" checked="checked" /> <label for="foo_foo">bar</label></li> <li><input name="foo[]" type="checkbox" value="bar" id="foo_bar" /> <label for="foo_bar">foo</label></li></ul> bar <ul class="checkbox_list"><li><input name="foo[]" type="checkbox" value="foobar" id="foo_foobar" checked="checked" /> <label for="foo_foobar">barfoo</label></li></ul> EOF; $t->is($w->render('foo', array('foo', 'foobar')), fix_linebreaks($output), '->render() has support for groups'); $w->setOption('choices', array('foo' => array('foo' => 'bar', 'bar' => 'foo'))); $output = <<<EOF foo <ul class="checkbox_list"><li><input name="foo[]" type="checkbox" value="foo" id="foo_foo" /> <label for="foo_foo">bar</label></li> <li><input name="foo[]" type="checkbox" value="bar" id="foo_bar" checked="checked" /> <label for="foo_bar">foo</label></li></ul> EOF; $t->is($w->render('foo', array('bar')), fix_linebreaks($output), '->render() accepts a single group'); try { $w = new sfWidgetFormSelectCheckbox(); $t->fail('__construct() throws an RuntimeException if you don\'t pass a choices option'); } catch (RuntimeException $e) { $t->pass('__construct() throws an RuntimeException if you don\'t pass a choices option'); } // choices as a callable $t->diag('choices as a callable'); function choice_callable() { return array(1, 2, 3); } $w = new sfWidgetFormSelectCheckbox(array('choices' => new sfCallable('choice_callable'))); $dom->loadHTML($w->render('foo')); $css = new sfDomCssSelector($dom);
// get_javascripts_for_form() get_stylesheets_for_form() $t->diag('get_javascripts_for_form() get_stylesheets_for_form()'); $form = new MyForm(); $output = <<<EOF <script type="text/javascript" src="/path/to/a/foo.js"></script> <script type="text/javascript" src="/path/to/a/bar.js"></script> EOF; $t->is(get_javascripts_for_form($form), fix_linebreaks($output), 'get_javascripts_for_form() returns script tags'); $output = <<<EOF <link rel="stylesheet" type="text/css" media="all" href="/path/to/a/foo.css" /> <link rel="stylesheet" type="text/css" media="print" href="/path/to/a/bar.css" /> EOF; $t->is(get_stylesheets_for_form($form), fix_linebreaks($output), 'get_stylesheets_for_form() returns link tags'); // use_javascripts_for_form() use_stylesheets_for_form() $t->diag('use_javascripts_for_form() use_stylesheets_for_form()'); $response = sfContext::getInstance()->getResponse(); $form = new MyForm(); $response->resetAssets(); use_stylesheets_for_form($form); $t->is_deeply($response->getStylesheets(), array('/path/to/a/foo.css' => array('media' => 'all'), '/path/to/a/bar.css' => array('media' => 'print')), 'use_stylesheets_for_form() adds stylesheets to the response'); $response->resetAssets(); use_javascripts_for_form($form); $t->is_deeply($response->getJavaScripts(), array('/path/to/a/foo.js' => array(), '/path/to/a/bar.js' => array()), 'use_javascripts_for_form() adds javascripts to the response');
<?php /* * 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(1); $dispatcher = new sfEventDispatcher(); $buffer = fopen('php://memory', 'rw'); $logger = new sfStreamLogger($dispatcher, array('stream' => $buffer)); $logger->log('foo'); rewind($buffer); $t->is(fix_linebreaks(stream_get_contents($buffer)), "foo\n", 'sfStreamLogger logs messages to a PHP stream');
<?php /* * This file is part of the symfony package. * (c) 2004-2006 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(1); $logger = new sfConsoleLogger(new sfEventDispatcher()); $logger->setStream($buffer = fopen('php://memory', 'rw')); $logger->log('foo'); rewind($buffer); $t->is(fix_linebreaks(stream_get_contents($buffer)), "foo\n", 'sfConsoleLogger logs messages to the console');
$ws = new sfWidgetFormSchema(array('w1' => $w1)); $w = new sfWidgetFormSchemaDecorator($ws, "<table>\n%content%</table>"); // ->getWidget() $t->diag('->getWidget()'); $t->is($w->getWidget(), $ws, '->getWidget() returns the decorated widget'); // ->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; $t->is($w->getFields(), array('w1' => $w1, 'w2' => $w2), 'sfWidgetFormSchemaDecorator implements the ArrayAccess interface for the fields'); $t->is($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');
\$response->addJavascript('all_foo', '', array ()); \$response->addJavascript('foobar', '', array ()); EOF; $t->is(fix_linebreaks($handler->addHtmlAsset('myView')), fix_linebreaks($content), 'addHtmlAsset() supports the - option to remove one javascript previously added'); $handler->setConfiguration(array('all' => array('stylesheets' => array('foo', 'bar', '-*', 'baz')))); $content = <<<EOF \$response->addStylesheet('baz', '', array ()); EOF; $t->is(fix_linebreaks($handler->addHtmlAsset('myView')), fix_linebreaks($content), 'addHtmlAsset() supports the -* option to remove all stylesheets previously added'); $handler->setConfiguration(array('all' => array('javascripts' => array('foo', 'bar', '-*', 'baz')))); $content = <<<EOF \$response->addJavascript('baz', '', array ()); EOF; $t->is(fix_linebreaks($handler->addHtmlAsset('myView')), fix_linebreaks($content), 'addHtmlAsset() supports the -* option to remove all javascripts previously added'); $handler->setConfiguration(array('all' => array('stylesheets' => array('-*', 'foobar')), 'default' => array('stylesheets' => array('default_foo', 'default_bar')))); $content = <<<EOF \$response->addStylesheet('foobar', '', array ()); EOF; $t->is(fix_linebreaks($handler->addHtmlAsset('myView')), fix_linebreaks($content), 'addHtmlAsset() supports the -* option to remove all assets previously added'); $handler->setConfiguration(array('myView' => array('stylesheets' => array('foobar', '-*', 'bar'), 'javascripts' => array('foobar', '-*', 'bar')), 'all' => array('stylesheets' => array('all_foo', 'all_foofoo', 'all_barbar'), 'javascripts' => array('all_foo', 'all_foofoo', 'all_barbar')), 'default' => array('stylesheets' => array('default_foo', 'default_foofoo', 'default_barbar'), 'javascripts' => array('default_foo', 'default_foofoo', 'default_barbar')))); $content = <<<EOF \$response->addStylesheet('bar', '', array ()); \$response->addJavascript('bar', '', array ()); EOF; $t->is(fix_linebreaks($handler->addHtmlAsset('myView')), fix_linebreaks($content), 'addHtmlAsset() supports the -* option to remove all assets previously added');
$t->diag('->setFile() ->getFile()'); $m = new sfClassManipulator($source); $m->setFile('foo'); $t->is($m->getFile(), 'foo', '->setFile() sets the name of the file associated with the source code'); // ::fromFile() $t->diag('::fromFile()'); $file = sys_get_temp_dir() . '/sf_tmp.php'; file_put_contents($file, $source); $m = sfClassManipulator::fromFile($file); $t->is($m->getFile(), $file, '::fromFile() sets the file internally'); // ->save() $t->diag('->save()'); $m = sfClassManipulator::fromFile($file); $m->wrapMethod('foo', '', '// code after'); $m->save(); $t->is(fix_linebreaks(file_get_contents($file)), fix_linebreaks($sourceWithCodeAfter), '->save() saves the modified code if a file is associated with the instance'); unlink($file); // ->filterMethod() $t->diag('->filterMethod()'); class MethodFilterer { public $lines = array(); public function filter1($line) { $this->lines[] = $line; return $line; } public function filter2($line) { return str_replace(array('if (true)', 'function foo()'), array('if (false)', 'function foo($arg)'), $line); }