FieldTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. 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 testGetNameReturnsKey()
  93. {
  94. $this->assertEquals('title', $this->field->getName());
  95. }
  96. public function testGetNameIncludesParent()
  97. {
  98. $this->field->setParent($this->createMockGroupWithName('news[article]'));
  99. $this->assertEquals('news[article][title]', $this->field->getName());
  100. }
  101. public function testGetIdReturnsKey()
  102. {
  103. $this->assertEquals('title', $this->field->getId());
  104. }
  105. public function testGetIdIncludesParent()
  106. {
  107. $this->field->setParent($this->createMockGroupWithId('news_article'));
  108. $this->assertEquals('news_article_title', $this->field->getId());
  109. }
  110. public function testIsRequiredReturnsOwnValueIfNoParent()
  111. {
  112. $this->field->setRequired(true);
  113. $this->assertTrue($this->field->isRequired());
  114. $this->field->setRequired(false);
  115. $this->assertFalse($this->field->isRequired());
  116. }
  117. public function testIsRequiredReturnsOwnValueIfParentIsRequired()
  118. {
  119. $group = $this->createMockGroup();
  120. $group->expects($this->any())
  121. ->method('isRequired')
  122. ->will($this->returnValue(true));
  123. $this->field->setParent($group);
  124. $this->field->setRequired(true);
  125. $this->assertTrue($this->field->isRequired());
  126. $this->field->setRequired(false);
  127. $this->assertFalse($this->field->isRequired());
  128. }
  129. public function testIsRequiredReturnsFalseIfParentIsNotRequired()
  130. {
  131. $group = $this->createMockGroup();
  132. $group->expects($this->any())
  133. ->method('isRequired')
  134. ->will($this->returnValue(false));
  135. $this->field->setParent($group);
  136. $this->field->setRequired(true);
  137. $this->assertFalse($this->field->isRequired());
  138. }
  139. public function testExceptionIfUnknownOption()
  140. {
  141. $this->setExpectedException('Symfony\Component\Form\Exception\InvalidOptionsException');
  142. new RequiredOptionsField('name', array('bar' => 'baz', 'moo' => 'maa'));
  143. }
  144. public function testExceptionIfMissingOption()
  145. {
  146. $this->setExpectedException('Symfony\Component\Form\Exception\MissingOptionsException');
  147. new RequiredOptionsField('name');
  148. }
  149. public function testIsSubmitted()
  150. {
  151. $this->assertFalse($this->field->isSubmitted());
  152. $this->field->submit('symfony');
  153. $this->assertTrue($this->field->isSubmitted());
  154. }
  155. public function testDefaultValuesAreTransformedCorrectly()
  156. {
  157. $field = new TestField('name');
  158. $this->assertEquals(null, $this->field->getData());
  159. $this->assertEquals('', $this->field->getDisplayedData());
  160. }
  161. public function testValuesAreTransformedCorrectlyIfNull_noValueTransformer()
  162. {
  163. $this->field->setData(null);
  164. $this->assertSame(null, $this->field->getData());
  165. $this->assertSame('', $this->field->getDisplayedData());
  166. }
  167. public function testValuesAreTransformedCorrectlyIfNotNull_noValueTransformer()
  168. {
  169. $this->field->setData(123);
  170. $this->assertSame(123, $this->field->getData());
  171. $this->assertSame('123', $this->field->getDisplayedData());
  172. }
  173. public function testSubmittedValuesAreTransformedCorrectly()
  174. {
  175. $valueTransformer = $this->createMockTransformer();
  176. $normTransformer = $this->createMockTransformer();
  177. $field = $this->getMock(
  178. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  179. array('processData'), // only mock processData()
  180. array('title', array(
  181. 'value_transformer' => $valueTransformer,
  182. 'normalization_transformer' => $normTransformer,
  183. ))
  184. );
  185. // 1a. The value is converted to a string and passed to the value transformer
  186. $valueTransformer->expects($this->once())
  187. ->method('reverseTransform')
  188. ->with($this->identicalTo('0'))
  189. ->will($this->returnValue('reverse[0]'));
  190. // 2. The output of the reverse transformation is passed to processData()
  191. // The processed data is accessible through getNormalizedData()
  192. $field->expects($this->once())
  193. ->method('processData')
  194. ->with($this->equalTo('reverse[0]'))
  195. ->will($this->returnValue('processed[reverse[0]]'));
  196. // 3. The processed data is denormalized and then accessible through
  197. // getData()
  198. $normTransformer->expects($this->once())
  199. ->method('reverseTransform')
  200. ->with($this->identicalTo('processed[reverse[0]]'))
  201. ->will($this->returnValue('denorm[processed[reverse[0]]]'));
  202. // 4. The processed data is transformed again and then accessible
  203. // through getDisplayedData()
  204. $valueTransformer->expects($this->once())
  205. ->method('transform')
  206. ->with($this->equalTo('processed[reverse[0]]'))
  207. ->will($this->returnValue('transform[processed[reverse[0]]]'));
  208. $field->submit(0);
  209. $this->assertEquals('denorm[processed[reverse[0]]]', $field->getData());
  210. $this->assertEquals('processed[reverse[0]]', $field->getNormalizedData());
  211. $this->assertEquals('transform[processed[reverse[0]]]', $field->getDisplayedData());
  212. }
  213. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsValue()
  214. {
  215. $transformer = $this->createMockTransformer();
  216. $field = $this->getMock(
  217. 'Symfony\Tests\Component\Form\Fixtures\TestField',
  218. array('processData'), // only mock processData()
  219. array('title', array(
  220. 'value_transformer' => $transformer,
  221. ))
  222. );
  223. // 1. Empty values are converted to NULL by convention
  224. $transformer->expects($this->once())
  225. ->method('reverseTransform')
  226. ->with($this->identicalTo(''))
  227. ->will($this->returnValue(null));
  228. // 2. NULL is passed to processData()
  229. $field->expects($this->once())
  230. ->method('processData')
  231. ->with($this->identicalTo(null))
  232. ->will($this->returnValue('processed'));
  233. // 3. The processed data is transformed (for displayed data)
  234. $transformer->expects($this->once())
  235. ->method('transform')
  236. ->with($this->equalTo('processed'))
  237. ->will($this->returnValue('transform[processed]'));
  238. $field->submit('');
  239. $this->assertSame('processed', $field->getData());
  240. $this->assertEquals('transform[processed]', $field->getDisplayedData());
  241. }
  242. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull()
  243. {
  244. $transformer = $this->createMockTransformer();
  245. $field = new TestField('title', array(
  246. 'value_transformer' => $transformer,
  247. ));
  248. // 1. Empty values are converted to NULL by convention
  249. $transformer->expects($this->once())
  250. ->method('reverseTransform')
  251. ->with($this->identicalTo(''))
  252. ->will($this->returnValue(null));
  253. // 2. The processed data is NULL and therefore transformed to an empty
  254. // string by convention
  255. $transformer->expects($this->once())
  256. ->method('transform')
  257. ->with($this->identicalTo(null))
  258. ->will($this->returnValue(''));
  259. $field->submit('');
  260. $this->assertSame(null, $field->getData());
  261. $this->assertEquals('', $field->getDisplayedData());
  262. }
  263. public function testSubmittedValuesAreTransformedCorrectlyIfEmpty_processDataReturnsNull_noValueTransformer()
  264. {
  265. $this->field->submit('');
  266. $this->assertSame(null, $this->field->getData());
  267. $this->assertEquals('', $this->field->getDisplayedData());
  268. }
  269. public function testValuesAreTransformedCorrectly()
  270. {
  271. // The value is first passed to the normalization transformer...
  272. $normTransformer = $this->createMockTransformer();
  273. $normTransformer->expects($this->exactly(2))
  274. ->method('transform')
  275. // Impossible to test with PHPUnit because called twice
  276. // ->with($this->identicalTo(0))
  277. ->will($this->returnValue('norm[0]'));
  278. // ...and then to the value transformer
  279. $valueTransformer = $this->createMockTransformer();
  280. $valueTransformer->expects($this->exactly(2))
  281. ->method('transform')
  282. // Impossible to test with PHPUnit because called twice
  283. // ->with($this->identicalTo('norm[0]'))
  284. ->will($this->returnValue('transform[norm[0]]'));
  285. $field = new TestField('title', array(
  286. 'value_transformer' => $valueTransformer,
  287. 'normalization_transformer' => $normTransformer,
  288. ));
  289. $field->setData(0);
  290. $this->assertEquals(0, $field->getData());
  291. $this->assertEquals('norm[0]', $field->getNormalizedData());
  292. $this->assertEquals('transform[norm[0]]', $field->getDisplayedData());
  293. }
  294. public function testSubmittedValuesAreTrimmedBeforeTransforming()
  295. {
  296. // The value is passed to the value transformer
  297. $transformer = $this->createMockTransformer();
  298. $transformer->expects($this->once())
  299. ->method('reverseTransform')
  300. ->with($this->identicalTo('a'))
  301. ->will($this->returnValue('reverse[a]'));
  302. $transformer->expects($this->exactly(2))
  303. ->method('transform')
  304. // Impossible to test with PHPUnit because called twice
  305. // ->with($this->identicalTo('reverse[a]'))
  306. ->will($this->returnValue('a'));
  307. $field = new TestField('title', array(
  308. 'value_transformer' => $transformer,
  309. ));
  310. $field->submit(' a ');
  311. $this->assertEquals('a', $field->getDisplayedData());
  312. $this->assertEquals('reverse[a]', $field->getData());
  313. }
  314. public function testSubmittedValuesAreNotTrimmedBeforeTransformingIfDisabled()
  315. {
  316. // The value is passed to the value transformer
  317. $transformer = $this->createMockTransformer();
  318. $transformer->expects($this->once())
  319. ->method('reverseTransform')
  320. ->with($this->identicalTo(' a '))
  321. ->will($this->returnValue('reverse[ a ]'));
  322. $transformer->expects($this->exactly(2))
  323. ->method('transform')
  324. // Impossible to test with PHPUnit because called twice
  325. // ->with($this->identicalTo('reverse[ a ]'))
  326. ->will($this->returnValue(' a '));
  327. $field = new TestField('title', array(
  328. 'trim' => false,
  329. 'value_transformer' => $transformer,
  330. ));
  331. $field->submit(' a ');
  332. $this->assertEquals(' a ', $field->getDisplayedData());
  333. $this->assertEquals('reverse[ a ]', $field->getData());
  334. }
  335. /*
  336. * This is important so that submit() can work even if setData() was not called
  337. * before
  338. */
  339. public function testWritePropertyTreatsEmptyValuesAsArrays()
  340. {
  341. $array = null;
  342. $field = new TestField('firstName');
  343. $field->submit('Bernhard');
  344. $field->writeProperty($array);
  345. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  346. }
  347. public function testWritePropertyDoesNotWritePropertyIfPropertyPathIsEmpty()
  348. {
  349. $object = new Author();
  350. $field = new TestField('firstName', array('property_path' => null));
  351. $field->submit('Bernhard');
  352. $field->writeProperty($object);
  353. $this->assertEquals(null, $object->firstName);
  354. }
  355. public function testIsTransformationSuccessfulReturnsTrueIfReverseTransformSucceeded()
  356. {
  357. $field = new TestField('title', array(
  358. 'trim' => false,
  359. ));
  360. $field->submit('a');
  361. $this->assertEquals('a', $field->getDisplayedData());
  362. $this->assertTrue($field->isTransformationSuccessful());
  363. }
  364. public function testIsTransformationSuccessfulReturnsFalseIfReverseTransformThrowsException()
  365. {
  366. // The value is passed to the value transformer
  367. $transformer = $this->createMockTransformer();
  368. $transformer->expects($this->once())
  369. ->method('reverseTransform')
  370. ->will($this->throwException(new TransformationFailedException()));
  371. $field = new TestField('title', array(
  372. 'trim' => false,
  373. 'value_transformer' => $transformer,
  374. ));
  375. $field->submit('a');
  376. $this->assertEquals('a', $field->getDisplayedData());
  377. $this->assertFalse($field->isTransformationSuccessful());
  378. }
  379. public function testGetRootReturnsRootOfParentIfSet()
  380. {
  381. $parent = $this->createMockGroup();
  382. $parent->expects($this->any())
  383. ->method('getRoot')
  384. ->will($this->returnValue('ROOT'));
  385. $this->field->setParent($parent);
  386. $this->assertEquals('ROOT', $this->field->getRoot());
  387. }
  388. public function testFieldsInitializedWithDataAreNotUpdatedWhenAddedToForms()
  389. {
  390. $author = new Author();
  391. $author->firstName = 'Bernhard';
  392. $field = new TestField('firstName', array(
  393. 'data' => 'foobar',
  394. ));
  395. $form = new Form('author', array(
  396. 'data' => $author,
  397. ));
  398. $form->add($field);
  399. $this->assertEquals('foobar', $field->getData());
  400. }
  401. public function testGetRootReturnsFieldIfNoParent()
  402. {
  403. $this->assertEquals($this->field, $this->field->getRoot());
  404. }
  405. public function testIsEmptyReturnsTrueIfNull()
  406. {
  407. $this->field->setData(null);
  408. $this->assertTrue($this->field->isEmpty());
  409. }
  410. public function testIsEmptyReturnsTrueIfEmptyString()
  411. {
  412. $this->field->setData('');
  413. $this->assertTrue($this->field->isEmpty());
  414. }
  415. public function testIsEmptyReturnsFalseIfZero()
  416. {
  417. $this->field->setData(0);
  418. $this->assertFalse($this->field->isEmpty());
  419. }
  420. protected function createMockTransformer()
  421. {
  422. return $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface', array(), array(), '', false, false);
  423. }
  424. protected function createMockTransformerTransformingTo($value)
  425. {
  426. $transformer = $this->createMockTransformer();
  427. $transformer->expects($this->any())
  428. ->method('reverseTransform')
  429. ->will($this->returnValue($value));
  430. return $transformer;
  431. }
  432. protected function createMockGroup()
  433. {
  434. return $this->getMock(
  435. 'Symfony\Component\Form\Form',
  436. array(),
  437. array(),
  438. '',
  439. false // don't call constructor
  440. );
  441. }
  442. protected function createMockGroupWithName($name)
  443. {
  444. $group = $this->createMockGroup();
  445. $group->expects($this->any())
  446. ->method('getName')
  447. ->will($this->returnValue($name));
  448. return $group;
  449. }
  450. protected function createMockGroupWithId($id)
  451. {
  452. $group = $this->createMockGroup();
  453. $group->expects($this->any())
  454. ->method('getId')
  455. ->will($this->returnValue($id));
  456. return $group;
  457. }
  458. }