PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php

https://bitbucket.org/vladap/symfony
PHP | 384 lines | 269 code | 82 blank | 33 comment | 2 complexity | 9e9eb899de4ed19c42fec82442dd7c95 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\Core\DataMapper;
  11. use Symfony\Component\Form\FormConfigBuilder;
  12. use Symfony\Component\Form\FormConfigInterface;
  13. use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
  14. class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var PropertyPathMapper
  18. */
  19. private $mapper;
  20. /**
  21. * @var \PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $dispatcher;
  24. /**
  25. * @var \PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $propertyAccessor;
  28. protected function setUp()
  29. {
  30. if (!class_exists('Symfony\Component\EventDispatcher\Event')) {
  31. $this->markTestSkipped('The "EventDispatcher" component is not available');
  32. }
  33. if (!class_exists('Symfony\Component\PropertyAccess\PropertyAccess')) {
  34. $this->markTestSkipped('The "PropertyAccess" component is not available');
  35. }
  36. $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  37. $this->propertyAccessor = $this->getMock('Symfony\Component\PropertyAccess\PropertyAccessorInterface');
  38. $this->mapper = new PropertyPathMapper($this->propertyAccessor);
  39. }
  40. /**
  41. * @param $path
  42. * @return \PHPUnit_Framework_MockObject_MockObject
  43. */
  44. private function getPropertyPath($path)
  45. {
  46. return $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyPath')
  47. ->setConstructorArgs(array($path))
  48. ->setMethods(array('getValue', 'setValue'))
  49. ->getMock();
  50. }
  51. /**
  52. * @param FormConfigInterface $config
  53. * @param Boolean $synchronized
  54. * @return \PHPUnit_Framework_MockObject_MockObject
  55. */
  56. private function getForm(FormConfigInterface $config, $synchronized = true)
  57. {
  58. $form = $this->getMockBuilder('Symfony\Component\Form\Form')
  59. ->setConstructorArgs(array($config))
  60. ->setMethods(array('isSynchronized'))
  61. ->getMock();
  62. $form->expects($this->any())
  63. ->method('isSynchronized')
  64. ->will($this->returnValue($synchronized));
  65. return $form;
  66. }
  67. /**
  68. * @return \PHPUnit_Framework_MockObject_MockObject
  69. */
  70. private function getDataMapper()
  71. {
  72. return $this->getMock('Symfony\Component\Form\DataMapperInterface');
  73. }
  74. public function testMapDataToFormsPassesObjectRefIfByReference()
  75. {
  76. $car = new \stdClass();
  77. $engine = new \stdClass();
  78. $propertyPath = $this->getPropertyPath('engine');
  79. $this->propertyAccessor->expects($this->once())
  80. ->method('getValue')
  81. ->with($car, $propertyPath)
  82. ->will($this->returnValue($engine));
  83. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  84. $config->setByReference(true);
  85. $config->setPropertyPath($propertyPath);
  86. $form = $this->getForm($config);
  87. $this->mapper->mapDataToForms($car, array($form));
  88. // Can't use isIdentical() above because mocks always clone their
  89. // arguments which can't be disabled in PHPUnit 3.6
  90. $this->assertSame($engine, $form->getData());
  91. }
  92. public function testMapDataToFormsPassesObjectCloneIfNotByReference()
  93. {
  94. $car = new \stdClass();
  95. $engine = new \stdClass();
  96. $propertyPath = $this->getPropertyPath('engine');
  97. $this->propertyAccessor->expects($this->once())
  98. ->method('getValue')
  99. ->with($car, $propertyPath)
  100. ->will($this->returnValue($engine));
  101. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  102. $config->setByReference(false);
  103. $config->setPropertyPath($propertyPath);
  104. $form = $this->getForm($config);
  105. $this->mapper->mapDataToForms($car, array($form));
  106. $this->assertNotSame($engine, $form->getData());
  107. $this->assertEquals($engine, $form->getData());
  108. }
  109. public function testMapDataToFormsIgnoresEmptyPropertyPath()
  110. {
  111. $car = new \stdClass();
  112. $config = new FormConfigBuilder(null, '\stdClass', $this->dispatcher);
  113. $config->setByReference(true);
  114. $form = $this->getForm($config);
  115. $this->assertNull($form->getPropertyPath());
  116. $this->mapper->mapDataToForms($car, array($form));
  117. $this->assertNull($form->getData());
  118. }
  119. public function testMapDataToFormsIgnoresUnmapped()
  120. {
  121. $car = new \stdClass();
  122. $propertyPath = $this->getPropertyPath('engine');
  123. $this->propertyAccessor->expects($this->never())
  124. ->method('getValue');
  125. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  126. $config->setByReference(true);
  127. $config->setMapped(false);
  128. $config->setPropertyPath($propertyPath);
  129. $form = $this->getForm($config);
  130. $this->mapper->mapDataToForms($car, array($form));
  131. $this->assertNull($form->getData());
  132. }
  133. public function testMapDataToFormsIgnoresEmptyData()
  134. {
  135. $propertyPath = $this->getPropertyPath('engine');
  136. $this->propertyAccessor->expects($this->never())
  137. ->method('getValue');
  138. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  139. $config->setByReference(true);
  140. $config->setPropertyPath($propertyPath);
  141. $form = $this->getForm($config);
  142. $this->mapper->mapDataToForms(null, array($form));
  143. $this->assertNull($form->getData());
  144. }
  145. public function testMapDataToFormsSkipsVirtualForms()
  146. {
  147. $car = new \stdClass();
  148. $engine = new \stdClass();
  149. $propertyPath = $this->getPropertyPath('engine');
  150. $this->propertyAccessor->expects($this->once())
  151. ->method('getValue')
  152. ->with($car, $propertyPath)
  153. ->will($this->returnValue($engine));
  154. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  155. $config->setByReference(true);
  156. $config->setVirtual(true);
  157. $config->setCompound(true);
  158. $config->setDataMapper($this->getDataMapper());
  159. $form = $this->getForm($config);
  160. $config = new FormConfigBuilder('engine', '\stdClass', $this->dispatcher);
  161. $config->setByReference(true);
  162. $config->setPropertyPath($propertyPath);
  163. $child = $this->getForm($config);
  164. $form->add($child);
  165. $this->mapper->mapDataToForms($car, array($form));
  166. $this->assertNull($form->getData());
  167. $this->assertSame($engine, $child->getData());
  168. }
  169. public function testMapFormsToDataWritesBackIfNotByReference()
  170. {
  171. $car = new \stdClass();
  172. $engine = new \stdClass();
  173. $propertyPath = $this->getPropertyPath('engine');
  174. $this->propertyAccessor->expects($this->once())
  175. ->method('setValue')
  176. ->with($car, $propertyPath, $engine);
  177. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  178. $config->setByReference(false);
  179. $config->setPropertyPath($propertyPath);
  180. $config->setData($engine);
  181. $form = $this->getForm($config);
  182. $this->mapper->mapFormsToData(array($form), $car);
  183. }
  184. public function testMapFormsToDataWritesBackIfByReferenceButNoReference()
  185. {
  186. $car = new \stdClass();
  187. $engine = new \stdClass();
  188. $propertyPath = $this->getPropertyPath('engine');
  189. $this->propertyAccessor->expects($this->once())
  190. ->method('setValue')
  191. ->with($car, $propertyPath, $engine);
  192. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  193. $config->setByReference(true);
  194. $config->setPropertyPath($propertyPath);
  195. $config->setData($engine);
  196. $form = $this->getForm($config);
  197. $this->mapper->mapFormsToData(array($form), $car);
  198. }
  199. public function testMapFormsToDataWritesBackIfByReferenceAndReference()
  200. {
  201. $car = new \stdClass();
  202. $engine = new \stdClass();
  203. $propertyPath = $this->getPropertyPath('engine');
  204. // $car already contains the reference of $engine
  205. $this->propertyAccessor->expects($this->once())
  206. ->method('getValue')
  207. ->with($car, $propertyPath)
  208. ->will($this->returnValue($engine));
  209. $this->propertyAccessor->expects($this->never())
  210. ->method('setValue');
  211. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  212. $config->setByReference(true);
  213. $config->setPropertyPath($propertyPath);
  214. $config->setData($engine);
  215. $form = $this->getForm($config);
  216. $this->mapper->mapFormsToData(array($form), $car);
  217. }
  218. public function testMapFormsToDataIgnoresUnmapped()
  219. {
  220. $car = new \stdClass();
  221. $engine = new \stdClass();
  222. $propertyPath = $this->getPropertyPath('engine');
  223. $this->propertyAccessor->expects($this->never())
  224. ->method('setValue');
  225. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  226. $config->setByReference(true);
  227. $config->setPropertyPath($propertyPath);
  228. $config->setData($engine);
  229. $config->setMapped(false);
  230. $form = $this->getForm($config);
  231. $this->mapper->mapFormsToData(array($form), $car);
  232. }
  233. public function testMapFormsToDataIgnoresEmptyData()
  234. {
  235. $car = new \stdClass();
  236. $propertyPath = $this->getPropertyPath('engine');
  237. $this->propertyAccessor->expects($this->never())
  238. ->method('setValue');
  239. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  240. $config->setByReference(true);
  241. $config->setPropertyPath($propertyPath);
  242. $config->setData(null);
  243. $form = $this->getForm($config);
  244. $this->mapper->mapFormsToData(array($form), $car);
  245. }
  246. public function testMapFormsToDataIgnoresUnsynchronized()
  247. {
  248. $car = new \stdClass();
  249. $engine = new \stdClass();
  250. $propertyPath = $this->getPropertyPath('engine');
  251. $this->propertyAccessor->expects($this->never())
  252. ->method('setValue');
  253. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  254. $config->setByReference(true);
  255. $config->setPropertyPath($propertyPath);
  256. $config->setData($engine);
  257. $form = $this->getForm($config, false);
  258. $this->mapper->mapFormsToData(array($form), $car);
  259. }
  260. public function testMapFormsToDataIgnoresDisabled()
  261. {
  262. $car = new \stdClass();
  263. $engine = new \stdClass();
  264. $propertyPath = $this->getPropertyPath('engine');
  265. $this->propertyAccessor->expects($this->never())
  266. ->method('setValue');
  267. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  268. $config->setByReference(true);
  269. $config->setPropertyPath($propertyPath);
  270. $config->setData($engine);
  271. $config->setDisabled(true);
  272. $form = $this->getForm($config);
  273. $this->mapper->mapFormsToData(array($form), $car);
  274. }
  275. public function testMapFormsToDataSkipsVirtualForms()
  276. {
  277. $car = new \stdClass();
  278. $engine = new \stdClass();
  279. $parentPath = $this->getPropertyPath('name');
  280. $childPath = $this->getPropertyPath('engine');
  281. // getValue() and setValue() must never be invoked for $parentPath
  282. $this->propertyAccessor->expects($this->once())
  283. ->method('getValue')
  284. ->with($car, $childPath);
  285. $this->propertyAccessor->expects($this->once())
  286. ->method('setValue')
  287. ->with($car, $childPath, $engine);
  288. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  289. $config->setPropertyPath($parentPath);
  290. $config->setVirtual(true);
  291. $config->setCompound(true);
  292. $config->setDataMapper($this->getDataMapper());
  293. $form = $this->getForm($config);
  294. $config = new FormConfigBuilder('engine', '\stdClass', $this->dispatcher);
  295. $config->setByReference(true);
  296. $config->setPropertyPath($childPath);
  297. $config->setData($engine);
  298. $child = $this->getForm($config);
  299. $form->add($child);
  300. $this->mapper->mapFormsToData(array($form), $car);
  301. }
  302. }