PageRenderTime 22ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/form/Symfony/Component/Form/Tests/Util/PropertyPathTest.php

https://bitbucket.org/laborautonomo/laborautonomo-site
PHP | 557 lines | 336 code | 135 blank | 86 comment | 0 complexity | d41d2630e1cd919f1bfc2088a3035f0e 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\Util;
  11. use Symfony\Component\Form\Util\PropertyPath;
  12. use Symfony\Component\Form\Tests\Fixtures\Author;
  13. use Symfony\Component\Form\Tests\Fixtures\Magician;
  14. class PropertyPathTest extends \PHPUnit_Framework_TestCase
  15. {
  16. public function testGetValueReadsArray()
  17. {
  18. $array = array('firstName' => 'Bernhard');
  19. $path = new PropertyPath('[firstName]');
  20. $this->assertEquals('Bernhard', $path->getValue($array));
  21. }
  22. /**
  23. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
  24. */
  25. public function testGetValueThrowsExceptionIfIndexNotationExpected()
  26. {
  27. $array = array('firstName' => 'Bernhard');
  28. $path = new PropertyPath('firstName');
  29. $path->getValue($array);
  30. }
  31. public function testGetValueReadsZeroIndex()
  32. {
  33. $array = array('Bernhard');
  34. $path = new PropertyPath('[0]');
  35. $this->assertEquals('Bernhard', $path->getValue($array));
  36. }
  37. public function testGetValueReadsIndexWithSpecialChars()
  38. {
  39. $array = array('%!@$§.' => 'Bernhard');
  40. $path = new PropertyPath('[%!@$§.]');
  41. $this->assertEquals('Bernhard', $path->getValue($array));
  42. }
  43. public function testGetValueReadsNestedIndexWithSpecialChars()
  44. {
  45. $array = array('root' => array('%!@$§.' => 'Bernhard'));
  46. $path = new PropertyPath('[root][%!@$§.]');
  47. $this->assertEquals('Bernhard', $path->getValue($array));
  48. }
  49. public function testGetValueReadsArrayWithCustomPropertyPath()
  50. {
  51. $array = array('child' => array('index' => array('firstName' => 'Bernhard')));
  52. $path = new PropertyPath('[child][index][firstName]');
  53. $this->assertEquals('Bernhard', $path->getValue($array));
  54. }
  55. public function testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
  56. {
  57. $array = array('child' => array('index' => array()));
  58. $path = new PropertyPath('[child][index][firstName]');
  59. $this->assertNull($path->getValue($array));
  60. }
  61. public function testGetValueReadsProperty()
  62. {
  63. $object = new Author();
  64. $object->firstName = 'Bernhard';
  65. $path = new PropertyPath('firstName');
  66. $this->assertEquals('Bernhard', $path->getValue($object));
  67. }
  68. public function testGetValueIgnoresSingular()
  69. {
  70. $this->markTestSkipped('This feature is temporarily disabled as of 2.1');
  71. $object = (object) array('children' => 'Many');
  72. $path = new PropertyPath('children|child');
  73. $this->assertEquals('Many', $path->getValue($object));
  74. }
  75. public function testGetValueReadsPropertyWithSpecialCharsExceptDot()
  76. {
  77. $array = (object) array('%!@$§' => 'Bernhard');
  78. $path = new PropertyPath('%!@$§');
  79. $this->assertEquals('Bernhard', $path->getValue($array));
  80. }
  81. public function testGetValueReadsPropertyWithCustomPropertyPath()
  82. {
  83. $object = new Author();
  84. $object->child = array();
  85. $object->child['index'] = new Author();
  86. $object->child['index']->firstName = 'Bernhard';
  87. $path = new PropertyPath('child[index].firstName');
  88. $this->assertEquals('Bernhard', $path->getValue($object));
  89. }
  90. /**
  91. * @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
  92. */
  93. public function testGetValueThrowsExceptionIfPropertyIsNotPublic()
  94. {
  95. $path = new PropertyPath('privateProperty');
  96. $path->getValue(new Author());
  97. }
  98. public function testGetValueReadsGetters()
  99. {
  100. $path = new PropertyPath('lastName');
  101. $object = new Author();
  102. $object->setLastName('Schussek');
  103. $this->assertEquals('Schussek', $path->getValue($object));
  104. }
  105. public function testGetValueCamelizesGetterNames()
  106. {
  107. $path = new PropertyPath('last_name');
  108. $object = new Author();
  109. $object->setLastName('Schussek');
  110. $this->assertEquals('Schussek', $path->getValue($object));
  111. }
  112. /**
  113. * @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
  114. */
  115. public function testGetValueThrowsExceptionIfGetterIsNotPublic()
  116. {
  117. $path = new PropertyPath('privateGetter');
  118. $path->getValue(new Author());
  119. }
  120. public function testGetValueReadsIssers()
  121. {
  122. $path = new PropertyPath('australian');
  123. $object = new Author();
  124. $object->setAustralian(false);
  125. $this->assertFalse($path->getValue($object));
  126. }
  127. public function testGetValueReadHassers()
  128. {
  129. $path = new PropertyPath('read_permissions');
  130. $object = new Author();
  131. $object->setReadPermissions(true);
  132. $this->assertTrue($path->getValue($object));
  133. }
  134. public function testGetValueReadsMagicGet()
  135. {
  136. $path = new PropertyPath('magicProperty');
  137. $object = new Magician();
  138. $object->__set('magicProperty', 'foobar');
  139. $this->assertSame('foobar', $path->getValue($object));
  140. }
  141. /*
  142. * https://github.com/symfony/symfony/pull/4450
  143. */
  144. public function testGetValueReadsMagicGetThatReturnsConstant()
  145. {
  146. $path = new PropertyPath('magicProperty');
  147. $object = new Magician();
  148. $this->assertNull($path->getValue($object));
  149. }
  150. /**
  151. * @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
  152. */
  153. public function testGetValueThrowsExceptionIfIsserIsNotPublic()
  154. {
  155. $path = new PropertyPath('privateIsser');
  156. $path->getValue(new Author());
  157. }
  158. /**
  159. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
  160. */
  161. public function testGetValueThrowsExceptionIfPropertyDoesNotExist()
  162. {
  163. $path = new PropertyPath('foobar');
  164. $path->getValue(new Author());
  165. }
  166. /**
  167. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  168. */
  169. public function testGetValueThrowsExceptionIfNotObjectOrArray()
  170. {
  171. $path = new PropertyPath('foobar');
  172. $path->getValue('baz');
  173. }
  174. /**
  175. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  176. */
  177. public function testGetValueThrowsExceptionIfNull()
  178. {
  179. $path = new PropertyPath('foobar');
  180. $path->getValue(null);
  181. }
  182. /**
  183. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  184. */
  185. public function testGetValueThrowsExceptionIfEmpty()
  186. {
  187. $path = new PropertyPath('foobar');
  188. $path->getValue('');
  189. }
  190. public function testSetValueUpdatesArrays()
  191. {
  192. $array = array();
  193. $path = new PropertyPath('[firstName]');
  194. $path->setValue($array, 'Bernhard');
  195. $this->assertEquals(array('firstName' => 'Bernhard'), $array);
  196. }
  197. /**
  198. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
  199. */
  200. public function testSetValueThrowsExceptionIfIndexNotationExpected()
  201. {
  202. $array = array();
  203. $path = new PropertyPath('firstName');
  204. $path->setValue($array, 'Bernhard');
  205. }
  206. public function testSetValueUpdatesArraysWithCustomPropertyPath()
  207. {
  208. $array = array();
  209. $path = new PropertyPath('[child][index][firstName]');
  210. $path->setValue($array, 'Bernhard');
  211. $this->assertEquals(array('child' => array('index' => array('firstName' => 'Bernhard'))), $array);
  212. }
  213. public function testSetValueUpdatesProperties()
  214. {
  215. $object = new Author();
  216. $path = new PropertyPath('firstName');
  217. $path->setValue($object, 'Bernhard');
  218. $this->assertEquals('Bernhard', $object->firstName);
  219. }
  220. public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
  221. {
  222. $object = new Author();
  223. $object->child = array();
  224. $object->child['index'] = new Author();
  225. $path = new PropertyPath('child[index].firstName');
  226. $path->setValue($object, 'Bernhard');
  227. $this->assertEquals('Bernhard', $object->child['index']->firstName);
  228. }
  229. public function testSetValueUpdateMagicSet()
  230. {
  231. $object = new Magician();
  232. $path = new PropertyPath('magicProperty');
  233. $path->setValue($object, 'foobar');
  234. $this->assertEquals('foobar', $object->__get('magicProperty'));
  235. }
  236. public function testSetValueUpdatesSetters()
  237. {
  238. $object = new Author();
  239. $path = new PropertyPath('lastName');
  240. $path->setValue($object, 'Schussek');
  241. $this->assertEquals('Schussek', $object->getLastName());
  242. }
  243. public function testSetValueCamelizesSetterNames()
  244. {
  245. $object = new Author();
  246. $path = new PropertyPath('last_name');
  247. $path->setValue($object, 'Schussek');
  248. $this->assertEquals('Schussek', $object->getLastName());
  249. }
  250. /**
  251. * @expectedException Symfony\Component\Form\Exception\PropertyAccessDeniedException
  252. */
  253. public function testSetValueThrowsExceptionIfGetterIsNotPublic()
  254. {
  255. $path = new PropertyPath('privateSetter');
  256. $path->setValue(new Author(), 'foobar');
  257. }
  258. /**
  259. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  260. */
  261. public function testSetValueThrowsExceptionIfNotObjectOrArray()
  262. {
  263. $path = new PropertyPath('foobar');
  264. $value = 'baz';
  265. $path->setValue($value, 'bam');
  266. }
  267. /**
  268. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  269. */
  270. public function testSetValueThrowsExceptionIfNull()
  271. {
  272. $path = new PropertyPath('foobar');
  273. $value = null;
  274. $path->setValue($value, 'bam');
  275. }
  276. /**
  277. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  278. */
  279. public function testSetValueThrowsExceptionIfEmpty()
  280. {
  281. $path = new PropertyPath('foobar');
  282. $value = '';
  283. $path->setValue($value, 'bam');
  284. }
  285. public function testToString()
  286. {
  287. $path = new PropertyPath('reference.traversable[index].property');
  288. $this->assertEquals('reference.traversable[index].property', $path->__toString());
  289. }
  290. /**
  291. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
  292. */
  293. public function testDotIsRequiredBeforeProperty()
  294. {
  295. new PropertyPath('[index]property');
  296. }
  297. /**
  298. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
  299. */
  300. public function testDotCannotBePresentAtTheBeginning()
  301. {
  302. new PropertyPath('.property');
  303. }
  304. /**
  305. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
  306. */
  307. public function testUnexpectedCharacters()
  308. {
  309. new PropertyPath('property.$form');
  310. }
  311. /**
  312. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyPathException
  313. */
  314. public function testPathCannotBeEmpty()
  315. {
  316. new PropertyPath('');
  317. }
  318. /**
  319. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  320. */
  321. public function testPathCannotBeNull()
  322. {
  323. new PropertyPath(null);
  324. }
  325. /**
  326. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  327. */
  328. public function testPathCannotBeFalse()
  329. {
  330. new PropertyPath(false);
  331. }
  332. public function testZeroIsValidPropertyPath()
  333. {
  334. new PropertyPath('0');
  335. }
  336. public function testGetParentWithDot()
  337. {
  338. $propertyPath = new PropertyPath('grandpa.parent.child');
  339. $this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
  340. }
  341. public function testGetParentWithIndex()
  342. {
  343. $propertyPath = new PropertyPath('grandpa.parent[child]');
  344. $this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
  345. }
  346. public function testGetParentWhenThereIsNoParent()
  347. {
  348. $propertyPath = new PropertyPath('path');
  349. $this->assertNull($propertyPath->getParent());
  350. }
  351. public function testCopyConstructor()
  352. {
  353. $propertyPath = new PropertyPath('grandpa.parent[child]');
  354. $copy = new PropertyPath($propertyPath);
  355. $this->assertEquals($propertyPath, $copy);
  356. }
  357. public function testGetElement()
  358. {
  359. $propertyPath = new PropertyPath('grandpa.parent[child]');
  360. $this->assertEquals('child', $propertyPath->getElement(2));
  361. }
  362. /**
  363. * @expectedException \OutOfBoundsException
  364. */
  365. public function testGetElementDoesNotAcceptInvalidIndices()
  366. {
  367. $propertyPath = new PropertyPath('grandpa.parent[child]');
  368. $propertyPath->getElement(3);
  369. }
  370. /**
  371. * @expectedException \OutOfBoundsException
  372. */
  373. public function testGetElementDoesNotAcceptNegativeIndices()
  374. {
  375. $propertyPath = new PropertyPath('grandpa.parent[child]');
  376. $propertyPath->getElement(-1);
  377. }
  378. public function testIsProperty()
  379. {
  380. $propertyPath = new PropertyPath('grandpa.parent[child]');
  381. $this->assertTrue($propertyPath->isProperty(1));
  382. $this->assertFalse($propertyPath->isProperty(2));
  383. }
  384. /**
  385. * @expectedException \OutOfBoundsException
  386. */
  387. public function testIsPropertyDoesNotAcceptInvalidIndices()
  388. {
  389. $propertyPath = new PropertyPath('grandpa.parent[child]');
  390. $propertyPath->isProperty(3);
  391. }
  392. /**
  393. * @expectedException \OutOfBoundsException
  394. */
  395. public function testIsPropertyDoesNotAcceptNegativeIndices()
  396. {
  397. $propertyPath = new PropertyPath('grandpa.parent[child]');
  398. $propertyPath->isProperty(-1);
  399. }
  400. public function testIsIndex()
  401. {
  402. $propertyPath = new PropertyPath('grandpa.parent[child]');
  403. $this->assertFalse($propertyPath->isIndex(1));
  404. $this->assertTrue($propertyPath->isIndex(2));
  405. }
  406. /**
  407. * @expectedException \OutOfBoundsException
  408. */
  409. public function testIsIndexDoesNotAcceptInvalidIndices()
  410. {
  411. $propertyPath = new PropertyPath('grandpa.parent[child]');
  412. $propertyPath->isIndex(3);
  413. }
  414. /**
  415. * @expectedException \OutOfBoundsException
  416. */
  417. public function testIsIndexDoesNotAcceptNegativeIndices()
  418. {
  419. $propertyPath = new PropertyPath('grandpa.parent[child]');
  420. $propertyPath->isIndex(-1);
  421. }
  422. }