GeospatialTest.php 3.64 KB
Newer Older
1
<?php
Divine's avatar
Divine committed
2

Simon Schaufelberger's avatar
Simon Schaufelberger committed
3
declare(strict_types=1);
4 5 6

class GeospatialTest extends TestCase
{
Stas's avatar
Stas committed
7
    public function setUp(): void
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    {
        parent::setUp();

        Schema::collection('locations', function ($collection) {
            $collection->geospatial('location', '2dsphere');
        });

        Location::create([
            'name' => 'Picadilly',
            'location' => [
                'type' => 'LineString',
                'coordinates' => [
                    [
                        -0.1450383,
                        51.5069158,
                    ],
                    [
                        -0.1367563,
                        51.5100913,
                    ],
                    [
                        -0.1304123,
                        51.5112908,
                    ],
                ],
            ],
        ]);

        Location::create([
            'name' => 'StJamesPalace',
            'location' => [
                'type' => 'Point',
                'coordinates' => [
                    -0.139827,
                    51.504736,
                ],
            ],
        ]);
    }

Stas's avatar
Stas committed
48
    public function tearDown(): void
49 50 51 52 53 54 55 56 57
    {
        Schema::drop('locations');
    }

    public function testGeoWithin()
    {
        $locations = Location::where('location', 'geoWithin', [
            '$geometry' => [
                'type' => 'Polygon',
Jens Segers's avatar
Jens Segers committed
58
                'coordinates' => [
59
                    [
Jens Segers's avatar
Jens Segers committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
                        [
                            -0.1450383,
                            51.5069158,
                        ],
                        [
                            -0.1367563,
                            51.5100913,
                        ],
                        [
                            -0.1270247,
                            51.5013233,
                        ],
                        [
                            -0.1460866,
                            51.4952136,
                        ],
                        [
                            -0.1450383,
                            51.5069158,
                        ],
80
                    ],
Jens Segers's avatar
Jens Segers committed
81
                ],
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            ],
        ]);

        $this->assertEquals(1, $locations->count());

        $locations->get()->each(function ($item, $key) {
            $this->assertEquals('StJamesPalace', $item->name);
        });
    }

    public function testGeoIntersects()
    {
        $locations = Location::where('location', 'geoIntersects', [
            '$geometry' => [
                'type' => 'LineString',
                'coordinates' => [
                    [
                        -0.144044,
                        51.515215,
                    ],
                    [
                        -0.1367563,
                        51.5100913,
                    ],
                    [
                        -0.129545,
                        51.5078646,
                    ],
                ],
Jens Segers's avatar
Jens Segers committed
111
            ],
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        ]);

        $this->assertEquals(1, $locations->count());

        $locations->get()->each(function ($item, $key) {
            $this->assertEquals('Picadilly', $item->name);
        });
    }

    public function testNear()
    {
        $locations = Location::where('location', 'near', [
            '$geometry' => [
                'type' => 'Point',
                'coordinates' => [
                    -0.1367563,
                    51.5100913,
                ],
            ],
            '$maxDistance' => 50,
        ]);

        $locations = $locations->get();

        $this->assertEquals(1, $locations->count());

        $locations->each(function ($item, $key) {
            $this->assertEquals('Picadilly', $item->name);
        });
    }
}