Passed
Push — geoRange ( af30fa...198bfc )
by no
12:08 queued 08:09
created

GlobeCoordinateValueTest::testGetPrecision()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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