/**
  * test insert
  *
  * @return void
  */
 public function testInsert()
 {
     $conn = $this->conn;
     $repo = new DocumentRepository($conn);
     $doc = $this->getDocumentEntity();
     $doc->shouldReceive('getAttributes')->andReturn([]);
     $config = $this->getConfigEntity();
     $config->shouldReceive('get')->with('division')->andReturn(true);
     $config->shouldReceive('get')->with('instanceId')->andReturn('instanceId');
     $this->query->shouldReceive('insert');
     $result = $repo->insert($doc, $config);
     $this->assertInstanceOf('Xpressengine\\Document\\DocumentEntity', $result);
 }
 /**
  * insert document
  *
  * @param DocumentEntity $doc    document entity
  * @param ConfigEntity   $config config entity
  * @return DocumentEntity
  */
 public function insert(DocumentEntity $doc, ConfigEntity $config)
 {
     if ($doc->id == null) {
         throw new Exceptions\RequiredValueException();
     }
     if ($doc->writer == null) {
         throw new Exceptions\RequiredValueException();
     }
     // set default values
     if ($doc->userType == null) {
         $doc->userType = $doc::USER_TYPE_USER;
     }
     if ($doc->approved == null) {
         $doc->approved = $doc::APPROVED_APPROVED;
     }
     if ($doc->published == null) {
         $doc->published = $doc::PUBLISHED_PUBLISHED;
     }
     if ($doc->status == null) {
         $doc->status = $doc::STATUS_PUBLIC;
     }
     if ($doc->display == null) {
         $doc->display = $doc::DISPLAY_VISIBLE;
     }
     if ($doc->createdAt == null) {
         $doc->createdAt = date('Y-m-d H:i:s');
     }
     if ($doc->updatedAt == null) {
         $doc->updatedAt = $doc->createdAt;
     }
     if ($doc->published == 'published' && $doc->publishedAt == null) {
         $doc->publishedAt = $doc->createdAt;
     }
     if ($doc->langCode == null) {
         $doc->langCode = 'default';
     }
     $timestamp = time();
     if ($doc->parentId == null || $doc->parentId == '') {
         $doc->head = $timestamp . '-' . $doc->id;
     } else {
         // for 계층 모델
         if ($doc->parentId !== $doc->id) {
             $parent = $this->findById($doc->parentId);
             if ($parent === null) {
                 throw new Exceptions\DocumentNotExistsException();
             }
             $parent = new DocumentEntity($parent);
             $lastChar = $this->document->getLastChildReply($parent, $this->reply->getReplyCharLen());
             $this->reply->setReply($doc, $parent, $lastChar);
             $doc->head = $parent->head;
         }
     }
     $doc->listOrder = $doc->head . (isset($doc->reply) ? $doc->reply : '');
     $this->connection->beginTransaction();
     $doc = $this->document->insert($doc, $config);
     if ($config->get('revision') === true && $this->revision->isChanged($doc) === true) {
         $this->revision->insert($doc, $config);
     }
     $this->connection->commit();
     return $doc;
 }