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