FormFactoryTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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'), array($guesser1, $guesser2));
  45. $factory->expects($this->once())
  46. ->method('createBuilder')
  47. ->with('password', 'firstName', 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('createBuilder'), array($guesser));
  60. $factory->expects($this->once())
  61. ->method('createBuilder')
  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('createBuilder'), array($guesser));
  79. $factory->expects($this->once())
  80. ->method('createBuilder')
  81. ->with('text', 'firstName', array('max_length' => 11))
  82. ->will($this->returnValue('builderInstance'));
  83. $builder = $factory->createBuilderForProperty(
  84. 'Application\Author',
  85. 'firstName',
  86. array('max_length' => 11)
  87. );
  88. $this->assertEquals('builderInstance', $builder);
  89. }
  90. public function testCreateBuilderUsesMaxLengthIfFound()
  91. {
  92. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  93. $guesser1->expects($this->once())
  94. ->method('guessMaxLength')
  95. ->with('Application\Author', 'firstName')
  96. ->will($this->returnValue(new ValueGuess(
  97. 15,
  98. Guess::MEDIUM_CONFIDENCE
  99. )));
  100. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  101. $guesser2->expects($this->once())
  102. ->method('guessMaxLength')
  103. ->with('Application\Author', 'firstName')
  104. ->will($this->returnValue(new ValueGuess(
  105. 20,
  106. Guess::HIGH_CONFIDENCE
  107. )));
  108. $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
  109. $factory->expects($this->once())
  110. ->method('createBuilder')
  111. ->with('text', 'firstName', array('max_length' => 20))
  112. ->will($this->returnValue('builderInstance'));
  113. $builder = $factory->createBuilderForProperty(
  114. 'Application\Author',
  115. 'firstName'
  116. );
  117. $this->assertEquals('builderInstance', $builder);
  118. }
  119. public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
  120. {
  121. $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  122. $guesser1->expects($this->once())
  123. ->method('guessRequired')
  124. ->with('Application\Author', 'firstName')
  125. ->will($this->returnValue(new ValueGuess(
  126. true,
  127. Guess::MEDIUM_CONFIDENCE
  128. )));
  129. $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
  130. $guesser2->expects($this->once())
  131. ->method('guessRequired')
  132. ->with('Application\Author', 'firstName')
  133. ->will($this->returnValue(new ValueGuess(
  134. false,
  135. Guess::HIGH_CONFIDENCE
  136. )));
  137. $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
  138. $factory->expects($this->once())
  139. ->method('createBuilder')
  140. ->with('text', 'firstName', array('required' => false))
  141. ->will($this->returnValue('builderInstance'));
  142. $builder = $factory->createBuilderForProperty(
  143. 'Application\Author',
  144. 'firstName'
  145. );
  146. $this->assertEquals('builderInstance', $builder);
  147. }
  148. private function createMockFactory(array $methods = array(), array $guessers = array())
  149. {
  150. return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
  151. ->setMethods($methods)
  152. ->setConstructorArgs(array($this->typeLoader, $guessers))
  153. ->getMock();
  154. }
  155. }