/** * @dataProvider providePoint */ public function testNear($point, array $near, $spherical) { $results = array(array('dis' => 1, 'obj' => array('_id' => 1, 'loc' => array(1, 0))), array('dis' => 2, 'obj' => array('_id' => 2, 'loc' => array(2, 0)))); $command = array('geoNear' => self::collectionName, 'near' => $near, 'spherical' => $spherical, 'query' => new \stdClass()); $commandResult = array('ok' => 1, 'results' => $results); $database = $this->getMockDatabase(); $database->expects($this->once())->method('command')->with($command)->will($this->returnValue($commandResult)); $coll = $this->getTestCollection($database); $result = $coll->near($point); $arrayIterator = new ArrayIterator($results); $arrayIterator->setCommandResult($commandResult); $this->assertEquals($arrayIterator, $result); }
/** * Execute the geoNear command. * * @see Collection::near() * @param array|Point $near * @param array $query * @param array $options * @return ArrayIterator * @throws ResultException if the command fails */ protected function doNear($near, array $query, array $options) { $options = isset($options['timeout']) ? $this->convertSocketTimeout($options) : $options; if ($near instanceof Point) { $near = $near->jsonSerialize(); } $command = array(); $command['geoNear'] = $this->mongoCollection->getName(); $command['near'] = $near; $command['spherical'] = isset($near['type']); $command['query'] = (object) $query; $command = array_merge($command, $options); $database = $this->database; $result = $this->retry(function () use($database, $command) { return $database->command($command); }); if (empty($result['ok'])) { throw new ResultException($result); } $arrayIterator = new ArrayIterator(isset($result['results']) ? $result['results'] : array()); $arrayIterator->setCommandResult($result); return $arrayIterator; }
public function testFirstAndLast() { $arrayIterator = new ArrayIterator(array(1, 2, 3)); $this->assertSame(1, $arrayIterator->first()); $this->assertSame(3, $arrayIterator->last()); }