Completed
Push — merge-sm ( 44b830...c70fa4 )
by Jeroen De
10:03 queued 07:12
created

LocationParserTest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 2
Metric Value
dl 0
loc 159
rs 10
c 2
b 0
f 2
wmc 16
lcom 1
cbo 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 2
A getParserClass() 0 3 1
B validInputProvider() 0 25 2
A requireDataValue() 0 3 1
A testGivenTitleThatIsNotLink_titleIsSetAndLinkIsNot() 0 6 1
A titleProvider() 0 9 1
A assertTitleAndLinkAre() 0 4 1
A assertHasJsonKeyWithValue() 0 9 1
A testGivenTitleThatIsLink_linkIsSetAndTitleIsNot() 0 6 1
A linkProvider() 0 7 1
A getInstance() 0 3 1
A testGivenAddressAndNoTitle_addressIsSetAsTitle() 0 7 1
A testGivenAddressAndTitle_addressIsNotUsedAsTitle() 0 7 1
A testGivenCoordinatesAndNoTitle_noTitleIsSet() 0 7 1
1
<?php
2
3
namespace Maps\Test;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use Maps\Elements\Location;
7
use Maps\LocationParser;
8
use Title;
9
use ValueParsers\ParserOptions;
10
use ValueParsers\ValueParser;
11
12
/**
13
 * @covers Maps\LocationParser
14
 * @licence GNU GPL v2+
15
 * @author Jeroen De Dauw < [email protected] >
16
 */
17
class LocationParserTest extends \ValueParsers\Test\StringValueParserTest {
18
19
	public function setUp() {
20
		if ( !defined( 'MEDIAWIKI' ) ) {
21
			$this->markTestSkipped( 'MediaWiki is not available' );
22
		}
23
	}
24
25
	/**
26
	 * @return string
27
	 */
28
	protected function getParserClass() {
29
		return LocationParser::class;
30
	}
31
32
	/**
33
	 * @see ValueParserTestBase::validInputProvider
34
	 *
35
	 * @since 3.0
36
	 *
37
	 * @return array
38
	 */
39
	public function validInputProvider() {
40
		$argLists = [];
41
42
		$valid = [
43
			'55.7557860 N, 37.6176330 W' => [ 55.7557860, -37.6176330 ],
44
			'55.7557860, -37.6176330' => [ 55.7557860, -37.6176330 ],
45
			'55 S, 37.6176330 W' => [ -55, -37.6176330 ],
46
			'-55, -37.6176330' => [ -55, -37.6176330 ],
47
			'5.5S,37W ' => [ -5.5, -37 ],
48
			'-5.5,-37 ' => [ -5.5, -37 ],
49
			'4,2' => [ 4, 2 ],
50
		];
51
52
		foreach ( $valid as $value => $expected ) {
53
			$expected = new Location( new LatLongValue( $expected[0], $expected[1] ) );
54
			$argLists[] = [ (string)$value, $expected ];
55
		}
56
57
		$location = new Location( new LatLongValue( 4, 2 ) );
58
		$location->setTitle( 'Title' );
59
		$location->setText( 'some description' );
60
		$argLists[] = [ '4,2~Title~some description', $location ];
61
62
		return $argLists;
63
	}
64
65
	/**
66
	 * @see ValueParserTestBase::requireDataValue
67
	 *
68
	 * @since 3.0
69
	 *
70
	 * @return boolean
71
	 */
72
	protected function requireDataValue() {
73
		return false;
74
	}
75
76
	/**
77
	 * @dataProvider titleProvider
78
	 */
79
	public function testGivenTitleThatIsNotLink_titleIsSetAndLinkIsNot( $title ) {
80
		$parser = new LocationParser();
81
		$location = $parser->parse( '4,2~' . $title );
82
83
		$this->assertTitleAndLinkAre( $location, $title, '' );
84
	}
85
86
	public function titleProvider() {
87
		return [
88
			[ '' ],
89
			[ 'Title' ],
90
			[ 'Some title' ],
91
			[ 'link' ],
92
			[ 'links:foo' ],
93
		];
94
	}
95
96
	protected function assertTitleAndLinkAre( Location $location, $title, $link ) {
97
		$this->assertHasJsonKeyWithValue( $location, 'title', $title );
98
		$this->assertHasJsonKeyWithValue( $location, 'link', $link );
99
	}
100
101
	protected function assertHasJsonKeyWithValue( Location $polygon, $key, $value ) {
102
		$json = $polygon->getJSONObject();
103
104
		$this->assertArrayHasKey( $key, $json );
105
		$this->assertEquals(
106
			$value,
107
			$json[$key]
108
		);
109
	}
110
111
	/**
112
	 * @dataProvider linkProvider
113
	 */
114
	public function testGivenTitleThatIsLink_linkIsSetAndTitleIsNot( $link ) {
115
		$parser = new LocationParser();
116
		$location = $parser->parse( '4,2~link:' . $link );
117
118
		$this->assertTitleAndLinkAre( $location, '', $link );
119
	}
120
121
	public function linkProvider() {
122
		return [
123
			[ 'https://semantic-mediawiki.org' ],
124
			[ 'http://www.semantic-mediawiki.org' ],
125
			[ 'irc://freenode.net' ],
126
		];
127
	}
128
129
	/**
130
	 * @dataProvider titleLinkProvider
131
	 */
132
//	public function testGivenPageTitleAsLink_pageTitleIsTurnedIntoUrl( $link ) {
133
//		$parser = new LocationParser();
134
//		$location = $parser->parse( '4,2~link:' . $link );
135
//
136
//		$linkUrl = Title::newFromText( $link )->getFullURL();
137
//		$this->assertTitleAndLinkAre( $location, '', $linkUrl );
138
//	}
139
//
140
//	public function titleLinkProvider() {
141
//		return array(
142
//			array( 'Foo' ),
143
//			array( 'Some_Page' ),
144
//		);
145
//	}
146
147
	protected function getInstance() {
148
		return new LocationParser();
149
	}
150
151
	public function testGivenAddressAndNoTitle_addressIsSetAsTitle() {
152
		$options = new ParserOptions( ['useaddressastitle' => true] );
153
		$parser = new LocationParser( $options );
154
		$location = $parser->parse( 'Tempelhofer Ufer 42' );
155
156
		$this->assertSame( 'Tempelhofer Ufer 42', $location->getTitle() );
157
	}
158
159
	public function testGivenAddressAndTitle_addressIsNotUsedAsTitle() {
160
		$options = new ParserOptions( ['useaddressastitle' => true] );
161
		$parser = new LocationParser( $options );
162
		$location = $parser->parse( 'Tempelhofer Ufer 42~Great title of doom' );
163
164
		$this->assertSame( 'Great title of doom', $location->getTitle() );
165
	}
166
167
	public function testGivenCoordinatesAndNoTitle_noTitleIsSet() {
168
		$options = new ParserOptions( ['useaddressastitle' => true] );
169
		$parser = new LocationParser( $options );
170
		$location = $parser->parse( '4,2' );
171
172
		$this->assertFalse( $location->getOptions()->hasOption( 'title' ) );
173
	}
174
175
}
176