public function callbackPostSubmit(FormEvent $event) { $form = $event->getForm(); if ($form->has('picture')) { $picFile = $form->get('picture')->getViewData(); if ($picFile instanceof \Symfony\Component\HttpFoundation\File\UploadedFile) { try { $pub = $event->getData(); $this->repository->insertUpload($pub, $picFile); $event->setData($pub); } catch (\Exception $e) { $form->get('picture')->addError(new FormError($e->getMessage())); } } } }
public function testValidInsert() { $file = $this->getMockBuilder('Symfony\\Component\\HttpFoundation\\File\\UploadedFile')->disableOriginalConstructor()->getMock(); $file->expects($this->atLeastOnce())->method('getMimeType')->will($this->returnValue('image/png')); $file->expects($this->atLeastOnce())->method('isValid')->will($this->returnValue(true)); $file->expects($this->atLeastOnce())->method('getPathname')->will($this->returnValue(__DIR__ . '/../../../Resources/public/img/mascot.png')); $file->expects($this->never())->method('move'); $this->assertEquals(0, $this->picture->getFileSize()); $this->sut->insertUpload($this->picture, $file); $this->assertEquals('image/png', $this->picture->getMimeType()); $this->assertRegexp('#^[\\da-f]{40}\\.png$#', $this->picture->getStorageKey()); $this->assertNotEquals(0, $this->picture->getFileSize()); return $this->picture->getStorageKey(); }