* Illuminate\Database\Query\Builder 를 extends 하여 기능 추가 * insert, update, delete, select 동작할 때 dynamic, proxy 사용여부에 따라 추가 기능 사용 * proxy 사용할 때 ProxyManager 에 등록된 Proxy 동작 * dynamic 을 사용하지 않으면 Illuminate Builder 직접 사용
Author: XE Developers (developers@xpressengine.com)
Inheritance: extends Illuminate\Database\Query\Builder
 /**
  * test delete
  *
  * @return void
  */
 public function testDelete()
 {
     $conn = $this->conn;
     $repo = new DocumentRepository($conn);
     $doc = $this->getDocumentEntity();
     $config = $this->getConfigEntity();
     $config->shouldReceive('get')->with('division')->andReturn(true);
     $config->shouldReceive('get')->with('instanceId')->andReturn('instanceId');
     $this->query->shouldReceive('delete')->andReturn(1);
     $this->query->shouldReceive('where')->andReturn($this->query);
     $result = $repo->delete($doc, $config);
     $this->assertEquals(1, $result);
     $result = $repo->deleteByInstanceId('instanceId');
     $this->assertEquals(1, $result);
 }
 /**
  * test insert
  *
  * @return void
  */
 public function testInsert()
 {
     $conn = $this->conn;
     $revisionManager = $this->revisionManager;
     $keygen = $this->keygen;
     $repo = m::mock('Xpressengine\\Document\\Repositories\\RevisionRepository', [$conn, $revisionManager, $keygen])->shouldAllowMockingProtectedMethods()->makePartial();
     $doc = $this->getDocumentEntity();
     $doc->shouldReceive('getAttributes')->andReturn([]);
     $doc->id = 'documentId';
     $config = $this->getConfigEntity();
     $config->shouldReceive('get')->with('group')->andReturn('document-instanceId');
     $repo->shouldReceive('nextNo')->andReturn(1);
     $keygen->shouldReceive('generate')->andReturn('revisionId');
     $revisionManager->shouldReceive('getHandler')->andReturn($dynamicFieldHandler = m::mock('Xpressengine\\DynamicField\\DynamicFieldHandler'));
     $dynamicFieldHandler->shouldReceive('getConfigHandler')->andReturn($dfConfigHandler = m::mock('Xpressengine\\DynamicField\\ConfigHandler'));
     $dfConfigHandler->shouldReceive('gets')->andReturn([]);
     $this->query->shouldReceive('insert');
     $revisionManager->shouldReceive('add');
     $result = $repo->insert($doc, $config);
     $this->assertEquals($doc, $result);
 }
 /**
  * test query builder interface without proxy, dynamic
  *
  * @return void
  */
 public function testWithDynamicAndProxy()
 {
     $params = ['some' => 'some'];
     $processor = m::mock('Illuminate\\Database\\Query\\Processors\\Processor');
     $processor->shouldReceive('processSelect')->andReturn([['id' => 1]]);
     $processor->shouldReceive('processInsertGetId')->andReturn(1);
     $grammar = m::mock('Illuminate\\Database\\Query\\Grammars\\Grammar');
     $grammar->shouldReceive('compileInsert')->andReturn('insert into table');
     $grammar->shouldReceive('compileInsertGetId')->andReturn('insert into table');
     $grammar->shouldReceive('compileUpdate')->andReturn('update table set');
     $grammar->shouldReceive('compileDelete')->andReturn('delete from table');
     $grammar->shouldReceive('compileSelect')->andReturn('select * from table');
     $schemaBuilder = m::mock('Illuminate\\Database\\Schema\\Builder');
     $schemaBuilder->shouldReceive('getColumnListing')->andReturn(['some']);
     $connection = m::mock('Illuminate\\Database\\Connection');
     $connection->shouldReceive('getPostProcessor')->andReturn($processor);
     $connection->shouldReceive('getQueryGrammar')->andReturn($grammar);
     $connection->shouldReceive('getSchemaBuilder')->andReturn($schemaBuilder);
     $proxyManager = m::mock('Xpressengine\\Database\\ProxyManager');
     $proxyManager->shouldReceive('set');
     $proxyManager->shouldReceive('insert');
     $proxyManager->shouldReceive('update');
     $proxyManager->shouldReceive('delete');
     $proxyManager->shouldReceive('wheres');
     $proxyManager->shouldReceive('orders');
     $connector = m::mock('Xpressengine\\Database\\VirtualConnection');
     $connector->shouldReceive('getDefaultConnection')->andReturn($connection);
     $connector->shouldReceive('getSchemaBuilder')->andReturn($schemaBuilder);
     $connector->shouldReceive('getProxyManager')->andReturn($proxyManager);
     $connector->shouldReceive('insert')->andReturn(true);
     $connector->shouldReceive('insertGetId')->andReturn(1);
     $connector->shouldReceive('update')->andReturn(1);
     $connector->shouldReceive('delete')->andReturn(1);
     $connector->shouldReceive('select')->andReturn(1);
     $connector->shouldReceive('getSchema')->andReturn($params);
     /** @var \Xpressengine\Database\VirtualConnection $connector */
     $query = new DynamicQuery($connector, $grammar, $processor);
     $query->useProxy(true);
     $this->assertEquals(true, $query->insert($params));
     $this->assertEquals(1, $query->insertGetId($params));
     $this->assertEquals(true, $query->update($params));
     $this->assertEquals(true, $query->delete($params));
     $this->assertInstanceOf('Xpressengine\\Database\\DynamicQuery', $query->where([]));
     $proxyManager->shouldReceive('get');
     $proxyManager->shouldReceive('first');
     $this->assertEquals([['id' => 1]], $query->get());
     $this->assertEquals(['id' => 1], $query->first());
 }
 /**
  * $query 에 join 된 쿼리를 리턴
  *
  * @param DynamicQuery $query query builder
  * @return Builder
  */
 public function joinRevision(DynamicQuery $query)
 {
     $config = $this->config;
     $tableName = $query->from;
     $table = $this->handler->getConfigHandler()->getRevisionTableName($config);
     if ($query->hasDynamicTable($table)) {
         return $query;
     }
     $query->leftJoin($table, function (JoinClause $join) use($tableName, $table, $config) {
         $join->on(sprintf('%s.%s', $tableName, $config->get('joinColumnName')), '=', sprintf('%s.dynamicFieldTargetId', $table))->on(sprintf('%s.revisionId', $tableName), '=', sprintf('%s.revisionId', $table));
     });
     $query->setDynamicTable($table);
     return $query;
 }
 /**
  * For period search
  *
  * @param DynamicQuery $query xe database query builder
  * @param string       $since since datetime
  * @param string       $until until datetime
  * @return void
  */
 private function periodWhere(DynamicQuery &$query, $since, $until)
 {
     if ($since !== null && $until !== null) {
         $query->whereBetween('used.createdAt', [$since, $until]);
     } elseif ($since !== null) {
         $query->where('used.createdAt', '>', $since);
     } elseif ($until !== null) {
         $query->where('used.createdAt', '<', $until);
     }
 }
Example #6
0
 /**
  * get proxy manager
  *
  * @return ProxyManager|null
  */
 public function getProxyManager()
 {
     return $this->query->getProxyManager();
 }