FormFactoryTest.php 15 KB

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