Completed
Push — leaflet-cluster ( bc6b2e...f0eec4 )
by Peter
12:42 queued 05:38
created

testGivenCoordinatesAndNoTitle_noTitleIsSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 1
eloc 5
c 3
b 0
f 2
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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
	/**
20
	 * @return string
21
	 */
22
	protected function getParserClass() {
23
		return LocationParser::class;
24
	}
25
26
	/**
27
	 * @see ValueParserTestBase::validInputProvider
28
	 *
29
	 * @since 3.0
30
	 *
31
	 * @return array
32
	 */
33
	public function validInputProvider() {
34
		$argLists = [];
35
36
		$valid = [
37
			'55.7557860 N, 37.6176330 W' => [ 55.7557860, -37.6176330 ],
38
			'55.7557860, -37.6176330' => [ 55.7557860, -37.6176330 ],
39
			'55 S, 37.6176330 W' => [ -55, -37.6176330 ],
40
			'-55, -37.6176330' => [ -55, -37.6176330 ],
41
			'5.5S,37W ' => [ -5.5, -37 ],
42
			'-5.5,-37 ' => [ -5.5, -37 ],
43
			'4,2' => [ 4, 2 ],
44
		];
45
46
		foreach ( $valid as $value => $expected ) {
47
			$expected = new Location( new LatLongValue( $expected[0], $expected[1] ) );
48
			$argLists[] = [ (string)$value, $expected ];
49
		}
50
51
		$location = new Location( new LatLongValue( 4, 2 ) );
52
		$location->setTitle( 'Title' );
53
		$location->setText( 'some description' );
54
		$argLists[] = [ '4,2~Title~some description', $location ];
55
56
		return $argLists;
57
	}
58
59
	/**
60
	 * @see ValueParserTestBase::requireDataValue
61
	 *
62
	 * @since 3.0
63
	 *
64
	 * @return boolean
65
	 */
66
	protected function requireDataValue() {
67
		return false;
68
	}
69
70
	/**
71
	 * @dataProvider titleProvider
72
	 */
73
	public function testGivenTitleThatIsNotLink_titleIsSetAndLinkIsNot( $title ) {
74
		$parser = new LocationParser();
75
		$location = $parser->parse( '4,2~' . $title );
76
77
		$this->assertTitleAndLinkAre( $location, $title, '' );
78
	}
79
80
	public function titleProvider() {
81
		return [
82
			[ '' ],
83
			[ 'Title' ],
84
			[ 'Some title' ],
85
			[ 'link' ],
86
			[ 'links:foo' ],
87
		];
88
	}
89
90
	protected function assertTitleAndLinkAre( Location $location, $title, $link ) {
91
		$this->assertHasJsonKeyWithValue( $location, 'title', $title );
92
		$this->assertHasJsonKeyWithValue( $location, 'link', $link );
93
	}
94
95
	protected function assertHasJsonKeyWithValue( Location $polygon, $key, $value ) {
96
		$json = $polygon->getJSONObject();
97
98
		$this->assertArrayHasKey( $key, $json );
99
		$this->assertEquals(
100
			$value,
101
			$json[$key]
102
		);
103
	}
104
105
	/**
106
	 * @dataProvider linkProvider
107
	 */
108
	public function testGivenTitleThatIsLink_linkIsSetAndTitleIsNot( $link ) {
109
		$parser = new LocationParser();
110
		$location = $parser->parse( '4,2~link:' . $link );
111
112
		$this->assertTitleAndLinkAre( $location, '', $link );
113
	}
114
115
	public function linkProvider() {
116
		return [
117
			[ 'https://semantic-mediawiki.org' ],
118
			[ 'http://www.semantic-mediawiki.org' ],
119
			[ 'irc://freenode.net' ],
120
		];
121
	}
122
123
	/**
124
	 * @dataProvider titleLinkProvider
125
	 */
126
//	public function testGivenPageTitleAsLink_pageTitleIsTurnedIntoUrl( $link ) {
127
//		$parser = new LocationParser();
128
//		$location = $parser->parse( '4,2~link:' . $link );
129
//
130
//		$linkUrl = Title::newFromText( $link )->getFullURL();
131
//		$this->assertTitleAndLinkAre( $location, '', $linkUrl );
132
//	}
133
//
134
//	public function titleLinkProvider() {
135
//		return array(
136
//			array( 'Foo' ),
137
//			array( 'Some_Page' ),
138
//		);
139
//	}
140
141
	protected function getInstance() {
142
		return new LocationParser();
143
	}
144
145
	public function testGivenAddressAndNoTitle_addressIsSetAsTitle() {
146
		$options = new ParserOptions( ['useaddressastitle' => true] );
147
		$parser = new LocationParser( $options );
148
		$location = $parser->parse( 'Tempelhofer Ufer 42' );
149
150
		$this->assertSame( 'Tempelhofer Ufer 42', $location->getTitle() );
151
	}
152
153
	public function testGivenAddressAndTitle_addressIsNotUsedAsTitle() {
154
		$options = new ParserOptions( ['useaddressastitle' => true] );
155
		$parser = new LocationParser( $options );
156
		$location = $parser->parse( 'Tempelhofer Ufer 42~Great title of doom' );
157
158
		$this->assertSame( 'Great title of doom', $location->getTitle() );
159
	}
160
161
	public function testGivenCoordinatesAndNoTitle_noTitleIsSet() {
162
		$options = new ParserOptions( ['useaddressastitle' => true] );
163
		$parser = new LocationParser( $options );
164
		$location = $parser->parse( '4,2' );
165
166
		$this->assertFalse( $location->getOptions()->hasOption( 'title' ) );
167
	}
168
169
}
170