FormFactoryTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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('createNamedBuilder'), array($guesser1, $guesser2));
  45. $factory->expects($this->once())
  46. ->method('createNamedBuilder')
  47. ->with('password', 'firstName', null, array('max_length' => 7))
  48. ->will($this->returnValue('builderInstance'));
  49. $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  50. $this->assertEquals('builderInstance', $builder);
  51. }
  52. public function testCreateBuilderCreatesTextFieldIfNoGuess()
  53. {
  54. $guesser = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  55. $guesser->expects($this->once())
  56. ->method('guessType')
  57. ->with('Application\Author', 'firstName')
  58. ->will($this->returnValue(null));
  59. $factory = $this->createMockFactory(array('createNamedBuilder'), array($guesser));
  60. $factory->expects($this->once())
  61. ->method('createNamedBuilder')
  62. ->with('text', 'firstName')
  63. ->will($this->returnValue('builderInstance'));
  64. $builder = $factory->createBuilderForProperty('Application\Author', 'firstName');
  65. $this->assertEquals('builderInstance', $builder);
  66. }
  67. public function testOptionsCanBeOverridden()
  68. {
  69. $guesser = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  70. $guesser->expects($this->once())
  71. ->method('guessType')
  72. ->with('Application\Author', 'firstName')
  73. ->will($this->returnValue(new TypeGuess(
  74. 'text',
  75. array('max_length' => 10),
  76. Guess::MEDIUM_CONFIDENCE
  77. )));
  78. $factory = $this->createMockFactory(array('createNamedBuilder'), array($guesser));
  79. $factory->expects($this->once())
  80. ->method('createNamedBuilder')
  81. ->with('text', 'firstName', null, array('max_length' => 11))
  82. ->will($this->returnValue('builderInstance'));
  83. $builder = $factory->createBuilderForProperty(
  84. 'Application\Author',
  85. 'firstName',
  86. null,
  87. array('max_length' => 11)
  88. );
  89. $this->assertEquals('builderInstance', $builder);
  90. }
  91. public function testCreateBuilderUsesMaxLengthIfFound()
  92. {
  93. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  94. $guesser1->expects($this->once())
  95. ->method('guessMaxLength')
  96. ->with('Application\Author', 'firstName')
  97. ->will($this->returnValue(new ValueGuess(
  98. 15,
  99. Guess::MEDIUM_CONFIDENCE
  100. )));
  101. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  102. $guesser2->expects($this->once())
  103. ->method('guessMaxLength')
  104. ->with('Application\Author', 'firstName')
  105. ->will($this->returnValue(new ValueGuess(
  106. 20,
  107. Guess::HIGH_CONFIDENCE
  108. )));
  109. $factory = $this->createMockFactory(array('createNamedBuilder'), array($guesser1, $guesser2));
  110. $factory->expects($this->once())
  111. ->method('createNamedBuilder')
  112. ->with('text', 'firstName', null, array('max_length' => 20))
  113. ->will($this->returnValue('builderInstance'));
  114. $builder = $factory->createBuilderForProperty(
  115. 'Application\Author',
  116. 'firstName'
  117. );
  118. $this->assertEquals('builderInstance', $builder);
  119. }
  120. public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
  121. {
  122. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  123. $guesser1->expects($this->once())
  124. ->method('guessRequired')
  125. ->with('Application\Author', 'firstName')
  126. ->will($this->returnValue(new ValueGuess(
  127. true,
  128. Guess::MEDIUM_CONFIDENCE
  129. )));
  130. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  131. $guesser2->expects($this->once())
  132. ->method('guessRequired')
  133. ->with('Application\Author', 'firstName')
  134. ->will($this->returnValue(new ValueGuess(
  135. false,
  136. Guess::HIGH_CONFIDENCE
  137. )));
  138. $factory = $this->createMockFactory(array('createNamedBuilder'), array($guesser1, $guesser2));
  139. $factory->expects($this->once())
  140. ->method('createNamedBuilder')
  141. ->with('text', 'firstName', null, array('required' => false))
  142. ->will($this->returnValue('builderInstance'));
  143. $builder = $factory->createBuilderForProperty(
  144. 'Application\Author',
  145. 'firstName'
  146. );
  147. $this->assertEquals('builderInstance', $builder);
  148. }
  149. private function createMockFactory(array $methods = array(), array $guessers = array())
  150. {
  151. return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  152. ->setMethods($methods)
  153. ->setConstructorArgs(array($this->typeLoader, $guessers))
  154. ->getMock();
  155. }
  156. }