Completed
Push — master ( 6f3a5b...502ebf )
by Jeroen De
124:28 queued 62:55
created

testGetValueReturnsItself()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Tests\DataValues\Geo\Values;
6
7
use DataValues\Geo\Values\GlobeCoordinateValue;
8
use DataValues\Geo\Values\LatLongValue;
9
use DataValues\IllegalValueException;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers \DataValues\Geo\Values\GlobeCoordinateValue
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class GlobeCoordinateValueTest extends TestCase {
19
20
	public function testGetLatitudeReturnsConstructorValue() {
21
		$this->assertSame(
22
			12.34,
23
			( new GlobeCoordinateValue( new LatLongValue( 12.34, 56.78 ) ) )->getLatitude()
24
		);
25
	}
26
27
	public function testGetLongitudeReturnsConstructorValue() {
28
		$this->assertSame(
29
			56.78,
30
			( new GlobeCoordinateValue( new LatLongValue( 12.34, 56.78 ) ) )->getLongitude()
31
		);
32
	}
33
34
	/**
35
	 * @dataProvider validNonNullGlobeProvider
36
	 */
37
	public function testGetGlobeReturnsConstructorValue( ?string $globe ) {
38
		$this->assertSame(
39
			$globe,
40
			( new GlobeCoordinateValue(
41
				new LatLongValue( 12.34, 56.78 ),
42
				null,
43
				$globe
44
			) )->getGlobe()
45
		);
46
	}
47
48
	public function validNonNullGlobeProvider() {
49
		yield [ GlobeCoordinateValue::GLOBE_EARTH ];
50
		yield [ "coruscant" ];
51
		yield [ "Schar's World" ];
52
		yield [ 'a' ];
53
		yield [ '0' ];
54
	}
55
56
	public function testNullGlobeDefaultToEarth() {
57
		$this->assertSame(
58
			GlobeCoordinateValue::GLOBE_EARTH,
59
			( new GlobeCoordinateValue( new LatLongValue( 12.34, 56.78 ) ) )->getGlobe()
60
		);
61
	}
62
63
	/**
64
	 * @dataProvider validPrecisionProvider
65
	 */
66
	public function testGetPrecisionReturnsConstructorValue( float $precision ) {
67
		$this->assertSame(
68
			$precision,
69
			( new GlobeCoordinateValue( new LatLongValue( 12.34, 56.78 ), $precision ) )->getPrecision()
70
		);
71
	}
72
73
	public function validPrecisionProvider() {
74
		yield [ 360 ];
75
		yield [ 359.9 ];
76
		yield [ -360 ];
77
		yield [ -359.9 ];
78
		yield [ 0 ];
79
		yield [ 1 ];
80
		yield [ -1 ];
81
		yield [ 0.1 ];
82
		yield [ -0.1 ];
83
		yield [ 123 ];
84
		yield [ -123 ];
85
		yield [ 123.4567890123456789 ];
86
	}
87
88
	/**
89
	 * @dataProvider illegalArrayValueProvider
90
	 */
91
	public function testNewFromArrayErrorHandling( $data ) {
92
		$this->expectException( IllegalValueException::class );
93
		GlobeCoordinateValue::newFromArray( $data );
0 ignored issues
show
Deprecated Code introduced by Thiemo Mättig
The method DataValues\Geo\Values\Gl...teValue::newFromArray() has been deprecated with message: since 2.0.1. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
94
	}
95
96
	public function illegalArrayValueProvider() {
97
		return [
98
			[ null ],
99
			[ '' ],
100
			[ [] ],
101
			[ [ 'latitude' => 0 ] ],
102
			[ [ 'longitude' => 0 ] ],
103
		];
104
	}
105
106
	public function testArrayValueCompatibility() {
107
		// These serializations where generated using revision f91f65f989cc3ffacbe924012d8f5b574e0b710c
108
		// The strings are the result of calling getArrayValue on the objects and then feeding this to serialize.
109
110
		$serialization = 'a:5:{s:8:"latitude";d:-4.2000000000000002;'
111
			. 's:9:"longitude";d:42;'
112
			. 's:8:"altitude";N;'
113
			. 's:9:"precision";d:0.01;'
114
			. 's:5:"globe";s:4:"mars";}';
115
116
		$arrayForm = unserialize( $serialization );
117
		$globeCoordinate = GlobeCoordinateValue::newFromArray( $arrayForm );
0 ignored issues
show
Deprecated Code introduced by Thiemo Mättig
The method DataValues\Geo\Values\Gl...teValue::newFromArray() has been deprecated with message: since 2.0.1. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
118
119
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
120
		$this->assertSame( 42.0, $globeCoordinate->getLongitude() );
121
		$this->assertSame( 0.01, $globeCoordinate->getPrecision() );
122
		$this->assertSame( 'mars', $globeCoordinate->getGlobe() );
123
124
		$serialization = 'a:5:{s:8:"latitude";d:-4.2000000000000002;'
125
			. 's:9:"longitude";d:-42;'
126
			. 's:8:"altitude";d:9001;'
127
			. 's:9:"precision";d:1;'
128
			. 's:5:"globe";s:33:"http://www.wikidata.org/entity/Q2";}';
129
130
		$arrayForm = unserialize( $serialization );
131
		$globeCoordinate = GlobeCoordinateValue::newFromArray( $arrayForm );
0 ignored issues
show
Deprecated Code introduced by Thiemo Mättig
The method DataValues\Geo\Values\Gl...teValue::newFromArray() has been deprecated with message: since 2.0.1. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
132
133
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
134
		$this->assertSame( -42.0, $globeCoordinate->getLongitude() );
135
		$this->assertSame( 1.0, $globeCoordinate->getPrecision() );
136
		$this->assertSame( 'http://www.wikidata.org/entity/Q2', $globeCoordinate->getGlobe() );
137
	}
138
139
	public function testSerializeCompatibility() {
140
		$globeCoordinate = unserialize(
141
			'C:42:"DataValues\Geo\Values\GlobeCoordinateValue":27:{[-4.2,-42,null,0.01,"mars"]}'
142
		);
143
		$this->assertInstanceOf( GlobeCoordinateValue::class, $globeCoordinate );
144
145
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
146
		$this->assertSame( -42.0, $globeCoordinate->getLongitude() );
147
		$this->assertSame( 0.01, $globeCoordinate->getPrecision() );
148
		$this->assertSame( 'mars', $globeCoordinate->getGlobe() );
149
150
		$globeCoordinate = unserialize(
151
			'C:42:"DataValues\Geo\Values\GlobeCoordinateValue":27:{[-4.2,-42,9001,0.01,"mars"]}'
152
		);
153
		$this->assertInstanceOf( GlobeCoordinateValue::class, $globeCoordinate );
154
	}
155
156
	public function testHashIsConsistentAcrossDifferentRuntimeEnvironments() {
157
		$latLongValue = new LatLongValue( 12.2, 12.2 );
158
159
		$globeCoordinateValue = new GlobeCoordinateValue( $latLongValue, 0.1, 'does not matter' );
160
161
		$this->assertSame( '08a33f1bbb4c8bd91b6531b5bffd91fd', $globeCoordinateValue->getHash() );
162
	}
163
164
	public function testGetLatLong() {
165
		$latLong = new LatLongValue( 1, 2 );
166
167
		$this->assertSame(
168
			$latLong,
169
			( new GlobeCoordinateValue( $latLong ) )->getLatLong()
170
		);
171
	}
172
173
	public function testTooHighPrecisionCausesInvalidArgumentException() {
174
		$this->expectException( \InvalidArgumentException::class );
175
		new GlobeCoordinateValue( new LatLongValue( 1, 2 ), 360.1 );
176
	}
177
178
	public function testTooLowPrecisionCausesInvalidArgumentException() {
179
		$this->expectException( \InvalidArgumentException::class );
180
		new GlobeCoordinateValue( new LatLongValue( 1, 2 ), -360.1 );
181
	}
182
183
	public function testEmptyGlobeCausesInvalidArgumentException() {
184
		$this->expectException( \InvalidArgumentException::class );
185
		new GlobeCoordinateValue( new LatLongValue( 1, 2 ), null, '' );
186
	}
187
188
	/**
189
	 * @dataProvider instanceProvider
190
	 */
191
	public function testValuesEqualThemselves( GlobeCoordinateValue $globeValue ) {
192
		$this->assertTrue( $globeValue->equals( $globeValue ) );
193
	}
194
195
	public function instanceProvider() {
196
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), 1 ) ];
0 ignored issues
show
Coding Style Comprehensibility introduced by Jeroen De Dauw
$argLists was never initialized. Although not strictly required by PHP, it is generally a good practice to add $argLists = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
197
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 42 ), 1 ) ];
198
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 42, 4.2 ), 0.1 ) ];
199
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 42, 42 ), 0.1 ) ];
200
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( -4.2, -4.2 ), 0.1 ) ];
201
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, -42 ), 0.1 ) ];
202
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( -42, 4.2 ), 10 ) ];
203
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 0.001 ) ];
204
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 360 ) ];
205
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 0, 0 ), -360 ) ];
206
207
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), 1, GlobeCoordinateValue::GLOBE_EARTH ) ];
208
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), 1, 'terminus' ) ];
209
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), 1, "Schar's World" ) ];
210
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), 1, 'coruscant' ) ];
211
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), 1, null ) ];
212
		$argLists[] = yield [ new GlobeCoordinateValue( new LatLongValue( 4.2, 4.2 ), null ) ];
213
	}
214
215
	/**
216
	 * @dataProvider instanceProvider
217
	 */
218
	public function testIdenticalValuesAreEqual( GlobeCoordinateValue $globeValue ) {
219
		$this->assertTrue( $globeValue->equals( $globeValue->getCopy() ) );
220
	}
221
222
	/**
223
	 * @dataProvider instanceProvider
224
	 */
225
	public function testSerializeRountripsWithUnserialize( GlobeCoordinateValue $globeValue ) {
226
		$this->assertEquals(
227
			$globeValue,
228
			unserialize( serialize( $globeValue ) )
229
		);
230
	}
231
232
	/**
233
	 * @dataProvider instanceProvider
234
	 */
235
	public function testGetArrayValueAndNewFromArrayRoundtrip( GlobeCoordinateValue $globeValue ) {
236
		$this->assertEquals(
237
			$globeValue,
238
			GlobeCoordinateValue::newFromArray( $globeValue->getArrayValue() )
0 ignored issues
show
Deprecated Code introduced by Jeroen De Dauw
The method DataValues\Geo\Values\Gl...teValue::newFromArray() has been deprecated with message: since 2.0.1. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
239
		);
240
	}
241
242
	/**
243
	 * @dataProvider instanceProvider
244
	 */
245
	public function testGetSortkeyReturnsLatitude( GlobeCoordinateValue $globeValue ) {
246
		$this->assertSame(
247
			$globeValue->getLatitude(),
248
			$globeValue->getSortKey()
249
		);
250
	}
251
252
	/**
253
	 * @dataProvider instanceProvider
254
	 */
255
	public function testGetValueReturnsItself( GlobeCoordinateValue $globeValue ) {
256
		$this->assertSame(
257
			$globeValue,
258
			$globeValue->getValue()
259
		);
260
	}
261
262
}
263