FormFactoryTest.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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\Component\Form\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\FormFactory;
  13. use Symfony\Component\Form\FormTypeGuesserChain;
  14. use Symfony\Component\Form\Guess\Guess;
  15. use Symfony\Component\Form\Guess\TypeGuess;
  16. use Symfony\Component\Form\Guess\ValueGuess;
  17. use Symfony\Component\Form\Tests\Fixtures\LegacyFooSubType;
  18. use Symfony\Component\Form\Tests\Fixtures\LegacyFooSubTypeWithParentInstance;
  19. use Symfony\Component\Form\Tests\Fixtures\LegacyFooType;
  20. /**
  21. * @author Bernhard Schussek <bschussek@gmail.com>
  22. */
  23. class FormFactoryTest extends TestCase
  24. {
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $guesser1;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $guesser2;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject
  35. */
  36. private $registry;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject
  39. */
  40. private $resolvedTypeFactory;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private $builder;
  45. /**
  46. * @var FormFactory
  47. */
  48. private $factory;
  49. protected function setUp()
  50. {
  51. $this->resolvedTypeFactory = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeFactoryInterface')->getMock();
  52. $this->guesser1 = $this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock();
  53. $this->guesser2 = $this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock();
  54. $this->registry = $this->getMockBuilder('Symfony\Component\Form\FormRegistryInterface')->getMock();
  55. $this->builder = $this->getMockBuilder('Symfony\Component\Form\Test\FormBuilderInterface')->getMock();
  56. $this->factory = new FormFactory($this->registry, $this->resolvedTypeFactory);
  57. $this->registry->expects($this->any())
  58. ->method('getTypeGuesser')
  59. ->will($this->returnValue(new FormTypeGuesserChain(array(
  60. $this->guesser1,
  61. $this->guesser2,
  62. ))));
  63. }
  64. public function testCreateNamedBuilderWithTypeName()
  65. {
  66. $options = array('a' => '1', 'b' => '2');
  67. $resolvedOptions = array('a' => '2', 'b' => '3');
  68. $resolvedType = $this->getMockResolvedType();
  69. $this->registry->expects($this->once())
  70. ->method('getType')
  71. ->with('type')
  72. ->will($this->returnValue($resolvedType));
  73. $resolvedType->expects($this->once())
  74. ->method('createBuilder')
  75. ->with($this->factory, 'name', $options)
  76. ->will($this->returnValue($this->builder));
  77. $this->builder->expects($this->any())
  78. ->method('getOptions')
  79. ->will($this->returnValue($resolvedOptions));
  80. $resolvedType->expects($this->once())
  81. ->method('buildForm')
  82. ->with($this->builder, $resolvedOptions);
  83. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', 'type', null, $options));
  84. }
  85. /**
  86. * @group legacy
  87. */
  88. public function testCreateNamedBuilderWithTypeInstance()
  89. {
  90. $options = array('a' => '1', 'b' => '2');
  91. $resolvedOptions = array('a' => '2', 'b' => '3');
  92. $type = new LegacyFooType();
  93. $resolvedType = $this->getMockResolvedType();
  94. $this->resolvedTypeFactory->expects($this->once())
  95. ->method('createResolvedType')
  96. ->with($type)
  97. ->will($this->returnValue($resolvedType));
  98. $resolvedType->expects($this->once())
  99. ->method('createBuilder')
  100. ->with($this->factory, 'name', $options)
  101. ->will($this->returnValue($this->builder));
  102. $this->builder->expects($this->any())
  103. ->method('getOptions')
  104. ->will($this->returnValue($resolvedOptions));
  105. $resolvedType->expects($this->once())
  106. ->method('buildForm')
  107. ->with($this->builder, $resolvedOptions);
  108. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', $type, null, $options));
  109. }
  110. /**
  111. * @group legacy
  112. */
  113. public function testCreateNamedBuilderWithTypeInstanceWithParentType()
  114. {
  115. $options = array('a' => '1', 'b' => '2');
  116. $resolvedOptions = array('a' => '2', 'b' => '3');
  117. $type = new LegacyFooSubType();
  118. $resolvedType = $this->getMockResolvedType();
  119. $parentResolvedType = $this->getMockResolvedType();
  120. $this->registry->expects($this->once())
  121. ->method('getType')
  122. ->with('foo')
  123. ->will($this->returnValue($parentResolvedType));
  124. $this->resolvedTypeFactory->expects($this->once())
  125. ->method('createResolvedType')
  126. ->with($type, array(), $parentResolvedType)
  127. ->will($this->returnValue($resolvedType));
  128. $resolvedType->expects($this->once())
  129. ->method('createBuilder')
  130. ->with($this->factory, 'name', $options)
  131. ->will($this->returnValue($this->builder));
  132. $this->builder->expects($this->any())
  133. ->method('getOptions')
  134. ->will($this->returnValue($resolvedOptions));
  135. $resolvedType->expects($this->once())
  136. ->method('buildForm')
  137. ->with($this->builder, $resolvedOptions);
  138. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', $type, null, $options));
  139. }
  140. /**
  141. * @group legacy
  142. */
  143. public function testCreateNamedBuilderWithTypeInstanceWithParentTypeInstance()
  144. {
  145. $options = array('a' => '1', 'b' => '2');
  146. $resolvedOptions = array('a' => '2', 'b' => '3');
  147. $type = new LegacyFooSubTypeWithParentInstance();
  148. $resolvedType = $this->getMockResolvedType();
  149. $parentResolvedType = $this->getMockResolvedType();
  150. $this->resolvedTypeFactory->expects($this->at(0))
  151. ->method('createResolvedType')
  152. ->with($type->getParent())
  153. ->will($this->returnValue($parentResolvedType));
  154. $this->resolvedTypeFactory->expects($this->at(1))
  155. ->method('createResolvedType')
  156. ->with($type, array(), $parentResolvedType)
  157. ->will($this->returnValue($resolvedType));
  158. $resolvedType->expects($this->once())
  159. ->method('createBuilder')
  160. ->with($this->factory, 'name', $options)
  161. ->will($this->returnValue($this->builder));
  162. $this->builder->expects($this->any())
  163. ->method('getOptions')
  164. ->will($this->returnValue($resolvedOptions));
  165. $resolvedType->expects($this->once())
  166. ->method('buildForm')
  167. ->with($this->builder, $resolvedOptions);
  168. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', $type, null, $options));
  169. }
  170. /**
  171. * @group legacy
  172. */
  173. public function testCreateNamedBuilderWithResolvedTypeInstance()
  174. {
  175. $options = array('a' => '1', 'b' => '2');
  176. $resolvedOptions = array('a' => '2', 'b' => '3');
  177. $resolvedType = $this->getMockResolvedType();
  178. $resolvedType->expects($this->once())
  179. ->method('createBuilder')
  180. ->with($this->factory, 'name', $options)
  181. ->will($this->returnValue($this->builder));
  182. $this->builder->expects($this->any())
  183. ->method('getOptions')
  184. ->will($this->returnValue($resolvedOptions));
  185. $resolvedType->expects($this->once())
  186. ->method('buildForm')
  187. ->with($this->builder, $resolvedOptions);
  188. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', $resolvedType, null, $options));
  189. }
  190. public function testCreateNamedBuilderFillsDataOption()
  191. {
  192. $givenOptions = array('a' => '1', 'b' => '2');
  193. $expectedOptions = array_merge($givenOptions, array('data' => 'DATA'));
  194. $resolvedOptions = array('a' => '2', 'b' => '3', 'data' => 'DATA');
  195. $resolvedType = $this->getMockResolvedType();
  196. $this->registry->expects($this->once())
  197. ->method('getType')
  198. ->with('type')
  199. ->will($this->returnValue($resolvedType));
  200. $resolvedType->expects($this->once())
  201. ->method('createBuilder')
  202. ->with($this->factory, 'name', $expectedOptions)
  203. ->will($this->returnValue($this->builder));
  204. $this->builder->expects($this->any())
  205. ->method('getOptions')
  206. ->will($this->returnValue($resolvedOptions));
  207. $resolvedType->expects($this->once())
  208. ->method('buildForm')
  209. ->with($this->builder, $resolvedOptions);
  210. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', 'type', 'DATA', $givenOptions));
  211. }
  212. public function testCreateNamedBuilderDoesNotOverrideExistingDataOption()
  213. {
  214. $options = array('a' => '1', 'b' => '2', 'data' => 'CUSTOM');
  215. $resolvedOptions = array('a' => '2', 'b' => '3', 'data' => 'CUSTOM');
  216. $resolvedType = $this->getMockResolvedType();
  217. $this->registry->expects($this->once())
  218. ->method('getType')
  219. ->with('type')
  220. ->will($this->returnValue($resolvedType));
  221. $resolvedType->expects($this->once())
  222. ->method('createBuilder')
  223. ->with($this->factory, 'name', $options)
  224. ->will($this->returnValue($this->builder));
  225. $this->builder->expects($this->any())
  226. ->method('getOptions')
  227. ->will($this->returnValue($resolvedOptions));
  228. $resolvedType->expects($this->once())
  229. ->method('buildForm')
  230. ->with($this->builder, $resolvedOptions);
  231. $this->assertSame($this->builder, $this->factory->createNamedBuilder('name', 'type', 'DATA', $options));
  232. }
  233. /**
  234. * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
  235. * @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
  236. */
  237. public function testCreateNamedBuilderThrowsUnderstandableException()
  238. {
  239. $this->factory->createNamedBuilder('name', new \stdClass());
  240. }
  241. /**
  242. * @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
  243. * @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
  244. */
  245. public function testCreateThrowsUnderstandableException()
  246. {
  247. $this->factory->create(new \stdClass());
  248. }
  249. public function testCreateUsesBlockPrefixIfTypeGivenAsString()
  250. {
  251. $options = array('a' => '1', 'b' => '2');
  252. $resolvedOptions = array('a' => '2', 'b' => '3');
  253. // the interface does not have the method, so use the real class
  254. $resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
  255. ->disableOriginalConstructor()
  256. ->getMock();
  257. $resolvedType->expects($this->any())
  258. ->method('getBlockPrefix')
  259. ->willReturn('TYPE_PREFIX');
  260. $this->registry->expects($this->any())
  261. ->method('getType')
  262. ->with('TYPE')
  263. ->will($this->returnValue($resolvedType));
  264. $resolvedType->expects($this->once())
  265. ->method('createBuilder')
  266. ->with($this->factory, 'TYPE_PREFIX', $options)
  267. ->will($this->returnValue($this->builder));
  268. $this->builder->expects($this->any())
  269. ->method('getOptions')
  270. ->will($this->returnValue($resolvedOptions));
  271. $resolvedType->expects($this->once())
  272. ->method('buildForm')
  273. ->with($this->builder, $resolvedOptions);
  274. $this->builder->expects($this->once())
  275. ->method('getForm')
  276. ->will($this->returnValue('FORM'));
  277. $this->assertSame('FORM', $this->factory->create('TYPE', null, $options));
  278. }
  279. /**
  280. * @group legacy
  281. */
  282. public function testCreateUsesTypeNameIfTypeGivenAsString()
  283. {
  284. $options = array('a' => '1', 'b' => '2');
  285. $resolvedOptions = array('a' => '2', 'b' => '3');
  286. $resolvedType = $this->getMockResolvedType();
  287. $this->registry->expects($this->any())
  288. ->method('getType')
  289. ->with('TYPE')
  290. ->will($this->returnValue($resolvedType));
  291. $resolvedType->expects($this->once())
  292. ->method('createBuilder')
  293. ->with($this->factory, 'TYPE', $options)
  294. ->will($this->returnValue($this->builder));
  295. $this->builder->expects($this->any())
  296. ->method('getOptions')
  297. ->will($this->returnValue($resolvedOptions));
  298. $resolvedType->expects($this->once())
  299. ->method('buildForm')
  300. ->with($this->builder, $resolvedOptions);
  301. $this->builder->expects($this->once())
  302. ->method('getForm')
  303. ->will($this->returnValue('FORM'));
  304. $this->assertSame('FORM', $this->factory->create('TYPE', null, $options));
  305. }
  306. /**
  307. * @group legacy
  308. */
  309. public function testCreateStripsNamespaceOffTypeName()
  310. {
  311. $options = array('a' => '1', 'b' => '2');
  312. $resolvedOptions = array('a' => '2', 'b' => '3');
  313. $resolvedType = $this->getMockResolvedType();
  314. $this->registry->expects($this->any())
  315. ->method('getType')
  316. ->with('Vendor\Name\Space\UserForm')
  317. ->will($this->returnValue($resolvedType));
  318. $resolvedType->expects($this->once())
  319. ->method('createBuilder')
  320. ->with($this->factory, 'user_form', $options)
  321. ->will($this->returnValue($this->builder));
  322. $this->builder->expects($this->any())
  323. ->method('getOptions')
  324. ->will($this->returnValue($resolvedOptions));
  325. $resolvedType->expects($this->once())
  326. ->method('buildForm')
  327. ->with($this->builder, $resolvedOptions);
  328. $this->builder->expects($this->once())
  329. ->method('getForm')
  330. ->will($this->returnValue('FORM'));
  331. $this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\UserForm', null, $options));
  332. }
  333. /**
  334. * @group legacy
  335. */
  336. public function testLegacyCreateStripsNamespaceOffTypeNameAccessByFQCN()
  337. {
  338. $options = array('a' => '1', 'b' => '2');
  339. $resolvedOptions = array('a' => '2', 'b' => '3');
  340. $resolvedType = $this->getMockResolvedType();
  341. $this->registry->expects($this->any())
  342. ->method('getType')
  343. ->with('userform')
  344. ->will($this->returnValue($resolvedType));
  345. $resolvedType->expects($this->once())
  346. ->method('createBuilder')
  347. ->with($this->factory, 'userform', $options)
  348. ->will($this->returnValue($this->builder));
  349. $this->builder->expects($this->any())
  350. ->method('getOptions')
  351. ->will($this->returnValue($resolvedOptions));
  352. $resolvedType->expects($this->once())
  353. ->method('buildForm')
  354. ->with($this->builder, $resolvedOptions);
  355. $this->builder->expects($this->once())
  356. ->method('getForm')
  357. ->will($this->returnValue('FORM'));
  358. $this->assertSame('FORM', $this->factory->create('userform', null, $options));
  359. }
  360. /**
  361. * @group legacy
  362. */
  363. public function testCreateStripsTypeSuffixOffTypeName()
  364. {
  365. $options = array('a' => '1', 'b' => '2');
  366. $resolvedOptions = array('a' => '2', 'b' => '3');
  367. $resolvedType = $this->getMockResolvedType();
  368. $this->registry->expects($this->any())
  369. ->method('getType')
  370. ->with('Vendor\Name\Space\UserType')
  371. ->will($this->returnValue($resolvedType));
  372. $resolvedType->expects($this->once())
  373. ->method('createBuilder')
  374. ->with($this->factory, 'user', $options)
  375. ->will($this->returnValue($this->builder));
  376. $this->builder->expects($this->any())
  377. ->method('getOptions')
  378. ->will($this->returnValue($resolvedOptions));
  379. $resolvedType->expects($this->once())
  380. ->method('buildForm')
  381. ->with($this->builder, $resolvedOptions);
  382. $this->builder->expects($this->once())
  383. ->method('getForm')
  384. ->will($this->returnValue('FORM'));
  385. $this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\UserType', null, $options));
  386. }
  387. /**
  388. * @group legacy
  389. */
  390. public function testCreateDoesNotStripTypeSuffixIfResultEmpty()
  391. {
  392. $options = array('a' => '1', 'b' => '2');
  393. $resolvedOptions = array('a' => '2', 'b' => '3');
  394. $resolvedType = $this->getMockResolvedType();
  395. $this->registry->expects($this->any())
  396. ->method('getType')
  397. ->with('Vendor\Name\Space\Type')
  398. ->will($this->returnValue($resolvedType));
  399. $resolvedType->expects($this->once())
  400. ->method('createBuilder')
  401. ->with($this->factory, 'type', $options)
  402. ->will($this->returnValue($this->builder));
  403. $this->builder->expects($this->any())
  404. ->method('getOptions')
  405. ->will($this->returnValue($resolvedOptions));
  406. $resolvedType->expects($this->once())
  407. ->method('buildForm')
  408. ->with($this->builder, $resolvedOptions);
  409. $this->builder->expects($this->once())
  410. ->method('getForm')
  411. ->will($this->returnValue('FORM'));
  412. $this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\Type', null, $options));
  413. }
  414. /**
  415. * @group legacy
  416. */
  417. public function testCreateConvertsTypeToUnderscoreSyntax()
  418. {
  419. $options = array('a' => '1', 'b' => '2');
  420. $resolvedOptions = array('a' => '2', 'b' => '3');
  421. $resolvedType = $this->getMockResolvedType();
  422. $this->registry->expects($this->any())
  423. ->method('getType')
  424. ->with('Vendor\Name\Space\MyProfileHTMLType')
  425. ->will($this->returnValue($resolvedType));
  426. $resolvedType->expects($this->once())
  427. ->method('createBuilder')
  428. ->with($this->factory, 'my_profile_html', $options)
  429. ->will($this->returnValue($this->builder));
  430. $this->builder->expects($this->any())
  431. ->method('getOptions')
  432. ->will($this->returnValue($resolvedOptions));
  433. $resolvedType->expects($this->once())
  434. ->method('buildForm')
  435. ->with($this->builder, $resolvedOptions);
  436. $this->builder->expects($this->once())
  437. ->method('getForm')
  438. ->will($this->returnValue('FORM'));
  439. $this->assertSame('FORM', $this->factory->create('Vendor\Name\Space\MyProfileHTMLType', null, $options));
  440. }
  441. /**
  442. * @group legacy
  443. */
  444. public function testCreateUsesTypeNameIfTypeGivenAsObject()
  445. {
  446. $options = array('a' => '1', 'b' => '2');
  447. $resolvedOptions = array('a' => '2', 'b' => '3');
  448. $resolvedType = $this->getMockResolvedType();
  449. $resolvedType->expects($this->once())
  450. ->method('getName')
  451. ->will($this->returnValue('TYPE'));
  452. $resolvedType->expects($this->once())
  453. ->method('createBuilder')
  454. ->with($this->factory, 'TYPE', $options)
  455. ->will($this->returnValue($this->builder));
  456. $this->builder->expects($this->any())
  457. ->method('getOptions')
  458. ->will($this->returnValue($resolvedOptions));
  459. $resolvedType->expects($this->once())
  460. ->method('buildForm')
  461. ->with($this->builder, $resolvedOptions);
  462. $this->builder->expects($this->once())
  463. ->method('getForm')
  464. ->will($this->returnValue('FORM'));
  465. $this->assertSame('FORM', $this->factory->create($resolvedType, null, $options));
  466. }
  467. public function testCreateNamed()
  468. {
  469. $options = array('a' => '1', 'b' => '2');
  470. $resolvedOptions = array('a' => '2', 'b' => '3');
  471. $resolvedType = $this->getMockResolvedType();
  472. $this->registry->expects($this->once())
  473. ->method('getType')
  474. ->with('type')
  475. ->will($this->returnValue($resolvedType));
  476. $resolvedType->expects($this->once())
  477. ->method('createBuilder')
  478. ->with($this->factory, 'name', $options)
  479. ->will($this->returnValue($this->builder));
  480. $this->builder->expects($this->any())
  481. ->method('getOptions')
  482. ->will($this->returnValue($resolvedOptions));
  483. $resolvedType->expects($this->once())
  484. ->method('buildForm')
  485. ->with($this->builder, $resolvedOptions);
  486. $this->builder->expects($this->once())
  487. ->method('getForm')
  488. ->will($this->returnValue('FORM'));
  489. $this->assertSame('FORM', $this->factory->createNamed('name', 'type', null, $options));
  490. }
  491. public function testCreateBuilderForPropertyWithoutTypeGuesser()
  492. {
  493. $registry = $this->getMockBuilder('Symfony\Component\Form\FormRegistryInterface')->getMock();
  494. $factory = $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  495. ->setMethods(array('createNamedBuilder'))
  496. ->setConstructorArgs(array($registry, $this->resolvedTypeFactory))
  497. ->getMock();
  498. $factory->expects($this->once())
  499. ->method('createNamedBuilder')
  500. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array())
  501. ->will($this->returnValue('builderInstance'));
  502. $this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  503. $this->assertEquals('builderInstance', $this->builder);
  504. }
  505. public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence()
  506. {
  507. $this->guesser1->expects($this->once())
  508. ->method('guessType')
  509. ->with('Application\Author', 'firstName')
  510. ->will($this->returnValue(new TypeGuess(
  511. 'Symfony\Component\Form\Extension\Core\Type\TextType',
  512. array('attr' => array('maxlength' => 10)),
  513. Guess::MEDIUM_CONFIDENCE
  514. )));
  515. $this->guesser2->expects($this->once())
  516. ->method('guessType')
  517. ->with('Application\Author', 'firstName')
  518. ->will($this->returnValue(new TypeGuess(
  519. 'Symfony\Component\Form\Extension\Core\Type\PasswordType',
  520. array('attr' => array('maxlength' => 7)),
  521. Guess::HIGH_CONFIDENCE
  522. )));
  523. $factory = $this->getMockFactory(array('createNamedBuilder'));
  524. $factory->expects($this->once())
  525. ->method('createNamedBuilder')
  526. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\PasswordType', null, array('attr' => array('maxlength' => 7)))
  527. ->will($this->returnValue('builderInstance'));
  528. $this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  529. $this->assertEquals('builderInstance', $this->builder);
  530. }
  531. public function testCreateBuilderCreatesTextFormIfNoGuess()
  532. {
  533. $this->guesser1->expects($this->once())
  534. ->method('guessType')
  535. ->with('Application\Author', 'firstName')
  536. ->will($this->returnValue(null));
  537. $factory = $this->getMockFactory(array('createNamedBuilder'));
  538. $factory->expects($this->once())
  539. ->method('createNamedBuilder')
  540. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType')
  541. ->will($this->returnValue('builderInstance'));
  542. $this->builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  543. $this->assertEquals('builderInstance', $this->builder);
  544. }
  545. public function testOptionsCanBeOverridden()
  546. {
  547. $this->guesser1->expects($this->once())
  548. ->method('guessType')
  549. ->with('Application\Author', 'firstName')
  550. ->will($this->returnValue(new TypeGuess(
  551. 'Symfony\Component\Form\Extension\Core\Type\TextType',
  552. array('attr' => array('class' => 'foo', 'maxlength' => 10)),
  553. Guess::MEDIUM_CONFIDENCE
  554. )));
  555. $factory = $this->getMockFactory(array('createNamedBuilder'));
  556. $factory->expects($this->once())
  557. ->method('createNamedBuilder')
  558. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array('attr' => array('class' => 'foo', 'maxlength' => 11)))
  559. ->will($this->returnValue('builderInstance'));
  560. $this->builder = $factory->createBuilderForProperty(
  561. 'Application\Author',
  562. 'firstName',
  563. null,
  564. array('attr' => array('maxlength' => 11))
  565. );
  566. $this->assertEquals('builderInstance', $this->builder);
  567. }
  568. public function testCreateBuilderUsesMaxLengthIfFound()
  569. {
  570. $this->guesser1->expects($this->once())
  571. ->method('guessMaxLength')
  572. ->with('Application\Author', 'firstName')
  573. ->will($this->returnValue(new ValueGuess(
  574. 15,
  575. Guess::MEDIUM_CONFIDENCE
  576. )));
  577. $this->guesser2->expects($this->once())
  578. ->method('guessMaxLength')
  579. ->with('Application\Author', 'firstName')
  580. ->will($this->returnValue(new ValueGuess(
  581. 20,
  582. Guess::HIGH_CONFIDENCE
  583. )));
  584. $factory = $this->getMockFactory(array('createNamedBuilder'));
  585. $factory->expects($this->once())
  586. ->method('createNamedBuilder')
  587. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array('attr' => array('maxlength' => 20)))
  588. ->will($this->returnValue('builderInstance'));
  589. $this->builder = $factory->createBuilderForProperty(
  590. 'Application\Author',
  591. 'firstName'
  592. );
  593. $this->assertEquals('builderInstance', $this->builder);
  594. }
  595. public function testCreateBuilderUsesMaxLengthAndPattern()
  596. {
  597. $this->guesser1->expects($this->once())
  598. ->method('guessMaxLength')
  599. ->with('Application\Author', 'firstName')
  600. ->will($this->returnValue(new ValueGuess(
  601. 20,
  602. Guess::HIGH_CONFIDENCE
  603. )));
  604. $this->guesser2->expects($this->once())
  605. ->method('guessPattern')
  606. ->with('Application\Author', 'firstName')
  607. ->will($this->returnValue(new ValueGuess(
  608. '.{5,}',
  609. Guess::HIGH_CONFIDENCE
  610. )));
  611. $factory = $this->getMockFactory(array('createNamedBuilder'));
  612. $factory->expects($this->once())
  613. ->method('createNamedBuilder')
  614. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array('attr' => array('maxlength' => 20, 'pattern' => '.{5,}', 'class' => 'tinymce')))
  615. ->will($this->returnValue('builderInstance'));
  616. $this->builder = $factory->createBuilderForProperty(
  617. 'Application\Author',
  618. 'firstName',
  619. null,
  620. array('attr' => array('class' => 'tinymce'))
  621. );
  622. $this->assertEquals('builderInstance', $this->builder);
  623. }
  624. public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
  625. {
  626. $this->guesser1->expects($this->once())
  627. ->method('guessRequired')
  628. ->with('Application\Author', 'firstName')
  629. ->will($this->returnValue(new ValueGuess(
  630. true,
  631. Guess::MEDIUM_CONFIDENCE
  632. )));
  633. $this->guesser2->expects($this->once())
  634. ->method('guessRequired')
  635. ->with('Application\Author', 'firstName')
  636. ->will($this->returnValue(new ValueGuess(
  637. false,
  638. Guess::HIGH_CONFIDENCE
  639. )));
  640. $factory = $this->getMockFactory(array('createNamedBuilder'));
  641. $factory->expects($this->once())
  642. ->method('createNamedBuilder')
  643. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array('required' => false))
  644. ->will($this->returnValue('builderInstance'));
  645. $this->builder = $factory->createBuilderForProperty(
  646. 'Application\Author',
  647. 'firstName'
  648. );
  649. $this->assertEquals('builderInstance', $this->builder);
  650. }
  651. public function testCreateBuilderUsesPatternIfFound()
  652. {
  653. $this->guesser1->expects($this->once())
  654. ->method('guessPattern')
  655. ->with('Application\Author', 'firstName')
  656. ->will($this->returnValue(new ValueGuess(
  657. '[a-z]',
  658. Guess::MEDIUM_CONFIDENCE
  659. )));
  660. $this->guesser2->expects($this->once())
  661. ->method('guessPattern')
  662. ->with('Application\Author', 'firstName')
  663. ->will($this->returnValue(new ValueGuess(
  664. '[a-zA-Z]',
  665. Guess::HIGH_CONFIDENCE
  666. )));
  667. $factory = $this->getMockFactory(array('createNamedBuilder'));
  668. $factory->expects($this->once())
  669. ->method('createNamedBuilder')
  670. ->with('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, array('attr' => array('pattern' => '[a-zA-Z]')))
  671. ->will($this->returnValue('builderInstance'));
  672. $this->builder = $factory->createBuilderForProperty(
  673. 'Application\Author',
  674. 'firstName'
  675. );
  676. $this->assertEquals('builderInstance', $this->builder);
  677. }
  678. private function getMockFactory(array $methods = array())
  679. {
  680. return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  681. ->setMethods($methods)
  682. ->setConstructorArgs(array($this->registry, $this->resolvedTypeFactory))
  683. ->getMock();
  684. }
  685. private function getMockResolvedType()
  686. {
  687. return $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeInterface')->getMock();
  688. }
  689. }