Passed
Push — master ( d0d743...47535d )
by no
03:42
created

testHashIsConsistentAcrossDifferentRuntimeEnvironments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\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
41
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, GlobeCoordinateValue::GLOBE_EARTH );
42
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, 'terminus' );
43
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, "Schar's World" );
44
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, 'coruscant' );
45
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, null );
46
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), null );
47
48
		return $argLists;
49
	}
50
51
	public function invalidConstructorArgumentsProvider() {
52
		$argLists = array();
53
54
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 'foo' );
55
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), true );
56
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), array( 1 ) );
57
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), '1' );
58
59
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, array( 1 ) );
60
		$argLists[] = array( new LatLongValue( 4.2, 4.2 ), 1, 1 );
61
62
		// TODO: test precisions that are out of the valid range
63
64
		return $argLists;
65
	}
66
67
	/**
68
	 * @dataProvider instanceProvider
69
	 * @param GlobeCoordinateValue $globeCoordinate
70
	 * @param array $arguments
71
	 */
72
	public function testGetLatitude( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
73
		$actual = $globeCoordinate->getLatitude();
74
75
		$this->assertInternalType( 'float', $actual );
76
		$this->assertSame( $arguments[0]->getLatitude(), $actual );
77
	}
78
79
	/**
80
	 * @dataProvider instanceProvider
81
	 * @param GlobeCoordinateValue $globeCoordinate
82
	 * @param array $arguments
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
	 * @param GlobeCoordinateValue $globeCoordinate
94
	 * @param array $arguments
95
	 */
96
	public function testGetPrecision( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
97
		$actual = $globeCoordinate->getPrecision();
98
99
		$this->assertTrue(
100
			is_float( $actual ) || is_int( $actual ) || $actual === null,
101
			'Precision is int or float or null'
102
		);
103
		$this->assertSame( $arguments[1], $actual );
104
	}
105
106
	/**
107
	 * @dataProvider instanceProvider
108
	 * @param GlobeCoordinateValue $globeCoordinate
109
	 * @param array $arguments
110
	 */
111
	public function testGetGlobe( GlobeCoordinateValue $globeCoordinate, array $arguments ) {
112
		$expected = isset( $arguments[2] )
113
			? $arguments[2]
114
			: GlobeCoordinateValue::GLOBE_EARTH;
115
116
		$actual = $globeCoordinate->getGlobe();
117
118
		$this->assertTrue(
119
			is_string( $actual ),
120
			'getGlobe should return a string'
121
		);
122
123
		$this->assertSame( $expected, $actual );
124
	}
125
126
	public function testArrayValueCompatibility() {
127
		// These serializations where generated using revision f91f65f989cc3ffacbe924012d8f5b574e0b710c
128
		// The strings are the result of calling getArrayValue on the objects and then feeding this to serialize.
129
130
		$serialization = 'a:5:{s:8:"latitude";d:-4.2000000000000002;'
131
			. 's:9:"longitude";d:42;'
132
			. 's:8:"altitude";N;'
133
			. 's:9:"precision";d:0.01;'
134
			. 's:5:"globe";s:4:"mars";}';
135
136
		$arrayForm = unserialize( $serialization );
137
		$globeCoordinate = GlobeCoordinateValue::newFromArray( $arrayForm );
138
139
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
140
		$this->assertSame( 42.0, $globeCoordinate->getLongitude() );
141
		$this->assertSame( 0.01, $globeCoordinate->getPrecision() );
142
		$this->assertSame( 'mars', $globeCoordinate->getGlobe() );
143
144
		$serialization = 'a:5:{s:8:"latitude";d:-4.2000000000000002;'
145
			. 's:9:"longitude";d:-42;'
146
			. 's:8:"altitude";d:9001;'
147
			. 's:9:"precision";d:1;'
148
			. 's:5:"globe";s:33:"http://www.wikidata.org/entity/Q2";}';
149
150
		$arrayForm = unserialize( $serialization );
151
		$globeCoordinate = GlobeCoordinateValue::newFromArray( $arrayForm );
152
153
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
154
		$this->assertSame( -42.0, $globeCoordinate->getLongitude() );
155
		$this->assertSame( 1.0, $globeCoordinate->getPrecision() );
156
		$this->assertSame( 'http://www.wikidata.org/entity/Q2', $globeCoordinate->getGlobe() );
157
	}
158
159
	public function testSerializeCompatibility() {
160
		// These serializations where generated using revision f91f65f989cc3ffacbe924012d8f5b574e0b710c
161
		// The strings are the result of feeding the objects directly into PHPs serialize method.
162
163
		$globeCoordinate = unserialize( 'C:31:"DataValues\GlobeCoordinateValue":27:{[-4.2,-42,null,0.01,"mars"]}' );
164
		$this->assertInstanceOf( $this->getClass(), $globeCoordinate );
165
166
		$this->assertSame( -4.2, $globeCoordinate->getLatitude() );
167
		$this->assertSame( -42.0, $globeCoordinate->getLongitude() );
168
		$this->assertSame( 0.01, $globeCoordinate->getPrecision() );
169
		$this->assertSame( 'mars', $globeCoordinate->getGlobe() );
170
171
		$globeCoordinate = unserialize( 'C:31:"DataValues\GlobeCoordinateValue":27:{[-4.2,-42,9001,0.01,"mars"]}' );
172
		$this->assertInstanceOf( $this->getClass(), $globeCoordinate );
173
	}
174
175
	public function testHashIsConsistentAcrossDifferentRuntimeEnvironments() {
176
		$latLongValue = new LatLongValue( 12.2, 12.2 );
177
178
		$globeCoordinateValue = new GlobeCoordinateValue( $latLongValue, 0.1, 'does not matter' );
179
180
		$this->assertEquals( '6cb12c61f8d4dce378b0e079aac2c780', $globeCoordinateValue->getHash() );
181
	}
182
183
}
184