Passed
Push — master ( 954049...2e99dd )
by Leszek
07:28
created

validConstructorArgumentsProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 1
eloc 19
nc 1
nop 0
1
<?php
2
3
namespace Tests\DataValues\Geo\Values;
4
5
use DataValues\Geo\Values\GlobeCoordinateValue;
6
use DataValues\Geo\Values\LatLongValue;
7
use DataValues\IllegalValueException;
8
use DataValues\Tests\DataValueTest;
9
10
/**
11
 * @covers DataValues\Geo\Values\GlobeCoordinateValue
12
 *
13
 * @group DataValue
14
 * @group DataValueExtensions
15
 *
16
 * @license GPL-2.0+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class GlobeCoordinateValueTest extends DataValueTest {
20
21
	/**
22
	 * @see DataValueTest::getClass
23
	 *
24
	 * @return string
25
	 */
26
	public function getClass() {
27
		return 'DataValues\Geo\Values\GlobeCoordinateValue';
28
	}
29
30
	public function validConstructorArgumentsProvider() {
31
		$argLists = array();
32
33
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1 );
34
		$argLists[] = array( new LatLongValue( 4.2, 42 ), 1 );
35
		$argLists[] = array( new LatLongValue( 42, 4.2 ), 0.1 );
36
		$argLists[] = array( new LatLongValue( 42, 42 ), 0.1 );
37
		$argLists[] = array( new LatLongValue( -4.2, -4.2 ), 0.1 );
38
		$argLists[] = array( new LatLongValue( 4.2, -42 ), 0.1 );
39
		$argLists[] = array( new LatLongValue( -42, 4.2 ), 10 );
40
		$argLists[] = array( new LatLongValue( 0, 0 ), 0.001 );
41
		$argLists[] = array( new LatLongValue( 0, 0 ), 360 );
42
		$argLists[] = array( new LatLongValue( 0, 0 ), -360 );
43
44
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, GlobeCoordinateValue::GLOBE_EARTH );
45
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, 'terminus' );
46
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, "Schar's World" );
47
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, 'coruscant' );
48
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, null );
49
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), null );
50
51
		return $argLists;
52
	}
53
54
	public function invalidConstructorArgumentsProvider() {
55
		$argLists = array();
56
57
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 361 );
58
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), -361 );
59
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 'foo' );
60
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), true );
61
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), array( 1 ) );
62
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), '1' );
63
64
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, '' );
65
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, true );
66
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, array( 1 ) );
67
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, 1 );
68
69
		return $argLists;
70
	}
71
72
	/**
73
	 * @dataProvider instanceProvider
74
	 */
75
	public function testGetLatitude( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
76
		$actual = $globeCoordinate->getLatitude();
77
78
		$this->assertInternalType( 'float', $actual );
79
		$this->assertSame( $arguments[0]->getLatitude(), $actual );
80
	}
81
82
	/**
83
	 * @dataProvider instanceProvider
84
	 */
85
	public function testGetLongitude( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
86
		$actual = $globeCoordinate->getLongitude();
87
88
		$this->assertInternalType( 'float', $actual );
89
		$this->assertSame( $arguments[0]->getLongitude(), $actual );
90
	}
91
92
	/**
93
	 * @dataProvider instanceProvider
94
	 */
95
	public function testGetPrecision( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
96
		$actual = $globeCoordinate->getPrecision();
97
98
		$this->assertTrue(
99
			is_float( $actual ) || is_int( $actual ) || $actual === null,
100
			'Precision is int or float or null'
101
		);
102
		$this->assertSame( $arguments[1], $actual );
103
	}
104
105
	/**
106
	 * @dataProvider instanceProvider
107
	 */
108
	public function testGetGlobe( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
109
		$expected = isset( $arguments[2] )
110
			? $arguments[2]
111
			: GlobeCoordinateValue::GLOBE_EARTH;
112
113
		$actual = $globeCoordinate->getGlobe();
114
115
		$this->assertTrue(
116
			is_string( $actual ),
117
			'getGlobe should return a string'
118
		);
119
120
		$this->assertSame( $expected, $actual );
121
	}
122
123
	public function provideIllegalArrayValue() {
124
		return [
125
			[ null ],
126
			[ '' ],
127
			[ [] ],
128
			[ [ 'latitude' => 0 ] ],
129
			[ [ 'longitude' => 0 ] ],
130
		];
131
	}
132
133
	/**
134
	 * @dataProvider provideIllegalArrayValue
135
	 */
136
	public function testNewFromArrayErrorHandling( $data ) {
137
		$this->setExpectedException( IllegalValueException::class );
138
		GlobeCoordinateValue::newFromArray( $data );
0 ignored issues
show
Deprecated Code introduced by
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...
139
	}
140
141
	public function testArrayValueCompatibility() {
142
		// These serializations where generated using revision f91f65f989cc3ffacbe924012d8f5b574e0b710c
143
		// The strings are the result of calling getArrayValue on the objects and then feeding this to serialize.
144
145
		$serialization = 'a:5:{s:8:"latitude";d:-4.2000000000000002;'
146
			. 's:9:"longitude";d:42;'
147
			. 's:8:"altitude";N;'
148
			. 's:9:"precision";d:0.01;'
149
			. 's:5:"globe";s:4:"mars";}';
150
151
		$arrayForm = unserialize( $serialization );
152
		$globeCoordinate = GlobeCoordinateValue::newFromArray( $arrayForm );
0 ignored issues
show
Deprecated Code introduced by
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...
153
154
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
155
		$this->assertSame( 42.0, $globeCoordinate->getLongitude() );
156
		$this->assertSame( 0.01, $globeCoordinate->getPrecision() );
157
		$this->assertSame( 'mars', $globeCoordinate->getGlobe() );
158
159
		$serialization = 'a:5:{s:8:"latitude";d:-4.2000000000000002;'
160
			. 's:9:"longitude";d:-42;'
161
			. 's:8:"altitude";d:9001;'
162
			. 's:9:"precision";d:1;'
163
			. 's:5:"globe";s:33:"http://www.wikidata.org/entity/Q2";}';
164
165
		$arrayForm = unserialize( $serialization );
166
		$globeCoordinate = GlobeCoordinateValue::newFromArray( $arrayForm );
0 ignored issues
show
Deprecated Code introduced by
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...
167
168
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
169
		$this->assertSame( -42.0, $globeCoordinate->getLongitude() );
170
		$this->assertSame( 1.0, $globeCoordinate->getPrecision() );
171
		$this->assertSame( 'http://www.wikidata.org/entity/Q2', $globeCoordinate->getGlobe() );
172
	}
173
174
	public function testSerializeCompatibility() {
175
		// These serializations where generated using revision f91f65f989cc3ffacbe924012d8f5b574e0b710c
176
		// The strings are the result of feeding the objects directly into PHPs serialize method.
177
178
		$globeCoordinate = unserialize( 'C:31:"DataValues\GlobeCoordinateValue":27:{[-4.2,-42,null,0.01,"mars"]}' );
179
		$this->assertInstanceOf( $this->getClass(), $globeCoordinate );
180
181
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
182
		$this->assertSame( -42.0, $globeCoordinate->getLongitude() );
183
		$this->assertSame( 0.01, $globeCoordinate->getPrecision() );
184
		$this->assertSame( 'mars', $globeCoordinate->getGlobe() );
185
186
		$globeCoordinate = unserialize( 'C:31:"DataValues\GlobeCoordinateValue":27:{[-4.2,-42,9001,0.01,"mars"]}' );
187
		$this->assertInstanceOf( $this->getClass(), $globeCoordinate );
188
	}
189
190
	public function testHashIsConsistentAcrossDifferentRuntimeEnvironments() {
191
		$latLongValue = new LatLongValue( 12.2, 12.2 );
192
193
		$globeCoordinateValue = new GlobeCoordinateValue( $latLongValue, 0.1, 'does not matter' );
194
195
		$this->assertEquals( '08a33f1bbb4c8bd91b6531b5bffd91fd', $globeCoordinateValue->getHash() );
196
	}
197
198
}
199