Completed
Push — address-as-title ( 601934...feca68 )
by Peter
09:00
created

LocationParserTest::getParserClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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