/**
  * @dataProvider getCountQueryBuilderDataProvider
  * @param QueryBuilder $queryBuilder
  * @param string $expectedDql
  */
 public function testGetCountQueryBuilder(QueryBuilder $queryBuilder, $expectedDql)
 {
     $optimizer = new CountQueryBuilderOptimizer();
     $countQb = $optimizer->getCountQueryBuilder($queryBuilder);
     $this->assertInstanceOf('Doctrine\\ORM\\QueryBuilder', $countQb);
     // Check for expected DQL
     $this->assertEquals($expectedDql, $countQb->getQuery()->getDQL());
     // Check that Optimized DQL can be converted to SQL
     $this->assertNotEmpty($countQb->getQuery()->getSQL());
 }
 protected function setUp()
 {
     $this->optimizer = $this->getMock('Oro\\Bundle\\BatchBundle\\ORM\\QueryBuilder\\CountQueryBuilderOptimizer');
     $this->optimizer->expects($this->any())->method('getCountQueryBuilder')->with($this->isInstanceOf('Doctrine\\ORM\\QueryBuilder'))->willReturnArgument(0);
     $this->handler = $this->getMockBuilder('Oro\\Bundle\\SoapBundle\\Handler\\TotalHeaderHandler')->setConstructorArgs([$this->optimizer])->setMethods(['calculateCount'])->getMock();
     $configuration = $this->getMockBuilder('Doctrine\\ORM\\Configuration')->disableOriginalConstructor()->getMock();
     $configuration->expects($this->any())->method('getDefaultQueryHints')->will($this->returnValue([]));
     $configuration->expects($this->any())->method('isSecondLevelCacheEnabled')->will($this->returnValue(false));
     $this->em = $this->getMockBuilder('Doctrine\\ORM\\EntityManager')->disableOriginalConstructor()->getMock();
     $this->em->expects($this->any())->method('getConfiguration')->will($this->returnValue($configuration));
 }
Exemple #3
0
 /**
  * Calculates count
  *
  * @return int
  */
 public function computeNbResult()
 {
     $countQb = $this->countQueryBuilderOptimizer->getCountQueryBuilder($this->getQueryBuilder());
     $query = $countQb->getQuery();
     if (!$this->skipAclCheck) {
         $query = $this->aclHelper->apply($query);
     }
     $useWalker = null;
     if ($this->skipCountWalker !== null) {
         $useWalker = !$this->skipCountWalker;
     }
     return QueryCountCalculator::calculateCount($query, $useWalker);
 }
 /**
  * @dataProvider getCountQueryBuilderDataProvider
  *
  * @param callback $queryBuilder
  * @param string   $expectedDql
  */
 public function testGetCountQueryBuilder($queryBuilder, $expectedDql)
 {
     $listener = new CountQueryOptimizationListener();
     $eventDispatcher = new EventDispatcher();
     $eventDispatcher->addListener(CountQueryOptimizationEvent::EVENT_NAME, [$listener, 'onOptimize']);
     $optimizer = new CountQueryBuilderOptimizer();
     $optimizer->setEventDispatcher($eventDispatcher);
     $countQb = $optimizer->getCountQueryBuilder(call_user_func($queryBuilder, $this->em));
     $this->assertInstanceOf('Doctrine\\ORM\\QueryBuilder', $countQb);
     // Check for expected DQL
     $this->assertEquals($expectedDql, $countQb->getQuery()->getDQL());
     // Check that Optimized DQL can be converted to SQL
     $this->assertNotEmpty($countQb->getQuery()->getSQL());
 }
 /**
  * @param mixed $query
  *
  * @return int
  */
 protected function calculateTotalCount($query)
 {
     if ($query instanceof QueryBuilder) {
         $countQuery = $this->countQueryBuilderOptimizer->getCountQueryBuilder($query)->getQuery();
     } elseif ($query instanceof Query) {
         $countQuery = $this->cloneQuery($query)->setMaxResults(null)->setFirstResult(null);
     } elseif ($query instanceof SqlQueryBuilder) {
         $countQuery = $this->cloneQuery($query)->setMaxResults(null)->setFirstResult(null)->getQuery();
     } elseif ($query instanceof SqlQuery) {
         $countQuery = $this->cloneQuery($query)->getQueryBuilder()->setMaxResults(null)->setFirstResult(null);
     } else {
         throw new \InvalidArgumentException(sprintf('Expected instance of Doctrine\\ORM\\QueryBuilder, Doctrine\\ORM\\Query' . ', Oro\\Bundle\\EntityBundle\\ORM\\SqlQueryBuilder' . ' or Oro\\Bundle\\EntityBundle\\ORM\\SqlQuery, "%s" given.', is_object($query) ? get_class($query) : gettype($query)));
     }
     return QueryCountCalculator::calculateCount($countQuery);
 }
 /**
  * {@inheritdoc}
  */
 public function handle(Context $context)
 {
     if ($context->has('totalCount')) {
         $totalCount = $context->get('totalCount');
         if (!is_callable($totalCount)) {
             throw new \InvalidArgumentException(sprintf('Expected callable for totalCount, "%s" given', is_object($totalCount) ? get_class($totalCount) : gettype($totalCount)));
         }
         $totalCount = call_user_func($totalCount);
         if (!is_int($totalCount)) {
             throw new \InvalidArgumentException(sprintf('Expected integer as result of totalCount callable, "%s" given', is_object($totalCount) ? get_class($totalCount) : gettype($totalCount)));
         }
     } else {
         if ($context->has('query')) {
             $value = $context->get('query');
             if ($value instanceof QueryBuilder) {
                 $countQb = $this->countQueryBuilderOptimizer->getCountQueryBuilder($value);
                 $query = $countQb->getQuery();
             } elseif ($value instanceof Query) {
                 $query = clone $value;
                 $query->setMaxResults(null)->setFirstResult(null);
             } elseif ($value instanceof SqlQueryBuilder) {
                 $query = clone $value;
                 $query->setMaxResults(null)->setFirstResult(null);
                 $query = $query->getQuery();
             } elseif ($value instanceof SqlQuery) {
                 $query = clone $value;
                 $query->getQueryBuilder()->setMaxResults(null)->setFirstResult(null);
             } else {
                 throw new \InvalidArgumentException(sprintf('Expected instance of Doctrine\\ORM\\QueryBuilder, Doctrine\\ORM\\Query' . ', Oro\\Component\\DoctrineUtils\\ORM\\SqlQueryBuilder' . ' or Oro\\Component\\DoctrineUtils\\ORM\\SqlQuery, "%s" given', is_object($value) ? get_class($value) : gettype($value)));
             }
         } else {
             $qb = $context->getController()->getManager()->getRepository()->createQueryBuilder('e');
             $query = $qb->getQuery();
         }
         $totalCount = $this->calculateCount($query);
     }
     $context->getResponse()->headers->set(self::HEADER_NAME, $totalCount);
 }
 protected function setUp()
 {
     $this->optimizer = $this->getMock('Oro\\Bundle\\BatchBundle\\ORM\\QueryBuilder\\CountQueryBuilderOptimizer');
     $this->optimizer->expects($this->any())->method('getCountQueryBuilder')->with($this->isInstanceOf('Doctrine\\ORM\\QueryBuilder'))->willReturnArgument(0);
     $this->handler = $this->getMockBuilder('Oro\\Bundle\\SoapBundle\\Handler\\TotalHeaderHandler')->setConstructorArgs([$this->optimizer])->setMethods(['calculateCount'])->getMock();
 }