FormFactoryTest.php 6.9 KB

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