function it_is_iterable($queryBuilder, EntityManager $entityManager, AbstractQuery $query, From $from, CursorableRepositoryInterface $repository) { $this->shouldImplement('\\Iterator'); $page1 = [new Entity(10), new Entity(11), new Entity(12), new Entity(13), new Entity(14), new Entity(15), new Entity(16), new Entity(17), new Entity(18), new Entity(19)]; $page2 = [new Entity(20), new Entity(21), new Entity(22)]; $data = array_merge($page1, $page2); $rootIdExpr = 'o.id'; $entityClass = 'Pim\\Bundle\\CatalogBundle\\Model\\Product'; $from->getFrom()->shouldBeCalled()->willReturn($entityClass); $from->getAlias()->shouldBeCalled()->willReturn('o'); $queryBuilder->getRootAliases()->willReturn(['o']); $queryBuilder->getDQLPart('from')->willReturn([$from]); $queryBuilder->select($rootIdExpr)->willReturn($queryBuilder); $queryBuilder->resetDQLPart('from')->willReturn($queryBuilder); $queryBuilder->from(Argument::any(), Argument::any(), $rootIdExpr)->willReturn($queryBuilder); $queryBuilder->groupBy($rootIdExpr)->willReturn($queryBuilder); $queryBuilder->getQuery()->willReturn($query); $query->getArrayResult()->willReturn([10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16, 17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22]); $entityManager->getRepository($entityClass)->willReturn($repository); $repository->findByIds([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])->willReturn($page1); $repository->findByIds([20, 21, 22])->willReturn($page2); // methods that not iterate can be called twice $this->rewind()->shouldReturn(null); $this->valid()->shouldReturn(true); $this->valid()->shouldReturn(true); $this->current()->shouldReturn($data[0]); $this->current()->shouldReturn($data[0]); $this->key()->shouldReturn(0); $this->key()->shouldReturn(0); // for each call sequence for 13 items $this->rewind()->shouldReturn(null); for ($i = 0; $i < 13; $i++) { if ($i > 0) { $this->next()->shouldReturn(null); } $this->valid()->shouldReturn(true); $this->current()->shouldReturn($data[$i]); $this->key()->shouldReturn($i); } $this->next()->shouldReturn(null); $this->valid()->shouldReturn(false); // check behaviour after the end of data $this->current()->shouldReturn(false); $this->key()->shouldReturn(null); }
function it_increments_read_count_each_time_it_reads($channelManager, $repository, $stepExecution, $channelManager, $repository, Channel $channel, From $from, QueryBuilder $queryBuilder, AbstractQuery $query, ProductInterface $sku1, ProductInterface $sku2, ProductInterface $sku3) { $channelManager->getChannelByCode('foobar')->willReturn($channel); $repository->buildByChannelAndCompleteness($channel)->willReturn($queryBuilder); $queryBuilder->getRootAliases()->willReturn(['root']); $queryBuilder->getDQLPart('from')->willReturn([$from]); $queryBuilder->select('root.id')->willReturn($queryBuilder); $queryBuilder->resetDQLPart('from')->willReturn($queryBuilder); $from->getFrom()->willReturn('from_table'); $from->getAlias()->willReturn('alias_table'); $queryBuilder->from('from_table', 'alias_table', 'root.id')->willReturn($queryBuilder); $queryBuilder->groupBy('root.id')->willReturn($queryBuilder); $queryBuilder->getQuery()->willReturn($query); $query->getArrayResult()->willReturn(array_flip([1, 33, 789])); $repository->findByIds([1, 33, 789])->willReturn([$sku1, $sku2, $sku3]); $this->setChannel('foobar'); $stepExecution->incrementSummaryInfo('read')->shouldBeCalledTimes(3); $this->setChannel('foobar'); $this->read(); $this->read(); $this->read(); $this->read(); }
/** * @group DDC-1686 */ public function testExpressionGetter() { // Andx $andx = new Expr\Andx(array('1 = 1', '2 = 2')); $this->assertEquals(array('1 = 1', '2 = 2'), $andx->getParts()); // Comparison $comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar'); $this->assertEquals('foo', $comparison->getLeftExpr()); $this->assertEquals('bar', $comparison->getRightExpr()); $this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator()); // From $from = new Expr\From('Foo', 'f', 'f.id'); $this->assertEquals('f', $from->getAlias()); $this->assertEquals('Foo', $from->getFrom()); $this->assertEquals('f.id', $from->getIndexBy()); // Func $func = new Expr\Func('MAX', array('f.id')); $this->assertEquals('MAX', $func->getName()); $this->assertEquals(array('f.id'), $func->getArguments()); // GroupBy $group = new Expr\GroupBy(array('foo DESC', 'bar ASC')); $this->assertEquals(array('foo DESC', 'bar ASC'), $group->getParts()); // Join $join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id'); $this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType()); $this->assertEquals(Expr\Join::ON, $join->getConditionType()); $this->assertEquals('b.bar_id = 1', $join->getCondition()); $this->assertEquals('b.bar_id', $join->getIndexBy()); $this->assertEquals('f.bar', $join->getJoin()); $this->assertEquals('b', $join->getAlias()); // Literal $literal = new Expr\Literal(array('foo')); $this->assertEquals(array('foo'), $literal->getParts()); // Math $math = new Expr\Math(10, '+', 20); $this->assertEquals(10, $math->getLeftExpr()); $this->assertEquals(20, $math->getRightExpr()); $this->assertEquals('+', $math->getOperator()); // OrderBy $order = new Expr\OrderBy('foo', 'DESC'); $this->assertEquals(array('foo DESC'), $order->getParts()); // Andx $orx = new Expr\Orx(array('foo = 1', 'bar = 2')); $this->assertEquals(array('foo = 1', 'bar = 2'), $orx->getParts()); // Select $select = new Expr\Select(array('foo', 'bar')); $this->assertEquals(array('foo', 'bar'), $select->getParts()); }
/** * Updates a query root corresponding to an entity setting its index by. This method is intended to be used with * EntityRepository->createQueryBuilder(), which creates the initial FROM clause and do not allow you to update it * setting an index by. * * <code> * $qb = $userRepository->createQueryBuilder('u') * ->indexBy('u', 'u.id'); * * // Is equivalent to... * * $qb = $em->createQueryBuilder() * ->select('u') * ->from('User', 'u', 'u.id'); * </code> * * @param string $alias The root alias of the class. * @param string $indexBy The index for the from. * * @return QueryBuilder This QueryBuilder instance. * * @throws Query\QueryException */ public function indexBy($alias, $indexBy) { $rootAliases = $this->getRootAliases(); if (!in_array($alias, $rootAliases)) { throw new Query\QueryException(sprintf('Specified root alias %s must be set before invoking indexBy().', $alias)); } foreach ($this->_dqlParts['from'] as &$fromClause) { if ($fromClause->getAlias() !== $alias) { continue; } $fromClause = new Expr\From($fromClause->getFrom(), $fromClause->getAlias(), $indexBy); } return $this; }
/** * Gets the root entities of the query. This is the entity aliases involved * in the construction of the query. * * <code> * $qb = $em->createQueryBuilder() * ->select('u') * ->from('User', 'u'); * * $qb->getRootEntities(); // array('User') * </code> * * @return array */ public function getRootEntities() { $entities = array(); foreach ($this->_dqlParts['from'] as &$fromClause) { if (is_string($fromClause)) { $spacePos = strrpos($fromClause, ' '); $from = substr($fromClause, 0, $spacePos); $alias = substr($fromClause, $spacePos + 1); $fromClause = new Query\Expr\From($from, $alias); } $entities[] = $fromClause->getFrom(); } return $entities; }
/** * @param \Doctrine\ORM\Query\Expr\From $from */ private function addFromExprToAliasMap(Expr\From $from) { $this->aliasToClassMap[$from->getAlias()] = $from->getFrom(); }