Passed
Push — docz ( b247cd )
by Jeroen De
269:07 queued 242:37
created

LatLongValueTest::instanceProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Tests\DataValues\Geo\Values;
6
7
use DataValues\Geo\Values\LatLongValue;
8
use PHPUnit\Framework\TestCase;
9
10
/**
11
 * @covers \DataValues\Geo\Values\LatLongValue
12
 *
13
 * @license GPL-2.0-or-later
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class LatLongValueTest extends TestCase {
17
18
	public function testTooHighLatitudesCauseInvalidArgumentException() {
19
		$this->expectException( \InvalidArgumentException::class );
20
		new LatLongValue( 361, 0 );
21
	}
22
23
	public function testTooLowLatitudesCauseInvalidArgumentException() {
24
		$this->expectException( \InvalidArgumentException::class );
25
		new LatLongValue( -360.001, 0 );
26
	}
27
28
	public function testTooHighLongitudesCauseInvalidArgumentException() {
29
		$this->expectException( \InvalidArgumentException::class );
30
		new LatLongValue( 0, 360.001 );
31
	}
32
33
	public function testLo9wHighLongitudesCauseInvalidArgumentException() {
34
		$this->expectException( \InvalidArgumentException::class );
35
		new LatLongValue( 0, -361 );
36
	}
37
38
	public function testGetLatitudeReturnsConstructorValue() {
39
		$this->assertSame(
40
			12.34,
41
			( new LatLongValue( 12.34, 56.78 ) )->getLatitude()
42
		);
43
	}
44
45
	public function testGetLongitudeReturnsConstructorValue() {
46
		$this->assertSame(
47
			56.78,
48
			( new LatLongValue( 12.34, 56.78 ) )->getLongitude()
49
		);
50
	}
51
52
	/**
53
	 * @dataProvider invalidCoordinatesProvider
54
	 */
55
	public function testConstructorThrowsExceptionWhenParametersAreInvalid( float $latitude, float $longitude ) {
56
		$this->expectException( \InvalidArgumentException::class );
57
		new LatLongValue( $latitude, $longitude );
58
	}
59
60
	public function invalidCoordinatesProvider() {
61
		yield 'latitude too small' => [ -361, 0 ];
62
		yield 'latitude too big' => [ 361, 0 ];
63
		yield 'longitude too big' => [ 0, 361 ];
64
		yield 'longitude too small' => [ 0, -361 ];
65
	}
66
67
	public function testCopyProducesIdenticalObject() {
68
		$latLong = new LatLongValue( 1, 2 );
69
		$this->assertEquals(
70
			$latLong,
71
			$latLong->getCopy()
72
		);
73
	}
74
75
	public function testCopyProducesObjectWithDifferentIdentity() {
76
		$latLong = new LatLongValue( 1, 2 );
77
		$this->assertNotSame(
78
			$latLong,
79
			$latLong->getCopy()
80
		);
81
	}
82
83
	public function testGetHashProducesMd5() {
84
		$this->assertSame( '7a6ba7398547fbc6bc26fb3d77b93897', ( new LatLongValue( 0, 0 ) )->getHash() );
85
		$this->assertSame( 'b8af9bef608c55ae8c1610daa89e937f', ( new LatLongValue( 1, 2 ) )->getHash() );
86
	}
87
88
	/**
89
	 * @dataProvider instanceProvider
90
	 */
91
	public function testValuesEqualThemselves( LatLongValue $latLongValue ) {
92
		$this->assertTrue( $latLongValue->equals( $latLongValue ) );
93
	}
94
95
	public function instanceProvider() {
96
		yield [ new LatLongValue( 12.34, 56.78 ) ];
97
		yield [ new LatLongValue( 1, 1 ) ];
98
		yield [ new LatLongValue( 0, 0 ) ];
99
		yield [ new LatLongValue( -1, 10 ) ];
100
		yield [ new LatLongValue( 10, -1 ) ];
101
		yield [ new LatLongValue( -360, -360 ) ];
102
		yield [ new LatLongValue( 360, 360 ) ];
103
	}
104
105
	/**
106
	 * @dataProvider instanceProvider
107
	 */
108
	public function testIdenticalValuesAreEqual( LatLongValue $latLongValue ) {
109
		$this->assertTrue( $latLongValue->equals( $latLongValue->getCopy() ) );
110
	}
111
112
	public function testDifferentValuesDoNotEqual() {
113
		$this->assertFalse(
114
			( new LatLongValue( 0, 0 ) )->equals( new LatLongValue( 1, 0 ) )
115
		);
116
117
		$this->assertFalse(
118
			( new LatLongValue( 0, 0 ) )->equals( new LatLongValue( 0, 1 ) )
119
		);
120
121
		$this->assertFalse(
122
			( new LatLongValue( 0, 0 ) )->equals( new LatLongValue( 0, 0.01 ) )
123
		);
124
125
		$this->assertFalse(
126
			( new LatLongValue( 0, 1 ) )->equals( new LatLongValue( 0, -1 ) )
127
		);
128
	}
129
130
	public function testSerialize() {
131
		$this->assertSame(
132
			'12.34|56.78',
133
			( new LatLongValue( 12.34, 56.78 ) )->serialize()
134
		);
135
136
		$this->assertSame(
137
			'-12.34|0',
138
			( new LatLongValue( -12.34, 0 ) )->serialize()
139
		);
140
141
		$this->assertSame(
142
			'0|-56.78',
143
			( new LatLongValue( 0, -56.78 ) )->serialize()
144
		);
145
	}
146
147
	/**
148
	 * @dataProvider instanceProvider
149
	 */
150
	public function testSerializeRountripsWithUnserialize( LatLongValue $latLong ) {
151
		$this->assertEquals(
152
			$latLong,
153
			unserialize( serialize( $latLong ) )
154
		);
155
	}
156
157
	/**
158
	 * @dataProvider instanceProvider
159
	 */
160
	public function testGetArrayValueAndNewFromArrayRoundtrip( LatLongValue $latLong ) {
161
		$this->assertEquals(
162
			$latLong,
163
			LatLongValue::newFromArray( $latLong->getArrayValue() )
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\Geo\Values\LatLongValue::newFromArray() has been deprecated with message: since 2.0.1. When using this static constructor for DataValues of unknown
types, please use DataValueDeserializer from the data-values/serialization package instead.

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...
164
		);
165
	}
166
167
	public function testToArray() {
168
		$this->assertSame(
169
			[
170
				'value' => [
171
					'latitude' => 12.34,
172
					'longitude' => 56.78
173
				],
174
				'type' => 'geocoordinate',
175
			],
176
			( new LatLongValue( 12.34, 56.78 ) )->toArray()
177
		);
178
	}
179
180
	public function testGetType() {
181
		$this->assertSame(
182
			'geocoordinate',
183
			LatLongValue::getType()
184
		);
185
	}
186
187
	/**
188
	 * @dataProvider instanceProvider
189
	 */
190
	public function testGetSortkeyReturnsLatitude( LatLongValue $latLong ) {
191
		$this->assertSame(
192
			$latLong->getLatitude(),
193
			$latLong->getSortKey()
194
		);
195
	}
196
197
	/**
198
	 * @dataProvider instanceProvider
199
	 */
200
	public function testGetValueReturnsItself( LatLongValue $latLong ) {
201
		$this->assertSame(
202
			$latLong,
203
			$latLong->getValue()
204
		);
205
	}
206
207
	public function testNewFromArrayWithoutLatitudeCausesException() {
208
		$this->expectException( \InvalidArgumentException::class );
209
210
		LatLongValue::newFromArray( [
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\Geo\Values\LatLongValue::newFromArray() has been deprecated with message: since 2.0.1. When using this static constructor for DataValues of unknown
types, please use DataValueDeserializer from the data-values/serialization package instead.

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...
211
			'longitude' => 56.78,
212
		] );
213
	}
214
215
	public function testNewFromArrayWithoutLongitudeCausesException() {
216
		$this->expectException( \InvalidArgumentException::class );
217
218
		LatLongValue::newFromArray( [
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\Geo\Values\LatLongValue::newFromArray() has been deprecated with message: since 2.0.1. When using this static constructor for DataValues of unknown
types, please use DataValueDeserializer from the data-values/serialization package instead.

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...
219
			'latitude' => 12.34,
220
		] );
221
	}
222
223
	public function testNewFromArrayWithNonArrayParameterCausesException() {
224
		$this->expectException( \InvalidArgumentException::class );
225
		LatLongValue::newFromArray( 'such' );
0 ignored issues
show
Deprecated Code introduced by
The method DataValues\Geo\Values\LatLongValue::newFromArray() has been deprecated with message: since 2.0.1. When using this static constructor for DataValues of unknown
types, please use DataValueDeserializer from the data-values/serialization package instead.

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...
226
	}
227
228
}
229