Passed
Push — testz ( 584ff4 )
by Jeroen De
293:56 queued 277:54
created

LatLongValueTest::testGetType()   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 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
 * @group DataValue
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0-or-later
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class LatLongValueTest extends TestCase {
20
21
	public function testTooHighLatitudesCauseInvalidArgumentException() {
22
		$this->expectException( \InvalidArgumentException::class );
23
		new LatLongValue( 361, 0 );
24
	}
25
26
	public function testTooLowLatitudesCauseInvalidArgumentException() {
27
		$this->expectException( \InvalidArgumentException::class );
28
		new LatLongValue( -360.001, 0 );
29
	}
30
31
	public function testTooHighLongitudesCauseInvalidArgumentException() {
32
		$this->expectException( \InvalidArgumentException::class );
33
		new LatLongValue( 0, 360.001 );
34
	}
35
36
	public function testLo9wHighLongitudesCauseInvalidArgumentException() {
37
		$this->expectException( \InvalidArgumentException::class );
38
		new LatLongValue( 0, -361 );
39
	}
40
41
	public function testGetLatitudeReturnsConstructorValue() {
42
		$this->assertSame(
43
			12.34,
44
			( new LatLongValue( 12.34, 56.78 ) )->getLatitude()
45
		);
46
	}
47
48
	public function testGetLongitudeReturnsConstructorValue() {
49
		$this->assertSame(
50
			56.78,
51
			( new LatLongValue( 12.34, 56.78 ) )->getLongitude()
52
		);
53
	}
54
55
	/**
56
	 * @dataProvider invalidCoordinatesProvider
57
	 */
58
	public function testConstructorThrowsExceptionWhenParametersAreInvalid( float $latitude, float $longitude ) {
59
		$this->expectException( \InvalidArgumentException::class );
60
		new LatLongValue( $latitude, $longitude );
61
	}
62
63
	public function invalidCoordinatesProvider() {
64
		yield 'latitude too small' => [ -361, 0 ];
65
		yield 'latitude too big' => [ 361, 0 ];
66
		yield 'longitude too big' => [ 0, 361 ];
67
		yield 'longitude too small' => [ 0, -361 ];
68
	}
69
70
	public function testCopyProducesIdenticalObject() {
71
		$latLong = new LatLongValue( 1, 2 );
72
		$this->assertEquals(
73
			$latLong,
74
			$latLong->getCopy()
75
		);
76
	}
77
78
	public function testCopyProducesObjectWithDifferentIdentity() {
79
		$latLong = new LatLongValue( 1, 2 );
80
		$this->assertNotSame(
81
			$latLong,
82
			$latLong->getCopy()
83
		);
84
	}
85
86
	public function testGetHashProducesMd5() {
87
		$this->assertSame( '7a6ba7398547fbc6bc26fb3d77b93897', ( new LatLongValue( 0, 0 ) )->getHash() );
88
		$this->assertSame( 'b8af9bef608c55ae8c1610daa89e937f', ( new LatLongValue( 1, 2 ) )->getHash() );
89
	}
90
91
	/**
92
	 * @dataProvider instanceProvider
93
	 */
94
	public function testValuesEqualThemselves( LatLongValue $latLongValue ) {
95
		$this->assertTrue( $latLongValue->equals( $latLongValue ) );
96
	}
97
98
	public function instanceProvider() {
99
		yield [ new LatLongValue( 12.34, 56.78 ) ];
100
		yield [ new LatLongValue( 1, 1 ) ];
101
		yield [ new LatLongValue( 0, 0 ) ];
102
		yield [ new LatLongValue( -1, 10 ) ];
103
		yield [ new LatLongValue( 10, -1 ) ];
104
		yield [ new LatLongValue( -360, -360 ) ];
105
		yield [ new LatLongValue( 360, 360 ) ];
106
	}
107
108
	/**
109
	 * @dataProvider instanceProvider
110
	 */
111
	public function testIdenticalValuesAreEqual( LatLongValue $latLongValue ) {
112
		$this->assertTrue( $latLongValue->equals( $latLongValue->getCopy() ) );
113
	}
114
115
	public function testDifferentValuesDoNotEqual() {
116
		$this->assertFalse(
117
			( new LatLongValue( 0, 0 ) )->equals( new LatLongValue( 1, 0 ) )
118
		);
119
120
		$this->assertFalse(
121
			( new LatLongValue( 0, 0 ) )->equals( new LatLongValue( 0, 1 ) )
122
		);
123
124
		$this->assertFalse(
125
			( new LatLongValue( 0, 0 ) )->equals( new LatLongValue( 0, 0.01 ) )
126
		);
127
128
		$this->assertFalse(
129
			( new LatLongValue( 0, 1 ) )->equals( new LatLongValue( 0, -1 ) )
130
		);
131
	}
132
133
	public function testSerialize() {
134
		$this->assertSame(
135
			'12.34|56.78',
136
			( new LatLongValue( 12.34, 56.78 ) )->serialize()
137
		);
138
139
		$this->assertSame(
140
			'-12.34|0',
141
			( new LatLongValue( -12.34, 0 ) )->serialize()
142
		);
143
144
		$this->assertSame(
145
			'0|-56.78',
146
			( new LatLongValue( 0, -56.78 ) )->serialize()
147
		);
148
	}
149
150
	/**
151
	 * @dataProvider instanceProvider
152
	 */
153
	public function testSerializeRountripsWithUnserialize( LatLongValue $latLong ) {
154
		$this->assertEquals(
155
			$latLong,
156
			unserialize( serialize( $latLong ) )
157
		);
158
	}
159
160
	/**
161
	 * @dataProvider instanceProvider
162
	 */
163
	public function testGetArrayValueAndNewFromArrayRoundtrip( LatLongValue $latLong ) {
164
		$this->assertEquals(
165
			$latLong,
166
			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...
167
		);
168
	}
169
170
	public function testToArray() {
171
		$this->assertSame(
172
			[
173
				'value' => [
174
					'latitude' => 12.34,
175
					'longitude' => 56.78
176
				],
177
				'type' => 'geocoordinate',
178
			],
179
			( new LatLongValue( 12.34, 56.78 ) )->toArray()
180
		);
181
	}
182
183
	public function testGetType() {
184
		$this->assertSame(
185
			'geocoordinate',
186
			LatLongValue::getType()
187
		);
188
	}
189
190
	/**
191
	 * @dataProvider instanceProvider
192
	 */
193
	public function testGetSortkeyReturnsLatitude( LatLongValue $latLong ) {
194
		$this->assertSame(
195
			$latLong->getLatitude(),
196
			$latLong->getSortKey()
197
		);
198
	}
199
200
}
201