FormFactoryTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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;
  11. use Symfony\Component\Form\FormBuilder;
  12. use Symfony\Component\Form\FormFactory;
  13. use Symfony\Component\Form\Guess\Guess;
  14. use Symfony\Component\Form\Guess\ValueGuess;
  15. use Symfony\Component\Form\Guess\TypeGuess;
  16. use Symfony\Tests\Component\Form\Fixtures\TestExtension;
  17. use Symfony\Tests\Component\Form\Fixtures\FooType;
  18. use Symfony\Tests\Component\Form\Fixtures\FooTypeBarExtension;
  19. use Symfony\Tests\Component\Form\Fixtures\FooTypeBazExtension;
  20. class FormFactoryTest extends \PHPUnit_Framework_TestCase
  21. {
  22. private $extension1;
  23. private $extension2;
  24. private $guesser1;
  25. private $guesser2;
  26. private $factory;
  27. protected function setUp()
  28. {
  29. $this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
  30. $this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
  31. $this->extension1 = new TestExtension($this->guesser1);
  32. $this->extension2 = new TestExtension($this->guesser2);
  33. $this->factory = new FormFactory(array($this->extension1, $this->extension2));
  34. }
  35. public function testAddType()
  36. {
  37. $this->assertFalse($this->factory->hasType('foo'));
  38. $type = new FooType();
  39. $this->factory->addType($type);
  40. $this->assertTrue($this->factory->hasType('foo'));
  41. $this->assertSame($type, $this->factory->getType('foo'));
  42. }
  43. public function testAddTypeAddsExtensions()
  44. {
  45. $type = new FooType();
  46. $ext1 = new FooTypeBarExtension();
  47. $ext2 = new FooTypeBazExtension();
  48. $this->extension1->addTypeExtension($ext1);
  49. $this->extension2->addTypeExtension($ext2);
  50. $this->factory->addType($type);
  51. $this->assertEquals(array($ext1, $ext2), $type->getExtensions());
  52. }
  53. public function testGetTypeFromExtension()
  54. {
  55. $type = new FooType();
  56. $this->extension2->addType($type);
  57. $this->assertSame($type, $this->factory->getType('foo'));
  58. }
  59. public function testGetTypeAddsExtensions()
  60. {
  61. $type = new FooType();
  62. $ext1 = new FooTypeBarExtension();
  63. $ext2 = new FooTypeBazExtension();
  64. $this->extension1->addTypeExtension($ext1);
  65. $this->extension2->addTypeExtension($ext2);
  66. $this->extension2->addType($type);
  67. $type = $this->factory->getType('foo');
  68. $this->assertEquals(array($ext1, $ext2), $type->getExtensions());
  69. }
  70. /**
  71. * @expectedException Symfony\Component\Form\Exception\FormException
  72. */
  73. public function testGetTypeExpectsExistingType()
  74. {
  75. $this->factory->getType('bar');
  76. }
  77. public function testCreateNamedBuilder()
  78. {
  79. $type = new FooType();
  80. $this->extension1->addType($type);
  81. $builder = $this->factory->createNamedBuilder('foo', 'bar');
  82. $this->assertTrue($builder instanceof FormBuilder);
  83. $this->assertEquals('bar', $builder->getName());
  84. }
  85. public function testCreateNamedBuilderCallsBuildFormMethods()
  86. {
  87. $type = new FooType();
  88. $ext1 = new FooTypeBarExtension();
  89. $ext2 = new FooTypeBazExtension();
  90. $this->extension1->addTypeExtension($ext1);
  91. $this->extension2->addTypeExtension($ext2);
  92. $this->extension2->addType($type);
  93. $builder = $this->factory->createNamedBuilder('foo', 'bar');
  94. $this->assertTrue($builder->hasAttribute('foo'));
  95. $this->assertTrue($builder->hasAttribute('bar'));
  96. $this->assertTrue($builder->hasAttribute('baz'));
  97. }
  98. public function testCreateNamedBuilderFillsDataOption()
  99. {
  100. $type = new FooType();
  101. $this->extension1->addType($type);
  102. $builder = $this->factory->createNamedBuilder('foo', 'bar', 'xyz');
  103. // see FooType::buildForm()
  104. $this->assertEquals('xyz', $builder->getAttribute('data_option'));
  105. }
  106. public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
  107. {
  108. $type = new FooType();
  109. $this->extension1->addType($type);
  110. $builder = $this->factory->createNamedBuilder('foo', 'bar', 'xyz', array(
  111. 'data' => 'abc',
  112. ));
  113. // see FooType::buildForm()
  114. $this->assertEquals('abc', $builder->getAttribute('data_option'));
  115. }
  116. /**
  117. * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
  118. */
  119. public function testCreateNamedBuilderExpectsDataOptionToBeSupported()
  120. {
  121. $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  122. $type->expects($this->any())
  123. ->method('getName')
  124. ->will($this->returnValue('foo'));
  125. $type->expects($this->any())
  126. ->method('getExtensions')
  127. ->will($this->returnValue(array()));
  128. $type->expects($this->any())
  129. ->method('getAllowedOptionValues')
  130. ->will($this->returnValue(array()));
  131. $type->expects($this->any())
  132. ->method('getDefaultOptions')
  133. ->will($this->returnValue(array(
  134. 'required' => false,
  135. 'max_length' => null,
  136. )));
  137. $this->extension1->addType($type);
  138. $this->factory->createNamedBuilder('foo', 'bar');
  139. }
  140. /**
  141. * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
  142. */
  143. public function testCreateNamedBuilderExpectsRequiredOptionToBeSupported()
  144. {
  145. $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  146. $type->expects($this->any())
  147. ->method('getName')
  148. ->will($this->returnValue('foo'));
  149. $type->expects($this->any())
  150. ->method('getExtensions')
  151. ->will($this->returnValue(array()));
  152. $type->expects($this->any())
  153. ->method('getAllowedOptionValues')
  154. ->will($this->returnValue(array()));
  155. $type->expects($this->any())
  156. ->method('getDefaultOptions')
  157. ->will($this->returnValue(array(
  158. 'data' => null,
  159. 'max_length' => null,
  160. )));
  161. $this->extension1->addType($type);
  162. $this->factory->createNamedBuilder('foo', 'bar');
  163. }
  164. /**
  165. * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
  166. */
  167. public function testCreateNamedBuilderExpectsMaxLengthOptionToBeSupported()
  168. {
  169. $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  170. $type->expects($this->any())
  171. ->method('getName')
  172. ->will($this->returnValue('foo'));
  173. $type->expects($this->any())
  174. ->method('getExtensions')
  175. ->will($this->returnValue(array()));
  176. $type->expects($this->any())
  177. ->method('getAllowedOptionValues')
  178. ->will($this->returnValue(array()));
  179. $type->expects($this->any())
  180. ->method('getDefaultOptions')
  181. ->will($this->returnValue(array(
  182. 'data' => null,
  183. 'required' => false,
  184. )));
  185. $this->extension1->addType($type);
  186. $this->factory->createNamedBuilder('foo', 'bar');
  187. }
  188. /**
  189. * @expectedException Symfony\Component\Form\Exception\TypeDefinitionException
  190. */
  191. public function testCreateNamedBuilderExpectsBuilderToBeReturned()
  192. {
  193. $type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
  194. $type->expects($this->any())
  195. ->method('getName')
  196. ->will($this->returnValue('foo'));
  197. $type->expects($this->any())
  198. ->method('getExtensions')
  199. ->will($this->returnValue(array()));
  200. $type->expects($this->any())
  201. ->method('getAllowedOptionValues')
  202. ->will($this->returnValue(array()));
  203. $type->expects($this->any())
  204. ->method('getDefaultOptions')
  205. ->will($this->returnValue(array(
  206. 'data' => null,
  207. 'required' => false,
  208. 'max_length' => null,
  209. )));
  210. $type->expects($this->any())
  211. ->method('createBuilder')
  212. ->will($this->returnValue(null));
  213. $this->extension1->addType($type);
  214. $this->factory->createNamedBuilder('foo', 'bar');
  215. }
  216. /**
  217. * @expectedException Symfony\Component\Form\Exception\CreationException
  218. */
  219. public function testCreateNamedBuilderExpectsOptionsToExist()
  220. {
  221. $type = new FooType();
  222. $this->extension1->addType($type);
  223. $this->factory->createNamedBuilder('foo', 'bar', null, array(
  224. 'invalid' => 'xyz',
  225. ));
  226. }
  227. /**
  228. * @expectedException Symfony\Component\Form\Exception\CreationException
  229. */
  230. public function testCreateNamedBuilderExpectsOptionsToBeInValidRange()
  231. {
  232. $type = new FooType();
  233. $this->extension1->addType($type);
  234. $this->factory->createNamedBuilder('foo', 'bar', null, array(
  235. 'a_or_b' => 'c',
  236. ));
  237. }
  238. public function testCreateNamedBuilderAllowsExtensionsToExtendAllowedOptionValues()
  239. {
  240. $type = new FooType();
  241. $this->extension1->addType($type);
  242. $this->extension1->addTypeExtension(new FooTypeBarExtension());
  243. // no exception this time
  244. $this->factory->createNamedBuilder('foo', 'bar', null, array(
  245. 'a_or_b' => 'c',
  246. ));
  247. }
  248. public function testCreateNamedBuilderAddsTypeInstances()
  249. {
  250. $type = new FooType();
  251. $this->assertFalse($this->factory->hasType('foo'));
  252. $builder = $this->factory->createNamedBuilder($type, 'bar');
  253. $this->assertTrue($builder instanceof FormBuilder);
  254. $this->assertTrue($this->factory->hasType('foo'));
  255. }
  256. public function testCreateUsesTypeNameAsName()
  257. {
  258. $type = new FooType();
  259. $this->extension1->addType($type);
  260. $builder = $this->factory->createBuilder('foo');
  261. $this->assertEquals('foo', $builder->getName());
  262. }
  263. public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
  264. {
  265. $this->guesser1->expects($this->once())
  266. ->method('guessType')
  267. ->with('Application\Author', 'firstName')
  268. ->will($this->returnValue(new TypeGuess(
  269. 'text',
  270. array('max_length' => 10),
  271. Guess::MEDIUM_CONFIDENCE
  272. )));
  273. $this->guesser2->expects($this->once())
  274. ->method('guessType')
  275. ->with('Application\Author', 'firstName')
  276. ->will($this->returnValue(new TypeGuess(
  277. 'password',
  278. array('max_length' => 7),
  279. Guess::HIGH_CONFIDENCE
  280. )));
  281. $factory = $this->createMockFactory(array('createNamedBuilder'));
  282. $factory->expects($this->once())
  283. ->method('createNamedBuilder')
  284. ->with('password', 'firstName', null, array('max_length' => 7))
  285. ->will($this->returnValue('builderInstance'));
  286. $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  287. $this->assertEquals('builderInstance', $builder);
  288. }
  289. public function testCreateBuilderCreatesTextFieldIfNoGuess()
  290. {
  291. $this->guesser1->expects($this->once())
  292. ->method('guessType')
  293. ->with('Application\Author', 'firstName')
  294. ->will($this->returnValue(null));
  295. $factory = $this->createMockFactory(array('createNamedBuilder'));
  296. $factory->expects($this->once())
  297. ->method('createNamedBuilder')
  298. ->with('text', 'firstName')
  299. ->will($this->returnValue('builderInstance'));
  300. $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  301. $this->assertEquals('builderInstance', $builder);
  302. }
  303. public function testOptionsCanBeOverridden()
  304. {
  305. $this->guesser1->expects($this->once())
  306. ->method('guessType')
  307. ->with('Application\Author', 'firstName')
  308. ->will($this->returnValue(new TypeGuess(
  309. 'text',
  310. array('max_length' => 10),
  311. Guess::MEDIUM_CONFIDENCE
  312. )));
  313. $factory = $this->createMockFactory(array('createNamedBuilder'));
  314. $factory->expects($this->once())
  315. ->method('createNamedBuilder')
  316. ->with('text', 'firstName', null, array('max_length' => 11))
  317. ->will($this->returnValue('builderInstance'));
  318. $builder = $factory->createBuilderForProperty(
  319. 'Application\Author',
  320. 'firstName',
  321. null,
  322. array('max_length' => 11)
  323. );
  324. $this->assertEquals('builderInstance', $builder);
  325. }
  326. public function testCreateBuilderUsesMaxLengthIfFound()
  327. {
  328. $this->guesser1->expects($this->once())
  329. ->method('guessMaxLength')
  330. ->with('Application\Author', 'firstName')
  331. ->will($this->returnValue(new ValueGuess(
  332. 15,
  333. Guess::MEDIUM_CONFIDENCE
  334. )));
  335. $this->guesser2->expects($this->once())
  336. ->method('guessMaxLength')
  337. ->with('Application\Author', 'firstName')
  338. ->will($this->returnValue(new ValueGuess(
  339. 20,
  340. Guess::HIGH_CONFIDENCE
  341. )));
  342. $factory = $this->createMockFactory(array('createNamedBuilder'));
  343. $factory->expects($this->once())
  344. ->method('createNamedBuilder')
  345. ->with('text', 'firstName', null, array('max_length' => 20))
  346. ->will($this->returnValue('builderInstance'));
  347. $builder = $factory->createBuilderForProperty(
  348. 'Application\Author',
  349. 'firstName'
  350. );
  351. $this->assertEquals('builderInstance', $builder);
  352. }
  353. public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
  354. {
  355. $this->guesser1->expects($this->once())
  356. ->method('guessRequired')
  357. ->with('Application\Author', 'firstName')
  358. ->will($this->returnValue(new ValueGuess(
  359. true,
  360. Guess::MEDIUM_CONFIDENCE
  361. )));
  362. $this->guesser2->expects($this->once())
  363. ->method('guessRequired')
  364. ->with('Application\Author', 'firstName')
  365. ->will($this->returnValue(new ValueGuess(
  366. false,
  367. Guess::HIGH_CONFIDENCE
  368. )));
  369. $factory = $this->createMockFactory(array('createNamedBuilder'));
  370. $factory->expects($this->once())
  371. ->method('createNamedBuilder')
  372. ->with('text', 'firstName', null, array('required' => false))
  373. ->will($this->returnValue('builderInstance'));
  374. $builder = $factory->createBuilderForProperty(
  375. 'Application\Author',
  376. 'firstName'
  377. );
  378. $this->assertEquals('builderInstance', $builder);
  379. }
  380. private function createMockFactory(array $methods = array())
  381. {
  382. return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  383. ->setMethods($methods)
  384. ->setConstructorArgs(array(array($this->extension1, $this->extension2)))
  385. ->getMock();
  386. }
  387. }