-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathGeometryCastTest.php
219 lines (170 loc) · 7.39 KB
/
GeometryCastTest.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
use Illuminate\Support\Facades\DB;
use MatanYadaev\EloquentSpatial\AxisOrder;
use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\GeometryExpression;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
it('creates a model record with null geometry', function (): void {
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => null]);
expect($testPlace->point)->toBeNull();
});
it('updates a model record', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlace->update(['point' => $point2]);
expect($testPlace->point)->not->toEqual($point);
expect($testPlace->point)->toEqual($point2);
});
it('updates a model record with expression', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$pointFromAttributes = $testPlace->getAttributes()['point'];
expect(function () use ($testPlace, $pointFromAttributes): void {
$testPlace->update(['point' => $pointFromAttributes]);
})->not->toThrow(InvalidArgumentException::class);
});
it('updates a model record with null geometry', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlace->update(['point' => null]);
expect($testPlace->point)->toBeNull();
});
it('gets original geometry field', function (): void {
$point = new Point(0, 180, Srid::WGS84->value);
$point2 = new Point(0, 0, Srid::WGS84->value);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlace->point = $point2;
expect($testPlace->getOriginal('point'))->toEqual($point);
expect($testPlace->point)->not->toEqual($point);
expect($testPlace->point)->toEqual($point2);
});
it('serializes a model record to array with geometry', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$serialized = $testPlace->toArray();
$expectedArray = $point->toArray();
expect($serialized['point'])->toEqual($expectedArray);
});
it('serializes a model record to json with geometry', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$serialized = $testPlace->toJson();
// @phpstan-ignore-next-line
$json = json_encode(json_decode($serialized, true)['point']);
$expectedJson = $point->toJson();
expect($json)->toBe($expectedJson);
});
it('throws exception when cast serializing incorrect geometry object', function (): void {
expect(function (): void {
TestPlace::factory()->make([
'point' => new LineString([
new Point(0, 180),
new Point(1, 179),
]),
]);
})->toThrow(InvalidArgumentException::class);
});
it('throws exception when cast serializing non-geometry object', function (): void {
expect(function (): void {
TestPlace::factory()->make([
'point' => 'not-a-point-object',
]);
})->toThrow(InvalidArgumentException::class);
});
it('throws exception when cast deserializing incorrect geometry object', function (): void {
TestPlace::insert(array_merge(TestPlace::factory()->definition(), [
'point_with_line_string_cast' => DB::raw(
(new GeometryExpression('POINT(0, 180)'))->normalize(DB::connection())
),
]));
/** @var TestPlace $testPlace */
$testPlace = TestPlace::firstOrFail();
expect(function () use ($testPlace): void {
$testPlace->getAttribute('point_with_line_string_cast');
})->toThrow(InvalidArgumentException::class);
});
it('creates a model record with geometry from geo json array', function (): void {
$point = new Point(0, 180);
$pointGeoJsonArray = $point->toArray();
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->make(['point' => $pointGeoJsonArray]);
expect($testPlace->point)->toEqual($point);
});
it('checks a model record is not dirty after creation', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
expect($testPlace->isDirty())->toBeFalse();
});
it('checks a model record is not dirty after fetch', function (): void {
$point = new Point(0, 180);
TestPlace::factory()->create(['point' => $point]);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::firstOrFail();
expect($testPlace->isDirty())->toBeFalse();
});
it('checks a model record is dirty after update from null before save', function (): void {
$point = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create([]);
$testPlace->point = $point;
expect($testPlace->isDirty())->toBeTrue();
});
it('checks a model record is dirty after update before save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlace->point = $point2;
expect($testPlace->isDirty())->toBeTrue();
});
it('checks a model record is not dirty after update and save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 0);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlace->point = $point2;
$testPlace->save();
expect($testPlace->isDirty())->toBeFalse();
});
it('checks a model record is not dirty after update to same value before save', function (): void {
$point = new Point(0, 180);
$point2 = new Point(0, 180);
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => $point]);
$testPlace->point = $point2;
expect($testPlace->isDirty())->toBeFalse();
});
it('handles casting geometry columns with raw expressions', function (string $expression): void {
// Arrange
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => DB::raw($expression)]);
// Act & Assert
expect(function () use ($testPlace): void {
// Trigger 'point' attribute to cast raw expression to a `Point` object
$testPlace->originalIsEquivalent('point');
})->not->toThrow(Exception::class);
})->with([
'without SRID' => "ST_GeomFromText('POINT(0 0)')",
'with SRID' => "ST_GeomFromText('POINT(0 0)', 4326)",
]);
it('handles casting geometry columns with raw expressions with axis order', function (): void {
// Arrange
/** @var TestPlace $testPlace */
$testPlace = TestPlace::factory()->create(['point' => DB::raw("ST_GeomFromText('POINT(0 0)', 4326, 'axis-order=long-lat')")]);
// Act & Assert
expect(function () use ($testPlace): void {
// Trigger 'point' attribute to cast raw expression to a `Point` object
$testPlace->originalIsEquivalent('point');
})->not->toThrow(Exception::class);
})->skip(fn () => ! AxisOrder::supported(DB::connection()));