/**
  * @test
  */
 public function thatCommentCreatesWithAttachments()
 {
     $author = $this->createAuthor();
     $ticket = $this->createDummyTicket();
     $this->messageReferenceRepository->expects($this->once())->method('getReferenceByMessageId')->with($this->equalTo(self::DUMMY_MESSAGE_ID))->will($this->returnValue($this->messageReference));
     $this->messageReference->expects($this->once())->method('getTicket')->will($this->returnValue($ticket));
     $this->commentFactory->expects($this->once())->method('create')->with($this->equalTo(self::DUMMY_COMMENT_CONTENT), $this->equalTo($ticket), $this->equalTo($author))->will($this->returnValue($this->comment));
     $this->attachmentManager->expects($this->once())->method('createNewAttachment')->with($this->equalTo(self::DUMMY_FILENAME), $this->equalTo(self::DUMMY_FILE_CONTENT), $this->equalTo($this->comment));
     $this->ticketRepository->expects($this->once())->method('store')->with($this->equalTo($ticket));
     $this->messageReferenceService->createCommentForTicket(self::DUMMY_COMMENT_CONTENT, (string) $author, self::DUMMY_MESSAGE_ID, $this->attachments());
     $this->assertCount(1, $ticket->getComments());
     $this->assertEquals($this->comment, $ticket->getComments()->get(0));
 }
 /**
  * Post Comment for Ticket
  * @param Command\CommentCommand $command
  * @return Comment
  *
  * @throws ForbiddenException
  * @throws TicketNotFoundException
  */
 public function postNewCommentForTicket(Command\CommentCommand $command)
 {
     $this->isGranted('CREATE', 'Entity:DiamanteDeskBundle:Comment');
     \Assert\that($command->attachmentsInput)->nullOr()->all()->isInstanceOf('Diamante\\DeskBundle\\Api\\Dto\\AttachmentInput');
     /**
      * @var $ticket \Diamante\DeskBundle\Model\Ticket\Ticket
      */
     $ticket = $this->loadTicketBy($command->ticket);
     $author = User::fromString($command->author);
     $comment = $this->commentFactory->create($command->content, $ticket, $author, $command->private);
     $this->createAttachments($command, $comment);
     $ticket->updateStatus(new Status($command->ticketStatus));
     $ticket->postNewComment($comment);
     $this->ticketRepository->store($ticket);
     $this->dispatchWorkflowEvent($this->registry, $this->dispatcher, $comment);
     return $comment;
 }
 /**
  * @test
  */
 public function thatCommentPostsWithWithAttachments()
 {
     $ticket = $this->_dummyTicket;
     $author = $this->createDiamanteUser();
     $comment = new Comment(self::DUMMY_COMMENT_CONTENT, $ticket, $author, false);
     $this->ticketRepository->expects($this->once())->method('get')->with($this->equalTo(self::DUMMY_TICKET_ID))->will($this->returnValue($ticket));
     $this->commentFactory->expects($this->once())->method('create')->with($this->equalTo(self::DUMMY_COMMENT_CONTENT), $this->equalTo($ticket), $this->equalTo($author))->will($this->returnValue($comment));
     $this->ticketRepository->expects($this->once())->method('store')->with($this->equalTo($ticket));
     $attachmentInputs = $this->attachmentInputs();
     $this->attachmentManager->expects($this->exactly(count($attachmentInputs)))->method('createNewAttachment')->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING), $this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING), $this->equalTo($comment));
     $this->authorizationService->expects($this->once())->method('isActionPermitted')->with($this->equalTo('CREATE'), $this->equalTo('Entity:DiamanteDeskBundle:Comment'))->will($this->returnValue(true));
     $command = new CommentCommand();
     $command->content = self::DUMMY_COMMENT_CONTENT;
     $command->ticket = self::DUMMY_TICKET_ID;
     $command->author = $author;
     $command->ticketStatus = Status::IN_PROGRESS;
     $command->attachmentsInput = $attachmentInputs;
     $this->service->postNewCommentForTicket($command);
     $this->assertCount(1, $ticket->getComments());
     $this->assertEquals($comment, $ticket->getComments()->get(0));
 }
 /**
  * Creates Comment for Ticket
  *
  * @param $content
  * @param $authorId
  * @param $messageId
  * @param array $attachments
  * @return Ticket|null
  */
 public function createCommentForTicket($content, $authorId, $messageId, array $attachments = null)
 {
     $reference = $this->messageReferenceRepository->getReferenceByMessageId($messageId);
     if (is_null($reference)) {
         $this->logger->error(sprintf('Ticket not found for message: %s', $messageId));
         throw new \RuntimeException('Ticket loading failed, ticket not found.');
     }
     $ticket = $reference->getTicket();
     $author = User::fromString($authorId);
     if (empty($content)) {
         return null;
     }
     $comment = $this->commentFactory->create($content, $ticket, $author);
     if ($attachments) {
         $this->createAttachments($attachments, $comment);
     }
     $ticket->postNewComment($comment);
     $this->ticketRepository->store($ticket);
     $this->dispatchEvents($ticket);
     return $ticket;
 }