PageRenderTime 95ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataExtractorTest.php

https://gitlab.com/Marwamimo/Crowdrise_Web
PHP | 427 lines | 333 code | 60 blank | 34 comment | 0 complexity | 15d574f2d09e9d9df64e34c878f07c86 MD5 | raw file
  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\Component\Form\Tests\Extension\DataCollector;
  11. use Symfony\Component\Form\CallbackTransformer;
  12. use Symfony\Component\Form\Exception\TransformationFailedException;
  13. use Symfony\Component\Form\Extension\DataCollector\FormDataExtractor;
  14. use Symfony\Component\Form\FormBuilder;
  15. use Symfony\Component\Form\FormError;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
  18. use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
  19. class FormDataExtractorTest_SimpleValueExporter extends ValueExporter
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function exportValue($value, $depth = 1, $deep = false)
  25. {
  26. return is_object($value) ? sprintf('object(%s)', get_class($value)) : var_export($value, true);
  27. }
  28. }
  29. /**
  30. * @author Bernhard Schussek <bschussek@gmail.com>
  31. */
  32. class FormDataExtractorTest extends \PHPUnit_Framework_TestCase
  33. {
  34. /**
  35. * @var FormDataExtractorTest_SimpleValueExporter
  36. */
  37. private $valueExporter;
  38. /**
  39. * @var FormDataExtractor
  40. */
  41. private $dataExtractor;
  42. /**
  43. * @var \PHPUnit_Framework_MockObject_MockObject
  44. */
  45. private $dispatcher;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. private $factory;
  50. protected function setUp()
  51. {
  52. $this->valueExporter = new FormDataExtractorTest_SimpleValueExporter();
  53. $this->dataExtractor = new FormDataExtractor($this->valueExporter);
  54. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  55. $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
  56. }
  57. public function testExtractConfiguration()
  58. {
  59. $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  60. $type->expects($this->any())
  61. ->method('getName')
  62. ->will($this->returnValue('type_name'));
  63. $type->expects($this->any())
  64. ->method('getInnerType')
  65. ->will($this->returnValue(new \stdClass()));
  66. $form = $this->createBuilder('name')
  67. ->setType($type)
  68. ->getForm();
  69. $this->assertSame(array(
  70. 'id' => 'name',
  71. 'name' => 'name',
  72. 'type' => 'type_name',
  73. 'type_class' => 'stdClass',
  74. 'synchronized' => 'true',
  75. 'passed_options' => array(),
  76. 'resolved_options' => array(),
  77. ), $this->dataExtractor->extractConfiguration($form));
  78. }
  79. public function testExtractConfigurationSortsPassedOptions()
  80. {
  81. $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  82. $type->expects($this->any())
  83. ->method('getName')
  84. ->will($this->returnValue('type_name'));
  85. $type->expects($this->any())
  86. ->method('getInnerType')
  87. ->will($this->returnValue(new \stdClass()));
  88. $options = array(
  89. 'b' => 'foo',
  90. 'a' => 'bar',
  91. 'c' => 'baz',
  92. );
  93. $form = $this->createBuilder('name')
  94. ->setType($type)
  95. // passed options are stored in an attribute by
  96. // ResolvedTypeDataCollectorProxy
  97. ->setAttribute('data_collector/passed_options', $options)
  98. ->getForm();
  99. $this->assertSame(array(
  100. 'id' => 'name',
  101. 'name' => 'name',
  102. 'type' => 'type_name',
  103. 'type_class' => 'stdClass',
  104. 'synchronized' => 'true',
  105. 'passed_options' => array(
  106. 'a' => "'bar'",
  107. 'b' => "'foo'",
  108. 'c' => "'baz'",
  109. ),
  110. 'resolved_options' => array(),
  111. ), $this->dataExtractor->extractConfiguration($form));
  112. }
  113. public function testExtractConfigurationSortsResolvedOptions()
  114. {
  115. $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  116. $type->expects($this->any())
  117. ->method('getName')
  118. ->will($this->returnValue('type_name'));
  119. $type->expects($this->any())
  120. ->method('getInnerType')
  121. ->will($this->returnValue(new \stdClass()));
  122. $options = array(
  123. 'b' => 'foo',
  124. 'a' => 'bar',
  125. 'c' => 'baz',
  126. );
  127. $form = $this->createBuilder('name', $options)
  128. ->setType($type)
  129. ->getForm();
  130. $this->assertSame(array(
  131. 'id' => 'name',
  132. 'name' => 'name',
  133. 'type' => 'type_name',
  134. 'type_class' => 'stdClass',
  135. 'synchronized' => 'true',
  136. 'passed_options' => array(),
  137. 'resolved_options' => array(
  138. 'a' => "'bar'",
  139. 'b' => "'foo'",
  140. 'c' => "'baz'",
  141. ),
  142. ), $this->dataExtractor->extractConfiguration($form));
  143. }
  144. public function testExtractConfigurationBuildsIdRecursively()
  145. {
  146. $type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  147. $type->expects($this->any())
  148. ->method('getName')
  149. ->will($this->returnValue('type_name'));
  150. $type->expects($this->any())
  151. ->method('getInnerType')
  152. ->will($this->returnValue(new \stdClass()));
  153. $grandParent = $this->createBuilder('grandParent')
  154. ->setCompound(true)
  155. ->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'))
  156. ->getForm();
  157. $parent = $this->createBuilder('parent')
  158. ->setCompound(true)
  159. ->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'))
  160. ->getForm();
  161. $form = $this->createBuilder('name')
  162. ->setType($type)
  163. ->getForm();
  164. $grandParent->add($parent);
  165. $parent->add($form);
  166. $this->assertSame(array(
  167. 'id' => 'grandParent_parent_name',
  168. 'name' => 'name',
  169. 'type' => 'type_name',
  170. 'type_class' => 'stdClass',
  171. 'synchronized' => 'true',
  172. 'passed_options' => array(),
  173. 'resolved_options' => array(),
  174. ), $this->dataExtractor->extractConfiguration($form));
  175. }
  176. public function testExtractDefaultData()
  177. {
  178. $form = $this->createBuilder('name')->getForm();
  179. $form->setData('Foobar');
  180. $this->assertSame(array(
  181. 'default_data' => array(
  182. 'norm' => "'Foobar'",
  183. ),
  184. 'submitted_data' => array(),
  185. ), $this->dataExtractor->extractDefaultData($form));
  186. }
  187. public function testExtractDefaultDataStoresModelDataIfDifferent()
  188. {
  189. $form = $this->createBuilder('name')
  190. ->addModelTransformer(new FixedDataTransformer(array(
  191. 'Foo' => 'Bar',
  192. )))
  193. ->getForm();
  194. $form->setData('Foo');
  195. $this->assertSame(array(
  196. 'default_data' => array(
  197. 'norm' => "'Bar'",
  198. 'model' => "'Foo'",
  199. ),
  200. 'submitted_data' => array(),
  201. ), $this->dataExtractor->extractDefaultData($form));
  202. }
  203. public function testExtractDefaultDataStoresViewDataIfDifferent()
  204. {
  205. $form = $this->createBuilder('name')
  206. ->addViewTransformer(new FixedDataTransformer(array(
  207. 'Foo' => 'Bar',
  208. )))
  209. ->getForm();
  210. $form->setData('Foo');
  211. $this->assertSame(array(
  212. 'default_data' => array(
  213. 'norm' => "'Foo'",
  214. 'view' => "'Bar'",
  215. ),
  216. 'submitted_data' => array(),
  217. ), $this->dataExtractor->extractDefaultData($form));
  218. }
  219. public function testExtractSubmittedData()
  220. {
  221. $form = $this->createBuilder('name')->getForm();
  222. $form->submit('Foobar');
  223. $this->assertSame(array(
  224. 'submitted_data' => array(
  225. 'norm' => "'Foobar'",
  226. ),
  227. 'errors' => array(),
  228. 'synchronized' => 'true',
  229. ), $this->dataExtractor->extractSubmittedData($form));
  230. }
  231. public function testExtractSubmittedDataStoresModelDataIfDifferent()
  232. {
  233. $form = $this->createBuilder('name')
  234. ->addModelTransformer(new FixedDataTransformer(array(
  235. 'Foo' => 'Bar',
  236. '' => '',
  237. )))
  238. ->getForm();
  239. $form->submit('Bar');
  240. $this->assertSame(array(
  241. 'submitted_data' => array(
  242. 'norm' => "'Bar'",
  243. 'model' => "'Foo'",
  244. ),
  245. 'errors' => array(),
  246. 'synchronized' => 'true',
  247. ), $this->dataExtractor->extractSubmittedData($form));
  248. }
  249. public function testExtractSubmittedDataStoresViewDataIfDifferent()
  250. {
  251. $form = $this->createBuilder('name')
  252. ->addViewTransformer(new FixedDataTransformer(array(
  253. 'Foo' => 'Bar',
  254. '' => '',
  255. )))
  256. ->getForm();
  257. $form->submit('Bar');
  258. $this->assertSame(array(
  259. 'submitted_data' => array(
  260. 'norm' => "'Foo'",
  261. 'view' => "'Bar'",
  262. ),
  263. 'errors' => array(),
  264. 'synchronized' => 'true',
  265. ), $this->dataExtractor->extractSubmittedData($form));
  266. }
  267. public function testExtractSubmittedDataStoresErrors()
  268. {
  269. $form = $this->createBuilder('name')->getForm();
  270. $form->submit('Foobar');
  271. $form->addError(new FormError('Invalid!'));
  272. $this->assertSame(array(
  273. 'submitted_data' => array(
  274. 'norm' => "'Foobar'",
  275. ),
  276. 'errors' => array(
  277. array('message' => 'Invalid!', 'origin' => null, 'cause' => null),
  278. ),
  279. 'synchronized' => 'true',
  280. ), $this->dataExtractor->extractSubmittedData($form));
  281. }
  282. public function testExtractSubmittedDataStoresErrorOrigin()
  283. {
  284. $form = $this->createBuilder('name')->getForm();
  285. $error = new FormError('Invalid!');
  286. $error->setOrigin($form);
  287. $form->submit('Foobar');
  288. $form->addError($error);
  289. $this->assertSame(array(
  290. 'submitted_data' => array(
  291. 'norm' => "'Foobar'",
  292. ),
  293. 'errors' => array(
  294. array('message' => 'Invalid!', 'origin' => spl_object_hash($form), 'cause' => null),
  295. ),
  296. 'synchronized' => 'true',
  297. ), $this->dataExtractor->extractSubmittedData($form));
  298. }
  299. public function testExtractSubmittedDataStoresErrorCause()
  300. {
  301. $form = $this->createBuilder('name')->getForm();
  302. $exception = new \Exception();
  303. $form->submit('Foobar');
  304. $form->addError(new FormError('Invalid!', null, array(), null, $exception));
  305. $this->assertSame(array(
  306. 'submitted_data' => array(
  307. 'norm' => "'Foobar'",
  308. ),
  309. 'errors' => array(
  310. array('message' => 'Invalid!', 'origin' => null, 'cause' => 'object(Exception)'),
  311. ),
  312. 'synchronized' => 'true',
  313. ), $this->dataExtractor->extractSubmittedData($form));
  314. }
  315. public function testExtractSubmittedDataRemembersIfNonSynchronized()
  316. {
  317. $form = $this->createBuilder('name')
  318. ->addModelTransformer(new CallbackTransformer(
  319. function () {},
  320. function () {
  321. throw new TransformationFailedException('Fail!');
  322. }
  323. ))
  324. ->getForm();
  325. $form->submit('Foobar');
  326. $this->assertSame(array(
  327. 'submitted_data' => array(
  328. 'norm' => "'Foobar'",
  329. 'model' => 'NULL',
  330. ),
  331. 'errors' => array(),
  332. 'synchronized' => 'false',
  333. ), $this->dataExtractor->extractSubmittedData($form));
  334. }
  335. public function testExtractViewVariables()
  336. {
  337. $view = new FormView();
  338. $view->vars = array(
  339. 'b' => 'foo',
  340. 'a' => 'bar',
  341. 'c' => 'baz',
  342. 'id' => 'foo_bar',
  343. 'name' => 'bar',
  344. );
  345. $this->assertSame(array(
  346. 'id' => 'foo_bar',
  347. 'name' => 'bar',
  348. 'view_vars' => array(
  349. 'a' => "'bar'",
  350. 'b' => "'foo'",
  351. 'c' => "'baz'",
  352. 'id' => "'foo_bar'",
  353. 'name' => "'bar'",
  354. ),
  355. ), $this->dataExtractor->extractViewVariables($view));
  356. }
  357. /**
  358. * @param string $name
  359. * @param array $options
  360. *
  361. * @return FormBuilder
  362. */
  363. private function createBuilder($name, array $options = array())
  364. {
  365. return new FormBuilder($name, null, $this->dispatcher, $this->factory, $options);
  366. }
  367. }