FormFactoryTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 $rendererFactoryLoader;
  19. private $factory;
  20. protected function setUp()
  21. {
  22. $this->typeLoader = $this->getMock('Symfony\Component\Form\Type\Loader\TypeLoaderInterface');
  23. $this->rendererFactoryLoader = $this->getMock('Symfony\Component\Form\Renderer\Loader\FormRendererFactoryLoaderInterface');
  24. $this->factory = new FormFactory($this->typeLoader, $this->rendererFactoryLoader);
  25. }
  26. public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
  27. {
  28. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  29. $guesser1->expects($this->once())
  30. ->method('guessType')
  31. ->with('Application\Author', 'firstName')
  32. ->will($this->returnValue(new TypeGuess(
  33. 'text',
  34. array('max_length' => 10),
  35. Guess::MEDIUM_CONFIDENCE
  36. )));
  37. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  38. $guesser2->expects($this->once())
  39. ->method('guessType')
  40. ->with('Application\Author', 'firstName')
  41. ->will($this->returnValue(new TypeGuess(
  42. 'password',
  43. array('max_length' => 7),
  44. Guess::HIGH_CONFIDENCE
  45. )));
  46. $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $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'), array($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\Type\Guesser\TypeGuesserInterface');
  72. $guesser->expects($this->once())
  73. ->method('guessType')
  74. ->with('Application\Author', 'firstName')
  75. ->will($this->returnValue(new TypeGuess(
  76. 'text',
  77. array('max_length' => 10),
  78. Guess::MEDIUM_CONFIDENCE
  79. )));
  80. $factory = $this->createMockFactory(array('createBuilder'), array($guesser));
  81. $factory->expects($this->once())
  82. ->method('createBuilder')
  83. ->with('text', 'firstName', array('max_length' => 11))
  84. ->will($this->returnValue('builderInstance'));
  85. $builder = $factory->createBuilderForProperty(
  86. 'Application\Author',
  87. 'firstName',
  88. array('max_length' => 11)
  89. );
  90. $this->assertEquals('builderInstance', $builder);
  91. }
  92. public function testCreateBuilderUsesMaxLengthIfFound()
  93. {
  94. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  95. $guesser1->expects($this->once())
  96. ->method('guessMaxLength')
  97. ->with('Application\Author', 'firstName')
  98. ->will($this->returnValue(new ValueGuess(
  99. 15,
  100. Guess::MEDIUM_CONFIDENCE
  101. )));
  102. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  103. $guesser2->expects($this->once())
  104. ->method('guessMaxLength')
  105. ->with('Application\Author', 'firstName')
  106. ->will($this->returnValue(new ValueGuess(
  107. 20,
  108. Guess::HIGH_CONFIDENCE
  109. )));
  110. $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
  111. $factory->expects($this->once())
  112. ->method('createBuilder')
  113. ->with('text', 'firstName', array('max_length' => 20))
  114. ->will($this->returnValue('builderInstance'));
  115. $builder = $factory->createBuilderForProperty(
  116. 'Application\Author',
  117. 'firstName'
  118. );
  119. $this->assertEquals('builderInstance', $builder);
  120. }
  121. public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
  122. {
  123. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  124. $guesser1->expects($this->once())
  125. ->method('guessRequired')
  126. ->with('Application\Author', 'firstName')
  127. ->will($this->returnValue(new ValueGuess(
  128. true,
  129. Guess::MEDIUM_CONFIDENCE
  130. )));
  131. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  132. $guesser2->expects($this->once())
  133. ->method('guessRequired')
  134. ->with('Application\Author', 'firstName')
  135. ->will($this->returnValue(new ValueGuess(
  136. false,
  137. Guess::HIGH_CONFIDENCE
  138. )));
  139. $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
  140. $factory->expects($this->once())
  141. ->method('createBuilder')
  142. ->with('text', 'firstName', array('required' => false))
  143. ->will($this->returnValue('builderInstance'));
  144. $builder = $factory->createBuilderForProperty(
  145. 'Application\Author',
  146. 'firstName'
  147. );
  148. $this->assertEquals('builderInstance', $builder);
  149. }
  150. private function createMockFactory(array $methods = array(), array $guessers = array())
  151. {
  152. return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  153. ->setMethods($methods)
  154. ->setConstructorArgs(array($this->typeLoader, $this->rendererFactoryLoader, $guessers))
  155. ->getMock();
  156. }
  157. }