コード例 #1
0
ファイル: DocumentsTest.php プロジェクト: renatomefidf/sammui
 /**
  * Test Protocol Document
  * @depends testForm
  * @param Form $form
  */
 public function testProtocol(Form $form)
 {
     $formRepo = $this->documentManager->getRepository('FormBundle:Form');
     $formObj = $formRepo->find($form->getId());
     $protocol = new Protocol();
     $protocol->setForm($formObj);
     $protocol->setFirstSaveDate(new \MongoDate());
     $protocol->setLastSaveDate(new \MongoDate());
     $protocol->setConclusion('Conclusion');
     $file = new ProtocolFile();
     $upload = new UploadedFile(__FILE__, get_class());
     $file->setFile($upload);
     $file->setTitle('file_title');
     $file->setDescription('file_description');
     $file->setFilename($upload->getClientOriginalName());
     $file->setMimeType($upload->getClientMimeType());
     $file->setUploadDate(new \MongoDate());
     $file->setLength($upload->getSize());
     $file->setMd5('123');
     $file->setChunkSize(123);
     $file->setProtocol($protocol);
     $protocol->addFile($file);
     // Comment, User and NonUser are tested on ProtocolControllerTest
     $this->documentManager->persist($file);
     $this->documentManager->persist($protocol);
     $this->documentManager->flush();
     //        $this->documentManager->clear();
     $protocol = $this->documentManager->getRepository('FormBundle:Protocol')->find($protocol->getId());
     $this->assertNotEmpty($protocol->getId());
     $this->assertNotEmpty($protocol->getCreatedAt());
     $this->assertNotEmpty($protocol->getFirstSaveDate());
     $this->assertNotEmpty($protocol->getLastSaveDate());
     $this->assertNotEmpty($protocol->getForm());
     $this->assertEquals('Conclusion', $protocol->getConclusion());
     $this->assertTrue($protocol->getForm() instanceof Form);
     $this->assertEmpty($protocol->getOneComment('fake_123'));
     // Testing ProtocolFile
     //        $this->assertInstanceOf(get_class(new ArrayCollection()), $protocol->getFile());
     /** @var ProtocolFile $protocolFile */
     $protocolFile = $protocol->getFile()->first();
     $this->assertEquals('file_title', $protocolFile->getTitle());
     $this->assertEquals('file_description', $protocolFile->getDescription());
     $this->assertNotNull($protocolFile->getUploadDate());
     $this->assertNotNull($protocolFile->getLength());
     $this->assertNotNull($protocolFile->getChunkSize());
     $this->assertNotNull($protocolFile->getMd5());
     $protocol->removeFile($file);
     $this->documentManager->remove($protocol);
     $this->documentManager->remove($formObj);
     $this->documentManager->flush();
 }
コード例 #2
0
 /**
  * @Route("/upload/{protocolId}")
  * @Method({"POST"})
  * @param Request $request
  * @param $protocolId
  * @return null|ProtocolFile
  */
 public function postUploadAction(Request $request, $protocolId)
 {
     $protocolDM = $this->get('doctrine_mongodb')->getRepository('FormBundle:Protocol');
     $protocol = $protocolDM->find($protocolId);
     if (!$protocol) {
         throw $this->createNotFoundException("No Protocol found with id: \"{$protocolId}\"");
     }
     if ($request->files->get('file')) {
         /** @var $upload \Symfony\Component\HttpFoundation\File\UploadedFile */
         $upload = $request->files->get('file');
         $document = new ProtocolFile();
         $document->setFile($upload->getPathname());
         $document->setFilename($upload->getClientOriginalName());
         $document->setMimeType($upload->getClientMimeType());
         $document->setProtocol($protocol);
         $dm = $this->get('doctrine.odm.mongodb.document_manager');
         $dm->persist($document);
         $dm->flush();
         $dm->clear();
         return $protocolDM->find($protocol->getId())->getFile();
     } else {
         $view = $this->view(['code' => Response::HTTP_BAD_REQUEST, 'message' => 'You must provide a "file".'], Response::HTTP_BAD_REQUEST);
         return $this->handleView($view);
     }
 }