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

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

https://bitbucket.org/vvanuytven/php_auto
PHP | 319 lines | 219 code | 68 blank | 32 comment | 2 complexity | 42611d8a90f43a472498fa7241f8438a 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 testMapFormsToDataWritesBackIfNotByReference()
  146. {
  147. $car = new \stdClass();
  148. $engine = new \stdClass();
  149. $propertyPath = $this->getPropertyPath('engine');
  150. $this->propertyAccessor->expects($this->once())
  151. ->method('setValue')
  152. ->with($car, $propertyPath, $engine);
  153. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  154. $config->setByReference(false);
  155. $config->setPropertyPath($propertyPath);
  156. $config->setData($engine);
  157. $form = $this->getForm($config);
  158. $this->mapper->mapFormsToData(array($form), $car);
  159. }
  160. public function testMapFormsToDataWritesBackIfByReferenceButNoReference()
  161. {
  162. $car = new \stdClass();
  163. $engine = new \stdClass();
  164. $propertyPath = $this->getPropertyPath('engine');
  165. $this->propertyAccessor->expects($this->once())
  166. ->method('setValue')
  167. ->with($car, $propertyPath, $engine);
  168. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  169. $config->setByReference(true);
  170. $config->setPropertyPath($propertyPath);
  171. $config->setData($engine);
  172. $form = $this->getForm($config);
  173. $this->mapper->mapFormsToData(array($form), $car);
  174. }
  175. public function testMapFormsToDataWritesBackIfByReferenceAndReference()
  176. {
  177. $car = new \stdClass();
  178. $engine = new \stdClass();
  179. $propertyPath = $this->getPropertyPath('engine');
  180. // $car already contains the reference of $engine
  181. $this->propertyAccessor->expects($this->once())
  182. ->method('getValue')
  183. ->with($car, $propertyPath)
  184. ->will($this->returnValue($engine));
  185. $this->propertyAccessor->expects($this->never())
  186. ->method('setValue');
  187. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  188. $config->setByReference(true);
  189. $config->setPropertyPath($propertyPath);
  190. $config->setData($engine);
  191. $form = $this->getForm($config);
  192. $this->mapper->mapFormsToData(array($form), $car);
  193. }
  194. public function testMapFormsToDataIgnoresUnmapped()
  195. {
  196. $car = new \stdClass();
  197. $engine = new \stdClass();
  198. $propertyPath = $this->getPropertyPath('engine');
  199. $this->propertyAccessor->expects($this->never())
  200. ->method('setValue');
  201. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  202. $config->setByReference(true);
  203. $config->setPropertyPath($propertyPath);
  204. $config->setData($engine);
  205. $config->setMapped(false);
  206. $form = $this->getForm($config);
  207. $this->mapper->mapFormsToData(array($form), $car);
  208. }
  209. public function testMapFormsToDataIgnoresEmptyData()
  210. {
  211. $car = new \stdClass();
  212. $propertyPath = $this->getPropertyPath('engine');
  213. $this->propertyAccessor->expects($this->never())
  214. ->method('setValue');
  215. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  216. $config->setByReference(true);
  217. $config->setPropertyPath($propertyPath);
  218. $config->setData(null);
  219. $form = $this->getForm($config);
  220. $this->mapper->mapFormsToData(array($form), $car);
  221. }
  222. public function testMapFormsToDataIgnoresUnsynchronized()
  223. {
  224. $car = new \stdClass();
  225. $engine = new \stdClass();
  226. $propertyPath = $this->getPropertyPath('engine');
  227. $this->propertyAccessor->expects($this->never())
  228. ->method('setValue');
  229. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  230. $config->setByReference(true);
  231. $config->setPropertyPath($propertyPath);
  232. $config->setData($engine);
  233. $form = $this->getForm($config, false);
  234. $this->mapper->mapFormsToData(array($form), $car);
  235. }
  236. public function testMapFormsToDataIgnoresDisabled()
  237. {
  238. $car = new \stdClass();
  239. $engine = new \stdClass();
  240. $propertyPath = $this->getPropertyPath('engine');
  241. $this->propertyAccessor->expects($this->never())
  242. ->method('setValue');
  243. $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
  244. $config->setByReference(true);
  245. $config->setPropertyPath($propertyPath);
  246. $config->setData($engine);
  247. $config->setDisabled(true);
  248. $form = $this->getForm($config);
  249. $this->mapper->mapFormsToData(array($form), $car);
  250. }
  251. }