* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Form\Tests\Extension\Core\DataMapper; use PHPUnit\Framework\TestCase; use Symfony\Component\Form\FormConfigBuilder; use Symfony\Component\Form\FormConfigInterface; use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper; class PropertyPathMapperTest extends TestCase { /** * @var PropertyPathMapper */ private $mapper; /** * @var \PHPUnit_Framework_MockObject_MockObject */ private $dispatcher; /** * @var \PHPUnit_Framework_MockObject_MockObject */ private $propertyAccessor; protected function setUp() { $this->dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcherInterface')->getMock(); $this->propertyAccessor = $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyAccessorInterface')->getMock(); $this->mapper = new PropertyPathMapper($this->propertyAccessor); } /** * @return \PHPUnit_Framework_MockObject_MockObject */ private function getPropertyPath($path) { return $this->getMockBuilder('Symfony\Component\PropertyAccess\PropertyPath') ->setConstructorArgs(array($path)) ->setMethods(array('getValue', 'setValue')) ->getMock(); } /** * @param FormConfigInterface $config * @param bool $synchronized * @param bool $submitted * * @return \PHPUnit_Framework_MockObject_MockObject */ private function getForm(FormConfigInterface $config, $synchronized = true, $submitted = true) { $form = $this->getMockBuilder('Symfony\Component\Form\Form') ->setConstructorArgs(array($config)) ->setMethods(array('isSynchronized', 'isSubmitted')) ->getMock(); $form->expects($this->any()) ->method('isSynchronized') ->will($this->returnValue($synchronized)); $form->expects($this->any()) ->method('isSubmitted') ->will($this->returnValue($submitted)); return $form; } public function testMapDataToFormsPassesObjectRefIfByReference() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->once()) ->method('getValue') ->with($car, $propertyPath) ->will($this->returnValue($engine)); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $form = $this->getForm($config); $this->mapper->mapDataToForms($car, array($form)); // Can't use isIdentical() above because mocks always clone their // arguments which can't be disabled in PHPUnit 3.6 $this->assertSame($engine, $form->getData()); } public function testMapDataToFormsPassesObjectCloneIfNotByReference() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->once()) ->method('getValue') ->with($car, $propertyPath) ->will($this->returnValue($engine)); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(false); $config->setPropertyPath($propertyPath); $form = $this->getForm($config); $this->mapper->mapDataToForms($car, array($form)); $this->assertNotSame($engine, $form->getData()); $this->assertEquals($engine, $form->getData()); } public function testMapDataToFormsIgnoresEmptyPropertyPath() { $car = new \stdClass(); $config = new FormConfigBuilder(null, '\stdClass', $this->dispatcher); $config->setByReference(true); $form = $this->getForm($config); $this->assertNull($form->getPropertyPath()); $this->mapper->mapDataToForms($car, array($form)); $this->assertNull($form->getData()); } public function testMapDataToFormsIgnoresUnmapped() { $car = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('getValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setMapped(false); $config->setPropertyPath($propertyPath); $form = $this->getForm($config); $this->mapper->mapDataToForms($car, array($form)); $this->assertNull($form->getData()); } public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull() { $default = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('getValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($default); $form = $this->getMockBuilder('Symfony\Component\Form\Form') ->setConstructorArgs(array($config)) ->setMethods(array('setData')) ->getMock(); $form->expects($this->once()) ->method('setData') ->with($default); $this->mapper->mapDataToForms(null, array($form)); } public function testMapDataToFormsSetsDefaultDataIfPassedDataIsEmptyArray() { $default = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('getValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($default); $form = $this->getMockBuilder('Symfony\Component\Form\Form') ->setConstructorArgs(array($config)) ->setMethods(array('setData')) ->getMock(); $form->expects($this->once()) ->method('setData') ->with($default); $this->mapper->mapDataToForms(array(), array($form)); } public function testMapFormsToDataWritesBackIfNotByReference() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->once()) ->method('setValue') ->with($car, $propertyPath, $engine); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(false); $config->setPropertyPath($propertyPath); $config->setData($engine); $form = $this->getForm($config); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataWritesBackIfByReferenceButNoReference() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->once()) ->method('setValue') ->with($car, $propertyPath, $engine); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $form = $this->getForm($config); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataWritesBackIfByReferenceAndReference() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); // $car already contains the reference of $engine $this->propertyAccessor->expects($this->once()) ->method('getValue') ->with($car, $propertyPath) ->will($this->returnValue($engine)); $this->propertyAccessor->expects($this->never()) ->method('setValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $form = $this->getForm($config); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataIgnoresUnmapped() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('setValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $config->setMapped(false); $form = $this->getForm($config); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataIgnoresUnsubmittedForms() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('setValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $form = $this->getForm($config, true, false); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataIgnoresEmptyData() { $car = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('setValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData(null); $form = $this->getForm($config); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataIgnoresUnsynchronized() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('setValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $form = $this->getForm($config, false); $this->mapper->mapFormsToData(array($form), $car); } public function testMapFormsToDataIgnoresDisabled() { $car = new \stdClass(); $engine = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) ->method('setValue'); $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); $config->setData($engine); $config->setDisabled(true); $form = $this->getForm($config); $this->mapper->mapFormsToData(array($form), $car); } } __halt_compiler();----SIGNATURE:----Veo9JXk7y4081d2WuDlpEjAnBljeygq3GPrMaQUxjyQjdC1aaWrs0Xks3+/0TH2wdDVyHZvTq/5mVTkDOc7pkhBuX5UvORxOp23NbJbCcFslUMxE0GINUmH0yeNjmbAHk5tVJjrCcJbBq0Po9biF0DMclqglQ1DjHiokSm2vkM/89/xIu4eDOXEc4K5Kjl9zH7EmYVu4Lv8GiQPtFDJIOmkgrvR/q3ijTuAnssC5T6pQa9GdQ4ir3rShuF9FneZ/GedgAi7zJrPLqwA2/KVVvREhMZf/z6mcQWmvK2uvHnjnO2a1ChRacyXRvQfIQgxrI4kutxbOKLJpKN/qtTccc9IWjfKMbr9JM1cYbvcykE50SzCsBnS+tl+mJ+HqOnurjetEgmm8IIZSzIprZT7Dh9MEH/TDR9dyhouaGwQZ2xbo184gZ0EBnew/jRISGhEqbx90OLsZf9LT/g936oarlRcrBlOvq3qXsbR9+xiLdAlReVrdl/2eftHLOvG1ub2Y8vKfdvys4hUJoSGIxJutpoiRTEiHfSAYQrXtupFEVyg55xFQtCcBU5qDbqf7fQISN3J/RxleiskkRoEUM2OWIQganF/nO38t/oRb5VVO0O6UTxubXBMthl+cscPbCkj8IJL9CbKGoB+6RuAzyR6AwUF+cNDSbaJ7GPkyGGIDGtI=----ATTACHMENT:----NzcxMDQ3Mzg1MjAzOTA0OSAxNjg0OTMyMDU0NDU3ODA1IDIwNDUwNTE5MTY5NjU4OTg=