-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAbstractTestCase.php
374 lines (309 loc) · 12.7 KB
/
AbstractTestCase.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
declare(strict_types=1);
namespace Brick\Geo\Tests;
use Brick\Geo\CoordinateSystem;
use Brick\Geo\Geometry;
use Brick\Geo\MultiLineString;
use Brick\Geo\MultiPoint;
use Brick\Geo\MultiPolygon;
use Brick\Geo\Point;
use Brick\Geo\Curve;
use Brick\Geo\LineString;
use Brick\Geo\CircularString;
use Brick\Geo\CompoundCurve;
use Brick\Geo\Polygon;
use Brick\Geo\CurvePolygon;
use Brick\Geo\PolyhedralSurface;
use Brick\Geo\TIN;
use Brick\Geo\Triangle;
use Closure;
use PHPUnit\Framework\TestCase;
use Throwable;
/**
* Base class for Geometry tests.
*/
class AbstractTestCase extends TestCase
{
final protected function assertWktEquals(Geometry $geometry, string $wkt, int $srid = 0) : void
{
self::assertSame($wkt, $geometry->asText());
self::assertSame($srid, $geometry->SRID());
}
/**
* @param Geometry[] $geometries
* @param string[] $wkts
*/
final protected function assertWktEqualsMultiple(array $geometries, array $wkts, int $srid = 0) : void
{
foreach ($geometries as $geometry) {
self::assertSame($srid, $geometry->SRID());
}
self::assertSame(
$wkts,
array_map(
fn (Geometry $geometry) => $geometry->asText(),
$geometries
)
);
}
/**
* Asserts that two geometries' coordinates are equal with a given delta.
*/
final protected function assertGeometryEqualsWithDelta(Geometry $expected, Geometry $actual, float $delta = 0.0) : void
{
$expectedWKT = $expected->asText();
$actualWKT = $actual->asText();
self::assertSame($expected->geometryType(), $actual->geometryType());
self::assertEqualsWithDelta($expected->toArray(), $actual->toArray(), $delta,
'Failed asserting that two geometries are equal with delta.'
. "\n---Expected"
. "\n+++Actual"
. "\n@@ @@"
. "\n-" . $expectedWKT
. "\n+" . $actualWKT
);
}
/**
* @param Geometry $g The Geometry to test.
* @param array $coords The expected raw coordinates of the geometry.
* @param bool $hasZ Whether the geometry is expected to contain Z coordinates.
* @param bool $hasM Whether the geometry is expected to contain M coordinates.
* @param int $srid The expected SRID of the geometry.
*/
final protected function assertGeometryContents(Geometry $g, array $coords, bool $hasZ = false, bool $hasM = false, int $srid = 0) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $g->toArray());
self::assertSame($hasZ, $g->is3D());
self::assertSame($hasM, $g->isMeasured());
self::assertSame($srid, $g->SRID());
}
/**
* @param array $coords The expected coordinates of the Point as returned by toArray().
* @param bool $is3D Whether the Point is expected to contain a Z coordinate.
* @param bool $isMeasured Whether the Point is expected to contain a M coordinate.
* @param int $srid The expected SRID.
* @param Point $point The Point to test.
*/
final protected function assertPointEquals(array $coords, bool $is3D, bool $isMeasured, int $srid, Point $point) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $point->toArray());
self::assertSame($is3D, $point->is3D());
self::assertSame($isMeasured, $point->isMeasured());
self::assertSame($srid, $point->SRID());
}
final protected function assertPointXYEquals(float $x, float $y, int $srid, Point $point) : void
{
$this->assertPointEquals([$x, $y], false, false, $srid, $point);
}
final protected function assertPointXYZEquals(float $x, float $y, float $z, int $srid, Point $point) : void
{
$this->assertPointEquals([$x, $y, $z], true, false, $srid, $point);
}
/**
* @param array $coords The expected coordinates of the LineString as returned by toArray().
* @param bool $is3D Whether the LineString is expected to contain Z coordinates.
* @param bool $isMeasured Whether the LineString is expected to contain M coordinates.
* @param LineString $lineString The LineString to test.
*/
final protected function assertLineStringEquals(array $coords, bool $is3D, bool $isMeasured, LineString $lineString) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $lineString->toArray());
self::assertSame($is3D, $lineString->is3D());
self::assertSame($isMeasured, $lineString->isMeasured());
}
/**
* @param array $coords The expected coordinates of the Polygon as returned by toArray().
* @param bool $is3D Whether the Polygon is expected to contain Z coordinates.
* @param bool $isMeasured Whether the Polygon is expected to contain M coordinates.
* @param Polygon $polygon The Polygon to test.
*/
final protected function assertPolygonEquals(array $coords, bool $is3D, bool $isMeasured, Polygon $polygon) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $polygon->toArray());
self::assertSame($is3D, $polygon->is3D());
self::assertSame($isMeasured, $polygon->isMeasured());
}
/**
* @param array $coords The expected coordinates of the MultiPoint as returned by toArray().
* @param bool $is3D Whether the MultiPoint is expected to contain Z coordinates.
* @param bool $isMeasured Whether the MultiPoint is expected to contain M coordinates.
* @param MultiPoint $multiPoint The MultiPoint to test.
*/
final protected function assertMultiPointEquals(array $coords, bool $is3D, bool $isMeasured, MultiPoint $multiPoint) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $multiPoint->toArray());
self::assertSame($is3D, $multiPoint->is3D());
self::assertSame($isMeasured, $multiPoint->isMeasured());
}
/**
* @param array $coords The expected coordinates of the MultiLineString as returned by toArray().
* @param bool $is3D Whether the MultiLineString is expected to contain Z coordinates.
* @param bool $isMeasured Whether the MultiLineString is expected to contain M coordinates.
* @param MultiLineString $multiLineString The MultiLineString to test.
*/
final protected function assertMultiLineStringEquals(array $coords, bool $is3D, bool $isMeasured, MultiLineString $multiLineString) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $multiLineString->toArray());
self::assertSame($is3D, $multiLineString->is3D());
self::assertSame($isMeasured, $multiLineString->isMeasured());
}
/**
* @param array $coords The expected coordinates of the MultiPolygon as returned by toArray().
* @param bool $is3D Whether the MultiPolygon is expected to contain Z coordinates.
* @param bool $isMeasured Whether the MultiPolygon is expected to contain M coordinates.
* @param MultiPolygon $multiPolygon The MultiPolygon to test.
*/
final protected function assertMultiPolygonEquals(array $coords, bool $is3D, bool $isMeasured, MultiPolygon $multiPolygon) : void
{
$this->castToFloat($coords);
self::assertSame($coords, $multiPolygon->toArray());
self::assertSame($is3D, $multiPolygon->is3D());
self::assertSame($isMeasured, $multiPolygon->isMeasured());
}
final protected function createPoint(array $coords, CoordinateSystem $cs) : Point
{
return new Point($cs, ...$coords);
}
final protected function createLineString(array $coords, CoordinateSystem $cs) : LineString
{
$points = [];
foreach ($coords as $point) {
$points[] = $this->createPoint($point, $cs);
}
return new LineString($cs, ...$points);
}
final protected function createCircularString(array $coords, CoordinateSystem $cs) : CircularString
{
$points = [];
foreach ($coords as $point) {
$points[] = $this->createPoint($point,$cs);
}
return new CircularString($cs, ...$points);
}
/**
* Creates a LineString or CircularString from an array of coordinates.
*
* For the purpose of these tests, it is assumed that a curve with an even number of points is
* a LineString, and a curve with an odd number of points is a CircularString.
*/
final protected function createLineStringOrCircularString(array $coords, CoordinateSystem $cs) : Curve
{
if (count($coords) % 2 === 0) {
return $this->createLineString($coords, $cs);
}
return $this->createCircularString($coords, $cs);
}
final protected function createCompoundCurve(array $coords, CoordinateSystem $cs) : CompoundCurve
{
$curves = [];
foreach ($coords as $curve) {
$curves[] = $this->createLineStringOrCircularString($curve, $cs);
}
return new CompoundCurve($cs, ...$curves);
}
final protected function createPolygon(array $coords, CoordinateSystem $cs) : Polygon
{
$rings = [];
foreach ($coords as $ring) {
$rings[] = $this->createLineString($ring, $cs);
}
return new Polygon($cs, ...$rings);
}
final protected function createTriangle(array $coords, CoordinateSystem $cs) : Triangle
{
$rings = [];
foreach ($coords as $ring) {
$rings[] = $this->createLineString($ring, $cs);
}
return new Triangle($cs, ...$rings);
}
final protected function createCurvePolygon(array $coords, CoordinateSystem $cs) : CurvePolygon
{
$rings = [];
foreach ($coords as $ring) {
if (is_array($ring[0][0])) {
// CompoundCurve
$rings[] = $this->createCompoundCurve($ring, $cs);
} else {
// LineString or CircularString
$rings[] = $this->createLineStringOrCircularString($ring, $cs);
}
}
return new CurvePolygon($cs, ...$rings);
}
final protected function createMultiPoint(array $coords, CoordinateSystem $cs) : MultiPoint
{
$points = [];
foreach ($coords as $point) {
$points[] = $this->createPoint($point, $cs);
}
return new MultiPoint($cs, ...$points);
}
final protected function createMultiLineString(array $coords, CoordinateSystem $cs) : MultiLineString
{
$lineStrings = [];
foreach ($coords as $lineString) {
$lineStrings[] = $this->createLineString($lineString, $cs);
}
return new MultiLineString($cs, ...$lineStrings);
}
final protected function createMultiPolygon(array $coords, CoordinateSystem $cs) : MultiPolygon
{
$polygons = [];
foreach ($coords as $polygon) {
$polygons[] = $this->createPolygon($polygon, $cs);
}
return new MultiPolygon($cs, ...$polygons);
}
final protected function createPolyhedralSurface(array $coords, CoordinateSystem $cs) : PolyhedralSurface
{
$patches = [];
foreach ($coords as $patch) {
$patches[] = $this->createPolygon($patch, $cs);
}
return new PolyhedralSurface($cs, ...$patches);
}
final protected function createTIN(array $coords, CoordinateSystem $cs) : TIN
{
$patches = [];
foreach ($coords as $patch) {
$patches[] = $this->createTriangle($patch, $cs);
}
return new TIN($cs, ...$patches);
}
/**
* Casts all values in the array to floats.
*
* This allows to write more concise data providers such as [1 2] instead of [1.0, 2.0]
* while still strictly enforcing that the toArray() methods of the geometries return float values.
*/
final protected function castToFloat(array & $coords) : void
{
array_walk_recursive($coords, function (& $value) {
$value = (float) $value;
});
}
/**
* @param Closure():void $closure
* @param class-string<Throwable> $exceptionClass
*/
final protected function expectExceptionIn(Closure $closure, string $exceptionClass): void
{
try {
$closure();
} catch (Throwable $exception) {
if ($exception::class === $exceptionClass) {
$this->addtoAssertionCount(1);
return;
}
throw $exception;
}
$this->fail(sprintf('Failed asserting that exception of type "%s" is thrown.', $exceptionClass));
}
}