GeospatialTest.php 3.48 KB
Newer Older
1 2 3 4 5 6 7 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 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
<?php

class GeospatialTest extends TestCase
{
    public function setUp()
    {
        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,
                ],
            ],
        ]);
    }

    public function tearDown()
    {
        Schema::drop('locations');
    }

    public function testGeoWithin()
    {
        $locations = Location::where('location', 'geoWithin', [
            '$geometry' => [
                'type' => 'Polygon',
                'coordinates' => [[
                    [
                        -0.1450383,
                        51.5069158,
                    ],
                    [
                        -0.1367563,
                        51.5100913,
                    ],
                    [
                        -0.1270247,
                        51.5013233,
                    ],
                    [
                        -0.1460866,
                        51.4952136,
                    ],
                    [
                        -0.1450383,
                        51.5069158,
                    ],
                ]],
            ],
        ]);

        $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,
                    ],
                ],
            ]
        ]);

        $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);
        });
    }
}