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

/src/Symfony/Component/Form/Tests/Util/PropertyPathCollectionTest.php

https://github.com/Exercise/symfony
PHP | 185 lines | 130 code | 40 blank | 15 comment | 0 complexity | 27c37c7836f4a34f9677cd4da6fb25ee 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 PropertyPathCollectionTest_Car
  15. {
  16. // In the test, use a name that FormUtil can't uniquely singularify
  17. public function addAxis($axis) {}
  18. public function removeAxis($axis) {}
  19. public function getAxes() {}
  20. }
  21. class PropertyPathCollectionTest_CarCustomSingular
  22. {
  23. public function addFoo($axis) {}
  24. public function removeFoo($axis) {}
  25. public function getAxes() {}
  26. }
  27. class PropertyPathCollectionTest_Engine
  28. {
  29. }
  30. class PropertyPathCollectionTest_CarOnlyAdder
  31. {
  32. public function addAxis($axis) {}
  33. public function getAxes() {}
  34. }
  35. class PropertyPathCollectionTest_CarOnlyRemover
  36. {
  37. public function removeAxis($axis) {}
  38. public function getAxes() {}
  39. }
  40. class PropertyPathCollectionTest_CompositeCar
  41. {
  42. public function getStructure() {}
  43. }
  44. class PropertyPathCollectionTest_CarStructure
  45. {
  46. public function addAxis($axis) {}
  47. public function removeAxis($axis) {}
  48. public function getAxes() {}
  49. }
  50. abstract class PropertyPathCollectionTest extends \PHPUnit_Framework_TestCase
  51. {
  52. abstract protected function getCollection(array $array);
  53. public function testSetValueCallsAdderAndRemoverForCollections()
  54. {
  55. $car = $this->getMock(__CLASS__ . '_Car');
  56. $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
  57. $axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
  58. $path = new PropertyPath('axes');
  59. $car->expects($this->at(0))
  60. ->method('getAxes')
  61. ->will($this->returnValue($axesBefore));
  62. $car->expects($this->at(1))
  63. ->method('removeAxis')
  64. ->with('fourth');
  65. $car->expects($this->at(2))
  66. ->method('addAxis')
  67. ->with('first');
  68. $car->expects($this->at(3))
  69. ->method('addAxis')
  70. ->with('third');
  71. $path->setValue($car, $axesAfter);
  72. }
  73. public function testSetValueCallsAdderAndRemoverForNestedCollections()
  74. {
  75. $car = $this->getMock(__CLASS__ . '_CompositeCar');
  76. $structure = $this->getMock(__CLASS__ . '_CarStructure');
  77. $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
  78. $axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
  79. $path = new PropertyPath('structure.axes');
  80. $car->expects($this->any())
  81. ->method('getStructure')
  82. ->will($this->returnValue($structure));
  83. $structure->expects($this->at(0))
  84. ->method('getAxes')
  85. ->will($this->returnValue($axesBefore));
  86. $structure->expects($this->at(1))
  87. ->method('removeAxis')
  88. ->with('fourth');
  89. $structure->expects($this->at(2))
  90. ->method('addAxis')
  91. ->with('first');
  92. $structure->expects($this->at(3))
  93. ->method('addAxis')
  94. ->with('third');
  95. $path->setValue($car, $axesAfter);
  96. }
  97. public function testSetValueCallsCustomAdderAndRemover()
  98. {
  99. $car = $this->getMock(__CLASS__ . '_CarCustomSingular');
  100. $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
  101. $axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
  102. $path = new PropertyPath('axes|foo');
  103. $car->expects($this->at(0))
  104. ->method('getAxes')
  105. ->will($this->returnValue($axesBefore));
  106. $car->expects($this->at(1))
  107. ->method('removeFoo')
  108. ->with('fourth');
  109. $car->expects($this->at(2))
  110. ->method('addFoo')
  111. ->with('first');
  112. $car->expects($this->at(3))
  113. ->method('addFoo')
  114. ->with('third');
  115. $path->setValue($car, $axesAfter);
  116. }
  117. /**
  118. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
  119. */
  120. public function testMapFormToDataFailsIfOnlyAdderFound()
  121. {
  122. $car = $this->getMock(__CLASS__ . '_CarOnlyAdder');
  123. $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
  124. $axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
  125. $path = new PropertyPath('axes');
  126. $car->expects($this->any())
  127. ->method('getAxes')
  128. ->will($this->returnValue($axesBefore));
  129. $path->setValue($car, $axesAfter);
  130. }
  131. /**
  132. * @expectedException Symfony\Component\Form\Exception\InvalidPropertyException
  133. */
  134. public function testMapFormToDataFailsIfOnlyRemoverFound()
  135. {
  136. $car = $this->getMock(__CLASS__ . '_CarOnlyRemover');
  137. $axesBefore = $this->getCollection(array(1 => 'second', 3 => 'fourth'));
  138. $axesAfter = $this->getCollection(array(0 => 'first', 1 => 'second', 2 => 'third'));
  139. $path = new PropertyPath('axes');
  140. $car->expects($this->any())
  141. ->method('getAxes')
  142. ->will($this->returnValue($axesBefore));
  143. $path->setValue($car, $axesAfter);
  144. }
  145. }