forked from MatanYadaev/laravel-eloquent-spatial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointTest.php
128 lines (87 loc) · 3.47 KB
/
PointTest.php
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
<?php
use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
uses(DatabaseMigrations::class);
it('creates a model record with point', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
expect($testPlace->point)->toBeInstanceOf(Point::class);
expect($testPlace->point)->toEqual($point);
});
it('creates a model record with point with SRID integer', function (): void {
$point = new Point(0, 180, Srid::WGS84->value);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
expect($testPlace->point->srid)->toBe(Srid::WGS84->value);
});
it('creates a model record with point with SRID enum', function (): void {
$point = new Point(0, 180, Srid::WGS84);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
expect($testPlace->point->srid)->toBe(Srid::WGS84->value);
});
it('creates point from JSON', function (): void {
$point = new Point(0, 180);
$pointFromJson = Point::fromJson('{"type":"Point","coordinates":[180,0]}');
expect($pointFromJson)->toEqual($point);
});
it('creates point with SRID from JSON', function (): void {
$point = new Point(0, 180, Srid::WGS84->value);
$pointFromJson = Point::fromJson('{"type":"Point","coordinates":[180,0]}', Srid::WGS84->value);
expect($pointFromJson)->toEqual($point);
});
it('generates point JSON', function (): void {
$point = new Point(0, 180);
$json = $point->toJson();
$expectedJson = '{"type":"Point","coordinates":[180,0]}';
expect($json)->toBe($expectedJson);
});
it('throws exception when creating point from invalid JSON', function (): void {
expect(function (): void {
Point::fromJson('{"type":"Point","coordinates":[]}');
})->toThrow(InvalidArgumentException::class);
});
it('creates point from WKT', function (): void {
$point = new Point(0, 180);
$pointFromWkt = Point::fromWkt('POINT(180 0)');
expect($pointFromWkt)->toEqual($point);
});
it('creates point with SRID from WKT', function (): void {
$point = new Point(0, 180, Srid::WGS84->value);
$pointFromWkt = Point::fromWkt('POINT(180 0)', Srid::WGS84->value);
expect($pointFromWkt)->toEqual($point);
});
it('generates point WKT', function (): void {
$point = new Point(0, 180);
$wkt = $point->toWkt();
$expectedWkt = 'POINT(180 0)';
expect($wkt)->toBe($expectedWkt);
});
it('creates point from WKB', function (): void {
$point = new Point(0, 180);
$pointFromWkb = Point::fromWkb($point->toWkb());
expect($pointFromWkb)->toEqual($point);
});
it('creates point with SRID from WKB', function (): void {
$point = new Point(0, 180, Srid::WGS84->value);
$pointFromWkb = Point::fromWkb($point->toWkb());
expect($pointFromWkb)->toEqual($point);
});
it('casts a Point to a string', function (): void {
$point = new Point(0, 180, Srid::WGS84->value);
expect($point->__toString())->toEqual('POINT(180 0)');
});
it('adds a macro toPoint', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});
$point = new Point(0, 180, Srid::WGS84->value);
// @phpstan-ignore-next-line
expect($point->getName())->toBe('Point');
});