function let(DocumentManager $manager, ChannelInterface $ecommerce, ChannelInterface $mobile, LocaleInterface $enUs, LocaleInterface $frFr, CategoryInterface $category, ChannelManager $channelManager, CategoryRepositoryInterface $categoryRepository, ProductRepository $productRepository, QueryBuilder $ormQb, Builder $odmQb, Query $odmQuery, Cursor $cursor) { $enUs->getCode()->willReturn('en_US'); $frFr->getCode()->willReturn('fr_FR'); $ecommerce->getCode()->willReturn('ecommerce'); $ecommerce->getLabel()->willReturn('ECommerce'); $ecommerce->getLocales()->willReturn(array($enUs, $frFr)); $ecommerce->getCategory()->willReturn($category); $mobile->getCode()->willReturn('mobile'); $mobile->getLabel()->willReturn('Mobile'); $mobile->getLocales()->willReturn(array($enUs)); $mobile->getCategory()->willReturn($category); $odmQuery->execute()->willReturn($cursor); $productRepository->createQueryBuilder()->willReturn($odmQb); $odmQb->hydrate(Argument::any())->willReturn($odmQb); $odmQb->field(Argument::any())->willReturn($odmQb); $odmQb->in(Argument::any())->willReturn($odmQb); $odmQb->equals(Argument::any())->willReturn($odmQb); $odmQb->select('_id')->willReturn($odmQb); $odmQb->getQuery()->willReturn($odmQuery); $categoryRepository->getAllChildrenQueryBuilder($category, true)->willReturn($ormQb); $categoryRepository->getCategoryIds($category, $ormQb)->willReturn(array(1, 2, 3)); $channelManager->getFullChannels()->willReturn(array($ecommerce, $mobile)); $manager->getRepository('pim_product_class')->willReturn($productRepository); $this->beConstructedWith($manager, $channelManager, $categoryRepository, 'pim_product_class'); }
public function testGeoNearOptionsArePassed() { $collection = $this->getMockCollection(); $queryArray = array('type' => Query::TYPE_GEO_LOCATION, 'geoNear' => array('near' => array(50, 50), 'distanceMultiplier' => 2.5, 'maxDistance' => 5, 'spherical' => true), 'limit' => 10, 'query' => array('altitude' => array('$gt' => 1))); $query = new Query($this->getMockDatabase(), $collection, $queryArray, array(), ''); $collection->expects($this->any())->method('geoNear')->with(array(50, 50), array('altitude' => array('$gt' => 1)), $this->logicalAnd(new ArrayHasValueUnderKey('distanceMultiplier', 2.5), new ArrayHasValueUnderKey('maxDistance', 5), new ArrayHasValueUnderKey('spherical', true), new ArrayHasValueUnderKey('num', 10))); $query->execute(); }
function it_reads_mongodb_records_one_by_one(Query $query, Cursor $cursor) { $query->execute()->willReturn($cursor); $result = ['foo', 'bar']; $cursor->getNext()->shouldBeCalled(); $cursor->current()->willReturn(array_shift($result)); $cursor->next()->will(function () use($cursor, &$result) { $cursor->current()->willReturn(array_shift($result)); }); $this->setQuery($query); $this->read()->shouldReturn('foo'); $this->read()->shouldReturn('bar'); $this->read()->shouldReturn(null); }
public function testEagerCursorPreparation() { $cursor = $this->getMockCursor(); $collection = $this->getMockCollection(); $collection->expects($this->once())->method('find')->with(array('foo' => 'bar'))->will($this->returnValue($cursor)); $queryArray = array('type' => Query::TYPE_FIND, 'query' => array('foo' => 'bar'), 'eagerCursor' => true); $query = new Query($collection, $queryArray, array()); $eagerCursor = $query->execute(); $this->assertInstanceOf('Doctrine\\MongoDB\\EagerCursor', $eagerCursor); $this->assertSame($cursor, $eagerCursor->getCursor()); }
/** * Execute the query and returns the results. * * @return mixed */ public function execute() { $uow = $this->dm->getUnitOfWork(); if ($this->isIndexRequired() && !$this->isIndexed()) { throw MongoDBException::queryNotIndexed($this->class->name, $this->getUnindexedFields()); } $results = parent::execute(); $hints = array(); if ($this->refresh) { $hints[self::HINT_REFRESH] = true; } if ($this->query['slaveOkay'] === true) { $hints[self::HINT_SLAVE_OKAY] = true; } // Unwrap the BaseEagerCursor if ($results instanceof BaseEagerCursor) { $results = $results->getCursor(); } // Convert the regular mongodb cursor to the odm cursor if ($results instanceof BaseCursor) { $results = $this->wrapCursor($results, $hints); } // Wrap odm cursor with EagerCursor if true if ($this->query['eagerCursor'] === true) { $results = new EagerCursor($results, $this->dm->getUnitOfWork(), $this->class); } // GeoLocationFindQuery just returns an instance of ArrayIterator so we have to // iterator over it and hydrate each object. if ($this->query['type'] === self::TYPE_GEO_LOCATION && $this->hydrate) { foreach ($results as $key => $result) { $document = $result['obj']; if ($this->class->distance) { $document[$this->class->distance] = $result['dis']; } $results[$key] = $uow->getOrCreateDocument($this->class->name, $document, $hints); } $results->reset(); } if ($this->primers) { $documentPersister = $this->dm->getUnitOfWork()->getDocumentPersister($this->class->name); foreach ($this->primers as $fieldName => $primer) { if ($primer) { $documentPersister->primeCollection($results, $fieldName, $primer, $hints); } } } if ($this->hydrate && is_array($results) && isset($results['_id'])) { // Convert a single document array to a document object $results = $uow->getOrCreateDocument($this->class->name, $results, $hints); } return $results; }
/** * Execute the query and returns the results. * * @return mixed */ public function execute() { $uow = $this->dm->getUnitOfWork(); $results = parent::execute(); // Convert the regular mongodb cursor to the odm cursor if ($results instanceof BaseCursor) { $cursor = $results->getMongoCursor(); $results = new Cursor($cursor, $this->dm->getUnitOfWork(), $this->class); $results->hydrate($this->hydrate); $results->refresh($this->refresh); } $hints = array(); if ($this->refresh) { $hints[self::HINT_REFRESH] = true; } // GeoLocationFindQuery just returns an instance of ArrayIterator so we have to // iterator over it and hydrate each object. if ($this->query['type'] === self::TYPE_GEO_LOCATION && $this->hydrate) { foreach ($results as $key => $result) { $document = $result['obj']; if ($this->class->distance) { $document[$this->class->distance] = $result['dis']; } $results[$key] = $uow->getOrCreateDocument($this->class->name, $document, $hints); } $results->reset(); } if ($this->hydrate && is_array($results) && isset($results['_id'])) { // Convert a single document array to a document object $results = $uow->getOrCreateDocument($this->class->name, $results, $hints); } return $results; }
/** * Prepare the Cursor returned by {@link Query::execute()}. * * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, * and set the hydrate option and UnitOfWork hints. This occurs in addition * to any preparation done by the base Query class. * * @see \Doctrine\MongoDB\Cursor::prepareCursor() * @param BaseCursor $cursor * @return Cursor|EagerCursor */ protected function prepareCursor(BaseCursor $cursor) { $cursor = parent::prepareCursor($cursor); // Unwrap a base EagerCursor if ($cursor instanceof BaseEagerCursor) { $cursor = $cursor->getCursor(); } // Convert the base Cursor into an ODM Cursor $cursor = new Cursor($cursor, $this->dm->getUnitOfWork(), $this->class); // Wrap ODM Cursor with EagerCursor if (!empty($this->query['eagerCursor'])) { $cursor = new EagerCursor($cursor, $this->dm->getUnitOfWork(), $this->class); } $cursor->hydrate($this->hydrate); $cursor->setHints($this->unitOfWorkHints); return $cursor; }
public function testSpecifyMaxTimeMSOnCursor() { $cursor = $this->getMockCursor(); $collection = $this->getMockCollection(); $collection->expects($this->once())->method('find')->with($this->equalTo(['foo' => 'bar']))->will($this->returnValue($cursor)); $cursor->expects($this->once())->method('maxTimeMS')->with($this->equalTo(30000))->will($this->returnValue($cursor)); $queryArray = ['type' => Query::TYPE_FIND, 'query' => ['foo' => 'bar'], 'maxTimeMS' => 30000]; $query = new Query($collection, $queryArray, []); $this->assertSame($cursor, $query->execute()); }
public function testUseIdentifierKeys() { $cursor = $this->getMockCursor(); $collection = $this->getMockCollection(); $collection->expects($this->once())->method('find')->with(array('foo' => 'bar'))->will($this->returnValue($cursor)); $cursor->expects($this->once())->method('setUseIdentifierKeys')->with(false)->will($this->returnValue($cursor)); $queryArray = array('type' => Query::TYPE_FIND, 'query' => array('foo' => 'bar'), 'useIdentifierKeys' => false); $query = new Query($collection, $queryArray, array()); $this->assertSame($cursor, $query->execute()); }
/** * Prepare the Cursor returned by {@link Query::execute()}. * * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, * and set the hydrate option and UnitOfWork hints. This occurs in addition * to any preparation done by the base Query class. * * @see \Doctrine\MongoDB\Cursor::prepareCursor() * @param BaseCursor $cursor * @return CursorInterface */ protected function prepareCursor(BaseCursor $cursor) { $cursor = parent::prepareCursor($cursor); // Convert the base Cursor into an ODM Cursor $cursorClass = !empty($this->query['eagerCursor']) ? 'Doctrine\\ODM\\MongoDB\\EagerCursor' : 'Doctrine\\ODM\\MongoDB\\Cursor'; $cursor = new $cursorClass($cursor, $this->dm->getUnitOfWork(), $this->class); $cursor->hydrate($this->hydrate); $cursor->setHints($this->unitOfWorkHints); if (!empty($this->primers)) { $referencePrimer = new ReferencePrimer($this->dm, $this->dm->getUnitOfWork()); $cursor->enableReferencePriming($this->primers, $referencePrimer); } return $cursor; }