|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\ODM\MongoDB\Tests\Functional; |
| 6 | + |
| 7 | +use Doctrine\ODM\MongoDB\Tests\BaseTestCase; |
| 8 | +use Documents\Place; |
| 9 | + |
| 10 | +class GeoTest extends BaseTestCase |
| 11 | +{ |
| 12 | + public function setUp(): void |
| 13 | + { |
| 14 | + parent::setUp(); |
| 15 | + |
| 16 | + $this->dm->getSchemaManager()->ensureDocumentIndexes(Place::class); |
| 17 | + $this->dm->persist(new Place( |
| 18 | + name: 'Central Park', |
| 19 | + location: ['type' => 'Point', 'coordinates' => [-73.97, 40.77]], |
| 20 | + category: 'Parks', |
| 21 | + )); |
| 22 | + $this->dm->persist(new Place( |
| 23 | + name: 'Sara D. Roosevelt Park', |
| 24 | + location: ['type' => 'Point', 'coordinates' => [-73.9928, 40.7193]], |
| 25 | + category: 'Parks', |
| 26 | + )); |
| 27 | + $this->dm->persist(new Place( |
| 28 | + name: 'Polo Grounds', |
| 29 | + location: ['type' => 'Point', 'coordinates' => [-73.9928, 40.7193]], |
| 30 | + category: 'Stadiums', |
| 31 | + )); |
| 32 | + $this->dm->flush(); |
| 33 | + } |
| 34 | + |
| 35 | + public function testGeoNearWithQuery(): void |
| 36 | + { |
| 37 | + $pipeline = $this->dm->createAggregationBuilder(Place::class) |
| 38 | + ->geoNear(-73.99279, 40.719296) |
| 39 | + ->field('category')->equals('Parks') |
| 40 | + ->distanceField('calculated') |
| 41 | + ->maxDistance(2) |
| 42 | + ->spherical(true) |
| 43 | + ->getAggregation(); |
| 44 | + |
| 45 | + $results = $pipeline->execute()->toArray(); |
| 46 | + self::assertCount(2, $results); |
| 47 | + self::assertSame('Sara D. Roosevelt Park', $results[0]['name']); |
| 48 | + self::assertIsFloat($results[0]['calculated']); |
| 49 | + } |
| 50 | + |
| 51 | + public function testGeoNearWithEmptyQuery(): void |
| 52 | + { |
| 53 | + $pipeline = $this->dm->createAggregationBuilder(Place::class) |
| 54 | + ->geoNear(-73.99279, 40.719296) |
| 55 | + ->distanceField('dist.calculated') |
| 56 | + ->maxDistance(2) |
| 57 | + ->includeLocs('dist.location') |
| 58 | + ->spherical(true) |
| 59 | + ->getAggregation(); |
| 60 | + |
| 61 | + $results = $pipeline->execute()->toArray(); |
| 62 | + self::assertCount(3, $results); |
| 63 | + self::assertSame('Sara D. Roosevelt Park', $results[0]['name']); |
| 64 | + self::assertIsArray($results[0]['dist']); |
| 65 | + self::assertIsArray($results[0]['dist']['location']); |
| 66 | + self::assertIsFloat($results[0]['dist']['calculated']); |
| 67 | + } |
| 68 | +} |
0 commit comments