/**
  * @param StaticSegment $staticSegment
  *
  * @return \Iterator|BufferedQueryResultIterator
  */
 protected function createSubordinateIterator($staticSegment)
 {
     $qb = $this->getIteratorQueryBuilder($staticSegment);
     $alias = sprintf('%s.id', MarketingListQueryBuilderAdapter::MEMBER_ALIAS);
     $qb->select([MarketingListQueryBuilderAdapter::MEMBER_ALIAS . '.id member_id', $staticSegment->getId() . ' static_segment_id', $qb->expr()->literal(StaticSegmentMember::STATE_ADD) . ' state'])->leftJoin(sprintf('%s.segmentMembers', MarketingListQueryBuilderAdapter::MEMBER_ALIAS), 'segmentMembers', Join::WITH, $qb->expr()->eq('segmentMembers.staticSegment', $staticSegment->getId()))->andWhere($qb->expr()->andX($qb->expr()->isNull('segmentMembers.id'), $qb->expr()->isNotNull(sprintf('%s.originId', MarketingListQueryBuilderAdapter::MEMBER_ALIAS)), $qb->expr()->eq(sprintf('%s.subscribersList', MarketingListQueryBuilderAdapter::MEMBER_ALIAS), ':subscribersList')))->setParameter('subscribersList', $staticSegment->getSubscribersList())->orderBy($alias)->groupBy($alias);
     $bufferedIterator = new BufferedQueryResultIterator($qb);
     $bufferedIterator->setReverse(true);
     $bufferedIterator->setBufferSize(self::BUFFER_SIZE);
     return $bufferedIterator;
 }
 /**
  * @param StaticSegment $staticSegment
  *
  * @return \Iterator|BufferedQueryResultIterator
  */
 protected function createSubordinateIterator($staticSegment)
 {
     if (!$this->segmentMemberClassName) {
         throw new \InvalidArgumentException('StaticSegmentMember class name must be provided');
     }
     $qb = $this->getIteratorQueryBuilder($staticSegment)->select(MarketingListQueryBuilderAdapter::MEMBER_ALIAS . '.id');
     $segmentMembersQb = clone $qb;
     $segmentMembersQb->resetDQLParts()->select(['staticSegment.id static_segment_id', 'smmb.id member_id', $segmentMembersQb->expr()->literal(StaticSegmentMember::STATE_UNSUBSCRIBE_DELETE) . ' state'])->from($this->segmentMemberClassName, 'segmentMember')->join('segmentMember.member', 'smmb')->join('segmentMember.staticSegment', 'staticSegment')->where($qb->expr()->andX($qb->expr()->eq('staticSegment.id', $staticSegment->getId()), $segmentMembersQb->expr()->notIn('smmb.id', $qb->getDQL())));
     $bufferedIterator = new BufferedQueryResultIterator($segmentMembersQb);
     $bufferedIterator->setReverse(true);
     return $bufferedIterator;
 }
 /**
  * {@inheritdoc}
  */
 protected function createSubordinateIterator($subscribersList)
 {
     parent::assertSubscribersList($subscribersList);
     if (!$this->memberClassName) {
         throw new \InvalidArgumentException('Member id must be provided');
     }
     $qb = $this->doctrineHelper->getEntityManager($this->memberClassName)->getRepository($this->memberClassName)->createQueryBuilder('mmb');
     $qb->select('mmb')->join('mmb.subscribersList', 'subscribersList')->where($qb->expr()->andX($qb->expr()->eq('mmb.status', ':status'), $qb->expr()->eq('subscribersList.originId', ':originId')))->setParameters(['status' => Member::STATUS_EXPORT, 'originId' => $subscribersList->getOriginId()])->addOrderBy('subscribersList.id');
     $bufferedIterator = new BufferedQueryResultIterator($qb);
     $bufferedIterator->setReverse(true);
     return $bufferedIterator;
 }
 public function testIteratorInReverseDirection()
 {
     $records = [['a_0' => '1'], ['a_0' => '2'], ['a_0' => '3']];
     $actualSqls = [];
     $statementCounter = 0;
     $statements = [$this->createFetchStatementMock([['sclr_0' => count($records)]]), $this->createFetchStatementMock([$records[0], $records[1]]), $this->createFetchStatementMock([$records[2]])];
     $this->getDriverConnectionMock($this->em)->expects($this->any())->method('query')->will($this->returnCallback(function ($sql) use(&$statements, &$statementCounter, &$actualSqls) {
         $actualSqls[$statementCounter] = $sql;
         $statement = $statements[$statementCounter];
         $statementCounter++;
         return $statement;
     }));
     $source = $this->em->createQueryBuilder()->select('o')->from('Stub:Entity', 'o');
     $iterator = new BufferedQueryResultIterator($source);
     $iterator->setReverse(true);
     $iterator->setBufferSize(2);
     $this->assertEquals(count($records), $iterator->count());
     $count = 0;
     foreach ($iterator as $record) {
         $this->assertInstanceOf('Oro\\Bundle\\BatchBundle\\Tests\\Unit\\ORM\\Query\\Stub\\Entity', $record);
         $this->assertEquals($records[$count]['a_0'], $record->a);
         $count++;
     }
     $this->assertEquals(count($records), $count);
     $this->assertCount(3, $actualSqls);
     $this->assertEquals('SELECT count(e0_.a) AS sclr_0 FROM Entity e0_', $actualSqls[0]);
     $this->assertEquals('SELECT e0_.a AS a_0, e0_.b AS b_1 FROM Entity e0_ LIMIT 2 OFFSET 2', $actualSqls[1]);
     $this->assertEquals('SELECT e0_.a AS a_0, e0_.b AS b_1 FROM Entity e0_ LIMIT 2 OFFSET 0', $actualSqls[2]);
 }