FieldTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. require_once __DIR__ . '/Fixtures/Author.php';
  12. require_once __DIR__ . '/Fixtures/TestField.php';
  13. require_once __DIR__ . '/Fixtures/InvalidField.php';
  14. require_once __DIR__ . '/Fixtures/RequiredOptionsField.php';
  15. use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
  16. use Symfony\Component\Form\PropertyPath;
  17. use Symfony\Component\Form\FieldError;
  18. use Symfony\Component\Form\Form;
  19. use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
  20. use Symfony\Tests\Component\Form\Fixtures\Author;
  21. use Symfony\Tests\Component\Form\Fixtures\TestField;
  22. use Symfony\Tests\Component\Form\Fixtures\InvalidField;
  23. use Symfony\Tests\Component\Form\Fixtures\RequiredOptionsField;
  24. class FieldTest extends \PHPUnit_Framework_TestCase
  25. {
  26. protected $field;
  27. protected function setUp()
  28. {
  29. $this->field = new TestField('title');
  30. }
  31. public function testGetPropertyPath_defaultPath()
  32. {
  33. $field = new TestField('title');
  34. $this->assertEquals(new PropertyPath('title'), $field->getPropertyPath());
  35. }
  36. public function testGetPropertyPath_pathIsZero()
  37. {
  38. $field = new TestField('title', array('property_path' => '0'));
  39. $this->assertEquals(new PropertyPath('0'), $field->getPropertyPath());
  40. }
  41. public function testGetPropertyPath_pathIsEmpty()
  42. {
  43. $field = new TestField('title', array('property_path' => ''));
  44. $this->assertEquals(null, $field->getPropertyPath());
  45. }
  46. public function testGetPropertyPath_pathIsNull()
  47. {
  48. $field = new TestField('title', array('property_path' => null));
  49. $this->assertEquals(null, $field->getPropertyPath());
  50. }
  51. public function testPassRequiredAsOption()
  52. {
  53. $field = new TestField('title', array('required' => false));
  54. $this->assertFalse($field->isRequired());
  55. $field = new TestField('title', array('required' => true));
  56. $this->assertTrue($field->isRequired());
  57. }
  58. public function testPassDisabledAsOption()
  59. {
  60. $field = new TestField('title', array('disabled' => false));
  61. $this->assertFalse($field->isDisabled());
  62. $field = new TestField('title', array('disabled' => true));
  63. $this->assertTrue($field->isDisabled());
  64. }
  65. public function testFieldIsDisabledIfParentIsDisabled()
  66. {
  67. $field = new TestField('title', array('disabled' => false));
  68. $field->setParent(new TestField('title', array('disabled' => true)));
  69. $this->assertTrue($field->isDisabled());
  70. }
  71. public function testFieldWithNoErrorsIsValid()
  72. {
  73. $this->field->submit('data');
  74. $this->assertTrue($this->field->isValid());
  75. }
  76. public function testFieldWithErrorsIsInvalid()
  77. {
  78. $this->field->submit('data');
  79. $this->field->addError(new FieldError('Some error'));
  80. $this->assertFalse($this->field->isValid());
  81. }
  82. public function testSubmitResetsErrors()
  83. {
  84. $this->field->addError(new FieldError('Some error'));
  85. $this->field->submit('data');
  86. $this->assertTrue($this->field->isValid());
  87. }
  88. public function testUnsubmittedFieldIsInvalid()
  89. {
  90. $this->assertFalse($this->field->isValid());
  91. }
  92. public function testIsRequiredReturnsOwnValueIfNoParent()
  93. {
  94. $this->field->setRequired(true);
  95. $this->assertTrue($this->field->isRequired());
  96. $this->field->setRequired(false);
  97. $this->assertFalse($this->field->isRequired());
  98. }
  99. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  100. {
  101. $group = $this->createMockGroup();
  102. $group->expects($this->any())
  103. ->method('isRequired')
  104. ->will($this->returnValue(true));
  105. $this->field->setParent($group);
  106. $this->field->setRequired(true);
  107. $this->assertTrue($this->field->isRequired());
  108. $this->field->setRequired(false);
  109. $this->assertFalse($this->field->isRequired());
  110. }
  111. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  112. {
  113. $group = $this->createMockGroup();
  114. $group->expects($this->any())
  115. ->method('isRequired')
  116. ->will($this->returnValue(false));
  117. $this->field->setParent($group);
  118. $this->field->setRequired(true);
  119. $this->assertFalse($this->field->isRequired());
  120. }
  121. public function testExceptionIfUnknownOption()
  122. {
  123. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
  124. new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
  125. }
  126. public function testExceptionIfMissingOption()
  127. {
  128. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  129. new RequiredOptionsField('name');
  130. }
  131. public function testIsSubmitted()
  132. {
  133. $this->assertFalse($this->field->isSubmitted());
  134. $this->field->submit('symfony');
  135. $this->assertTrue($this->field->isSubmitted());
  136. }
  137. public function testDefaultValuesAreTransformedCorrectly()
  138. {
  139. $field = new TestField('name');
  140. $this->assertEquals(null, $this->field->getData());
  141. $this->assertEquals('', $this->field->getDisplayedData());
  142. }
  143. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  144. {
  145. $this->field->setData(null);
  146. $this->assertSame(null, $this->field->getData());
  147. $this->assertSame('', $this->field->getDisplayedData());
  148. }
  149. public function testValuesAreTransformedCorrectlyIfNotNull_noValueTransformer()
  150. {
  151. $this->field->setData(123);
  152. $this->assertSame(123, $this->field->getData());
  153. $this->assertSame('123', $this->field->getDisplayedData());
  154. }
  155. public function testSubmittedValuesAreTransformedCorrectly()
  156. {
  157. $valueTransformer = $this->createMockTransformer();
  158. $normTransformer = $this->createMockTransformer();
  159. $field = $this->getMock(
  160. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  161. array('processData'), // only mock processData()
  162. array('title', array(
  163. 'value_transformer' => $valueTransformer,
  164. 'normalization_transformer' => $normTransformer,
  165. ))
  166. );
  167. // 1a. The value is converted to a string and passed to the value transformer
  168. $valueTransformer->expects($this->once())
  169. ->method('reverseTransform')
  170. ->with($this->identicalTo('0'))
  171. ->will($this->returnValue('reverse[0]'));
  172. // 2. The output of the reverse transformation is passed to processData()
  173. // The processed data is accessible through getNormalizedData()
  174. $field->expects($this->once())
  175. ->method('processData')
  176. ->with($this->equalTo('reverse[0]'))
  177. ->will($this->returnValue('processed[reverse[0]]'));
  178. // 3. The processed data is denormalized and then accessible through
  179. // getData()
  180. $normTransformer->expects($this->once())
  181. ->method('reverseTransform')
  182. ->with($this->identicalTo('processed[reverse[0]]'))
  183. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  184. // 4. The processed data is transformed again and then accessible
  185. // through getDisplayedData()
  186. $valueTransformer->expects($this->once())
  187. ->method('transform')
  188. ->with($this->equalTo('processed[reverse[0]]'))
  189. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  190. $field->submit(0);
  191. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  192. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  193. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  194. }
  195. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  196. {
  197. $transformer = $this->createMockTransformer();
  198. $field = $this->getMock(
  199. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  200. array('processData'), // only mock processData()
  201. array('title', array(
  202. 'value_transformer' => $transformer,
  203. ))
  204. );
  205. // 1. Empty values are converted to NULL by convention
  206. $transformer->expects($this->once())
  207. ->method('reverseTransform')
  208. ->with($this->identicalTo(''))
  209. ->will($this->returnValue(null));
  210. // 2. NULL is passed to processData()
  211. $field->expects($this->once())
  212. ->method('processData')
  213. ->with($this->identicalTo(null))
  214. ->will($this->returnValue('processed'));
  215. // 3. The processed data is transformed (for displayed data)
  216. $transformer->expects($this->once())
  217. ->method('transform')
  218. ->with($this->equalTo('processed'))
  219. ->will($this->returnValue('transform[processed]'));
  220. $field->submit('');
  221. $this->assertSame('processed', $field->getData());
  222. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  223. }
  224. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  225. {
  226. $transformer = $this->createMockTransformer();
  227. $field = new TestField('title', array(
  228. 'value_transformer' => $transformer,
  229. ));
  230. // 1. Empty values are converted to NULL by convention
  231. $transformer->expects($this->once())
  232. ->method('reverseTransform')
  233. ->with($this->identicalTo(''))
  234. ->will($this->returnValue(null));
  235. // 2. The processed data is NULL and therefore transformed to an empty
  236. // string by convention
  237. $transformer->expects($this->once())
  238. ->method('transform')
  239. ->with($this->identicalTo(null))
  240. ->will($this->returnValue(''));
  241. $field->submit('');
  242. $this->assertSame(null, $field->getData());
  243. $this->assertEquals('', $field->getDisplayedData());
  244. }
  245. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  246. {
  247. $this->field->submit('');
  248. $this->assertSame(null, $this->field->getData());
  249. $this->assertEquals('', $this->field->getDisplayedData());
  250. }
  251. public function testValuesAreTransformedCorrectly()
  252. {
  253. // The value is first passed to the normalization transformer...
  254. $normTransformer = $this->createMockTransformer();
  255. $normTransformer->expects($this->exactly(2))
  256. ->method('transform')
  257. // Impossible to test with PHPUnit because called twice
  258. // ->with($this->identicalTo(0))
  259. ->will($this->returnValue('norm[0]'));
  260. // ...and then to the value transformer
  261. $valueTransformer = $this->createMockTransformer();
  262. $valueTransformer->expects($this->exactly(2))
  263. ->method('transform')
  264. // Impossible to test with PHPUnit because called twice
  265. // ->with($this->identicalTo('norm[0]'))
  266. ->will($this->returnValue('transform[norm[0]]'));
  267. $field = new TestField('title', array(
  268. 'value_transformer' => $valueTransformer,
  269. 'normalization_transformer' => $normTransformer,
  270. ));
  271. $field->setData(0);
  272. $this->assertEquals(0, $field->getData());
  273. $this->assertEquals('norm[0]', $field->getNormalizedData());
  274. $this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
  275. }
  276. public function testSubmittedValuesAreTrimmedBeforeTransforming()
  277. {
  278. // The value is passed to the value transformer
  279. $transformer = $this->createMockTransformer();
  280. $transformer->expects($this->once())
  281. ->method('reverseTransform')
  282. ->with($this->identicalTo('a'))
  283. ->will($this->returnValue('reverse[a]'));
  284. $transformer->expects($this->exactly(2))
  285. ->method('transform')
  286. // Impossible to test with PHPUnit because called twice
  287. // ->with($this->identicalTo('reverse[a]'))
  288. ->will($this->returnValue('a'));
  289. $field = new TestField('title', array(
  290. 'value_transformer' => $transformer,
  291. ));
  292. $field->submit(' a ');
  293. $this->assertEquals('a', $field->getDisplayedData());
  294. $this->assertEquals('reverse[a]', $field->getData());
  295. }
  296. public function testSubmittedValuesAreNotTrimmedBeforeTransformingIfDisabled()
  297. {
  298. // The value is passed to the value transformer
  299. $transformer = $this->createMockTransformer();
  300. $transformer->expects($this->once())
  301. ->method('reverseTransform')
  302. ->with($this->identicalTo(' a '))
  303. ->will($this->returnValue('reverse[ a ]'));
  304. $transformer->expects($this->exactly(2))
  305. ->method('transform')
  306. // Impossible to test with PHPUnit because called twice
  307. // ->with($this->identicalTo('reverse[ a ]'))
  308. ->will($this->returnValue(' a '));
  309. $field = new TestField('title', array(
  310. 'trim' => false,
  311. 'value_transformer' => $transformer,
  312. ));
  313. $field->submit(' a ');
  314. $this->assertEquals(' a ', $field->getDisplayedData());
  315. $this->assertEquals('reverse[ a ]', $field->getData());
  316. }
  317. /*
  318. * This is important so that submit() can work even if setData() was not called
  319. * before
  320. */
  321. public function testWritePropertyTreatsEmptyValuesAsArrays()
  322. {
  323. $array = null;
  324. $field = new TestField('firstName');
  325. $field->submit('Bernhard');
  326. $field->writeProperty($array);
  327. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  328. }
  329. public function testWritePropertyDoesNotWritePropertyIfPropertyPathIsEmpty()
  330. {
  331. $object = new Author();
  332. $field = new TestField('firstName', array('property_path' => null));
  333. $field->submit('Bernhard');
  334. $field->writeProperty($object);
  335. $this->assertEquals(null, $object->firstName);
  336. }
  337. public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
  338. {
  339. $field = new TestField('title', array(
  340. 'trim' => false,
  341. ));
  342. $field->submit('a');
  343. $this->assertEquals('a', $field->getDisplayedData());
  344. $this->assertTrue($field->isTransformationSuccessful());
  345. }
  346. public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
  347. {
  348. // The value is passed to the value transformer
  349. $transformer = $this->createMockTransformer();
  350. $transformer->expects($this->once())
  351. ->method('reverseTransform')
  352. ->will($this->throwException(new TransformationFailedException()));
  353. $field = new TestField('title', array(
  354. 'trim' => false,
  355. 'value_transformer' => $transformer,
  356. ));
  357. $field->submit('a');
  358. $this->assertEquals('a', $field->getDisplayedData());
  359. $this->assertFalse($field->isTransformationSuccessful());
  360. }
  361. public function testGetRootReturnsRootOfParentIfSet()
  362. {
  363. $parent = $this->createMockGroup();
  364. $parent->expects($this->any())
  365. ->method('getRoot')
  366. ->will($this->returnValue('ROOT'));
  367. $this->field->setParent($parent);
  368. $this->assertEquals('ROOT', $this->field->getRoot());
  369. }
  370. public function testFieldsInitializedWithDataAreNotUpdatedWhenAddedToForms()
  371. {
  372. $author = new Author();
  373. $author->firstName = 'Bernhard';
  374. $field = new TestField('firstName', array(
  375. 'data' => 'foobar',
  376. ));
  377. $form = new Form('author', array(
  378. 'data' => $author,
  379. ));
  380. $form->add($field);
  381. $this->assertEquals('foobar', $field->getData());
  382. }
  383. public function testGetRootReturnsFieldIfNoParent()
  384. {
  385. $this->assertEquals($this->field, $this->field->getRoot());
  386. }
  387. public function testIsEmptyReturnsTrueIfNull()
  388. {
  389. $this->field->setData(null);
  390. $this->assertTrue($this->field->isEmpty());
  391. }
  392. public function testIsEmptyReturnsTrueIfEmptyString()
  393. {
  394. $this->field->setData('');
  395. $this->assertTrue($this->field->isEmpty());
  396. }
  397. public function testIsEmptyReturnsFalseIfZero()
  398. {
  399. $this->field->setData(0);
  400. $this->assertFalse($this->field->isEmpty());
  401. }
  402. protected function createMockTransformer()
  403. {
  404. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  405. }
  406. protected function createMockTransformerTransformingTo($value)
  407. {
  408. $transformer = $this->createMockTransformer();
  409. $transformer->expects($this->any())
  410. ->method('reverseTransform')
  411. ->will($this->returnValue($value));
  412. return $transformer;
  413. }
  414. protected function createMockGroup()
  415. {
  416. return $this->getMock(
  417. 'Symfony\Component\Form\Form',
  418. array(),
  419. array(),
  420. '',
  421. false // don't call constructor
  422. );
  423. }
  424. protected function createMockGroupWithName($name)
  425. {
  426. $group = $this->createMockGroup();
  427. $group->expects($this->any())
  428. ->method('getName')
  429. ->will($this->returnValue($name));
  430. return $group;
  431. }
  432. protected function createMockGroupWithId($id)
  433. {
  434. $group = $this->createMockGroup();
  435. $group->expects($this->any())
  436. ->method('getId')
  437. ->will($this->returnValue($id));
  438. return $group;
  439. }
  440. }