AbstractThemeFunctionalTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form\Renderer\Theme;
  11. use Symfony\Component\Form\Type\AbstractType;
  12. use Symfony\Component\Form\FormBuilder;
  13. use Symfony\Component\Form\CsrfProvider\DefaultCsrfProvider;
  14. use Symfony\Component\Form\Type\Loader\DefaultTypeLoader;
  15. use Symfony\Component\Form\FormFactory;
  16. /**
  17. * Test theme template files shipped with framework bundle.
  18. */
  19. abstract class AbstractThemeFunctionalTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /** @var FormFactory */
  22. private $factory;
  23. abstract protected function createThemeFactory();
  24. protected function getTemplate()
  25. {
  26. }
  27. public function setUp()
  28. {
  29. $themeFactory = $this->createThemeFactory();
  30. $template = $this->getTemplate();
  31. $csrfProvider = new DefaultCsrfProvider('foo');
  32. $validator = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
  33. $storage = new \Symfony\Component\HttpFoundation\File\TemporaryStorage('foo', 1, \sys_get_temp_dir());
  34. // ok more than 2 lines, see DefaultFormFactory.php for proposed simplication
  35. $typeLoader = new DefaultTypeLoader();
  36. $this->factory = new FormFactory($typeLoader);
  37. $typeLoader->initialize($this->factory, $themeFactory, $template, $csrfProvider, $validator , $storage);
  38. // this is the relevant bit about your own forms:
  39. $typeLoader->addType(new MyTestFormConfig());
  40. $typeLoader->addType(new MyTestSubFormConfig());
  41. }
  42. public function testFullFormRendering()
  43. {
  44. \Locale::setDefault('en');
  45. $form = $this->factory->create('my.form');
  46. $html = $form->getRenderer()->getWidget();
  47. libxml_use_internal_errors(true);
  48. $dom = new \DomDocument('UTF-8');
  49. $dom->loadHtml($html);
  50. $xpath = new \DomXpath($dom);
  51. $ids = array();
  52. foreach ($xpath->evaluate('//*[@id]') as $node) {
  53. $ids[] = $node->tagName . "#" . $node->getAttribute('id');
  54. }
  55. libxml_use_internal_errors(false);
  56. $this->assertEquals(array (
  57. 'input#form_field0_subfield0',
  58. 'input#form_field1',
  59. 'select#form_field2_month',
  60. 'select#form_field2_day',
  61. 'select#form_field2_year',
  62. 'select#form_field5_hour',
  63. 'select#form_field5_minute',
  64. 'input#form_field3_active',
  65. 'input#form_field3_inactive',
  66. 'select#form_field21',
  67. 'select#form_field22',
  68. 'select#form_field4_date_month',
  69. 'select#form_field4_date_day',
  70. 'select#form_field4_date_year',
  71. 'select#form_field4_time_hour',
  72. 'select#form_field4_time_minute',
  73. 'select#form_field6_month',
  74. 'select#form_field6_day',
  75. 'select#form_field6_year',
  76. 'input#form_field7',
  77. 'input#form_field8_file',
  78. 'input#form_field8_token',
  79. 'input#form_field8_name',
  80. 'input#form_field10',
  81. 'select#form_field11',
  82. 'select#form_field12',
  83. 'input#form_field13',
  84. 'input#form_field14',
  85. 'input#form_field15',
  86. 'input#form_field16',
  87. 'input#form_field17',
  88. 'input#form_field18_first',
  89. 'input#form_field18_second',
  90. 'select#form_field19',
  91. 'input#form_field20',
  92. ), $ids);
  93. }
  94. }
  95. class MyTestFormConfig extends AbstractType
  96. {
  97. public function configure(FormBuilder $builder, array $options)
  98. {
  99. $builder->setDataClass('Symfony\Bundle\FrameworkBundle\Tests\Form\MyTestObject');
  100. $builder->add('field0', 'my.sub_form');
  101. $builder->add('field1', 'text', array('max_length' => 127, 'id' => 'foo'));
  102. $builder->add('field2', 'date');
  103. $builder->add('field5', 'time');
  104. $builder->add('field3', 'choice', array(
  105. 'expanded' => true,
  106. 'multiple' => false,
  107. 'choices' => array('active' => 'Active', 'inactive' => 'Inactive'),
  108. ));
  109. $builder->add('field21', 'choice', array(
  110. 'expanded' => false,
  111. 'multiple' => false,
  112. 'choices' => array('active' => 'Active', 'inactive' => 'Inactive'),
  113. ));
  114. $builder->add('field22', 'choice', array(
  115. 'expanded' => false,
  116. 'multiple' => false,
  117. 'choices' => array('active' => 'Active', 'inactive' => 'Inactive'),
  118. 'preferred_choices' => array('active')
  119. ));
  120. $builder->add('field4', 'datetime');
  121. $builder->add('field6', 'birthday');
  122. $builder->add('field7', 'checkbox');
  123. $builder->add('field8', 'file');
  124. $builder->add('field9', 'hidden');
  125. $builder->add('field10', 'integer');
  126. $builder->add('field11', 'language');
  127. $builder->add('field12', 'locale');
  128. $builder->add('field13', 'money');
  129. $builder->add('field14', 'number');
  130. $builder->add('field15', 'password');
  131. $builder->add('field16', 'percent');
  132. $builder->add('field17', 'radio');
  133. $builder->add('field18', 'repeated', array('identifier' => 'password'));
  134. $builder->add('emails', 'collection', array(
  135. 'type' => 'text',
  136. ));
  137. $builder->add('field19', 'timezone');
  138. $builder->add('field20', 'url');
  139. }
  140. public function getName()
  141. {
  142. return 'my.form';
  143. }
  144. public function getParent(array $options) {
  145. return 'form';
  146. }
  147. }
  148. class MyTestObject
  149. {
  150. private $emails = 'test, foo, bar';
  151. }
  152. class MyTestSubFormConfig extends AbstractType
  153. {
  154. public function configure(FormBuilder $builder, array $options)
  155. {
  156. $builder->add('subfield0', 'text');
  157. }
  158. public function getName()
  159. {
  160. return 'my.sub_form';
  161. }
  162. public function getParent(array $options) {
  163. return 'form';
  164. }
  165. }