FormFactoryTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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\FormFactory;
  11. use Symfony\Component\Form\FormFactory;
  12. use Symfony\Component\Form\FieldGuesser\FieldGuess;
  13. use Symfony\Component\Form\FieldGuesser\FieldTypeGuess;
  14. class FormFactoryTest extends \PHPUnit_Framework_TestCase
  15. {
  16. private $typeLoader;
  17. private $factory;
  18. protected function setUp()
  19. {
  20. $this->typeLoader = $this->getMock('Symfony\Component\Form\Type\Loader\TypeLoaderInterface');
  21. $this->factory = new FormFactory($this->typeLoader);
  22. }
  23. public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
  24. {
  25. $guesser1 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  26. $guesser1->expects($this->once())
  27. ->method('guessType')
  28. ->with('Application\Author', 'firstName')
  29. ->will($this->returnValue(new FieldTypeGuess(
  30. 'text',
  31. array('max_length' => 10),
  32. FieldGuess::MEDIUM_CONFIDENCE
  33. )));
  34. $guesser2 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  35. $guesser2->expects($this->once())
  36. ->method('guessType')
  37. ->with('Application\Author', 'firstName')
  38. ->will($this->returnValue(new FieldTypeGuess(
  39. 'password',
  40. array('max_length' => 7),
  41. FieldGuess::HIGH_CONFIDENCE
  42. )));
  43. $factory = $this->createMockFactory(array('createBuilder'));
  44. $factory->addGuesser($guesser1);
  45. $factory->addGuesser($guesser2);
  46. $factory->expects($this->once())
  47. ->method('createBuilder')
  48. ->with('password', 'firstName', array('max_length' => 7))
  49. ->will($this->returnValue('builderInstance'));
  50. $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  51. $this->assertEquals('builderInstance', $builder);
  52. }
  53. public function testCreateBuilderCreatesTextFieldIfNoGuess()
  54. {
  55. $guesser = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  56. $guesser->expects($this->once())
  57. ->method('guessType')
  58. ->with('Application\Author', 'firstName')
  59. ->will($this->returnValue(null));
  60. $factory = $this->createMockFactory(array('createBuilder'));
  61. $factory->addGuesser($guesser);
  62. $factory->expects($this->once())
  63. ->method('createBuilder')
  64. ->with('text', 'firstName')
  65. ->will($this->returnValue('builderInstance'));
  66. $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  67. $this->assertEquals('builderInstance', $builder);
  68. }
  69. public function testOptionsCanBeOverridden()
  70. {
  71. $guesser = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  72. $guesser->expects($this->once())
  73. ->method('guessType')
  74. ->with('Application\Author', 'firstName')
  75. ->will($this->returnValue(new FieldTypeGuess(
  76. 'text',
  77. array('max_length' => 10),
  78. FieldGuess::MEDIUM_CONFIDENCE
  79. )));
  80. $factory = $this->createMockFactory(array('createBuilder'));
  81. $factory->addGuesser($guesser);
  82. $factory->expects($this->once())
  83. ->method('createBuilder')
  84. ->with('text', 'firstName', array('max_length' => 11))
  85. ->will($this->returnValue('builderInstance'));
  86. $builder = $factory->createBuilderForProperty(
  87. 'Application\Author',
  88. 'firstName',
  89. array('max_length' => 11)
  90. );
  91. $this->assertEquals('builderInstance', $builder);
  92. }
  93. public function testCreateBuilderUsesMaxLengthIfFound()
  94. {
  95. $guesser1 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  96. $guesser1->expects($this->once())
  97. ->method('guessMaxLength')
  98. ->with('Application\Author', 'firstName')
  99. ->will($this->returnValue(new FieldGuess(
  100. 15,
  101. FieldGuess::MEDIUM_CONFIDENCE
  102. )));
  103. $guesser2 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  104. $guesser2->expects($this->once())
  105. ->method('guessMaxLength')
  106. ->with('Application\Author', 'firstName')
  107. ->will($this->returnValue(new FieldGuess(
  108. 20,
  109. FieldGuess::HIGH_CONFIDENCE
  110. )));
  111. $factory = $this->createMockFactory(array('createBuilder'));
  112. $factory->addGuesser($guesser1);
  113. $factory->addGuesser($guesser2);
  114. $factory->expects($this->once())
  115. ->method('createBuilder')
  116. ->with('text', 'firstName', array('max_length' => 20))
  117. ->will($this->returnValue('builderInstance'));
  118. $builder = $factory->createBuilderForProperty(
  119. 'Application\Author',
  120. 'firstName'
  121. );
  122. $this->assertEquals('builderInstance', $builder);
  123. }
  124. public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
  125. {
  126. $guesser1 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  127. $guesser1->expects($this->once())
  128. ->method('guessRequired')
  129. ->with('Application\Author', 'firstName')
  130. ->will($this->returnValue(new FieldGuess(
  131. true,
  132. FieldGuess::MEDIUM_CONFIDENCE
  133. )));
  134. $guesser2 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
  135. $guesser2->expects($this->once())
  136. ->method('guessRequired')
  137. ->with('Application\Author', 'firstName')
  138. ->will($this->returnValue(new FieldGuess(
  139. false,
  140. FieldGuess::HIGH_CONFIDENCE
  141. )));
  142. $factory = $this->createMockFactory(array('createBuilder'));
  143. $factory->addGuesser($guesser1);
  144. $factory->addGuesser($guesser2);
  145. $factory->expects($this->once())
  146. ->method('createBuilder')
  147. ->with('text', 'firstName', array('required' => false))
  148. ->will($this->returnValue('builderInstance'));
  149. $builder = $factory->createBuilderForProperty(
  150. 'Application\Author',
  151. 'firstName'
  152. );
  153. $this->assertEquals('builderInstance', $builder);
  154. }
  155. private function createMockFactory(array $methods = array())
  156. {
  157. return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  158. ->setMethods($methods)
  159. ->setConstructorArgs(array($this->typeLoader))
  160. ->getMock();
  161. }
  162. }