Completed
Push — LocationParsers ( afa02e )
by Jeroen De
04:46
created

LocationParserTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 102
rs 10
c 1
b 1
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 2
A testGivenTitleThatIsNotLink_titleIsSetAndLinkIsNot() 0 6 1
A titleProvider() 0 9 1
A assertTitleAndLinkAre() 0 4 1
A assertHasJsonKeyWithValue() 0 6 1
A testGivenTitleThatIsLink_linkIsSetAndTitleIsNot() 0 6 1
A linkProvider() 0 7 1
A testGivenAddressAndNoTitle_addressIsSetAsTitle() 0 8 1
A testGivenAddressAndTitle_addressIsNotUsedAsTitle() 0 8 1
A testGivenCoordinatesAndNoTitle_noTitleIsSet() 0 6 1
1
<?php
2
3
namespace Maps\Test;
4
5
use DataValues\Geo\Values\LatLongValue;
6
use Maps\Elements\Location;
7
use Maps\Geocoders\NullGeocoder;
8
use Maps\Geocoders\StubGeocoder;
9
use Maps\LocationParser;
10
11
/**
12
 * @covers \Maps\LocationParser
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class LocationParserTest extends \PHPUnit_Framework_TestCase {
17
18
	public function setUp() {
19
		if ( !defined( 'MEDIAWIKI' ) ) {
20
			$this->markTestSkipped( 'MediaWiki is not available' );
21
		}
22
	}
23
24
	/**
25
	 * @dataProvider titleProvider
26
	 */
27
	public function testGivenTitleThatIsNotLink_titleIsSetAndLinkIsNot( $title ) {
28
		$parser = LocationParser::newInstance( new NullGeocoder() );
29
		$location = $parser->parse( '4,2~' . $title );
30
31
		$this->assertTitleAndLinkAre( $location, $title, '' );
32
	}
33
34
	public function titleProvider() {
35
		return [
36
			[ '' ],
37
			[ 'Title' ],
38
			[ 'Some title' ],
39
			[ 'link' ],
40
			[ 'links:foo' ],
41
		];
42
	}
43
44
	protected function assertTitleAndLinkAre( Location $location, $title, $link ) {
45
		$this->assertHasJsonKeyWithValue( $location, 'title', $title );
46
		$this->assertHasJsonKeyWithValue( $location, 'link', $link );
47
	}
48
49
	protected function assertHasJsonKeyWithValue( Location $polygon, $key, $value ) {
50
		$json = $polygon->getJSONObject();
51
52
		$this->assertArrayHasKey( $key, $json );
53
		$this->assertEquals( $value, $json[$key] );
54
	}
55
56
	/**
57
	 * @dataProvider linkProvider
58
	 */
59
	public function testGivenTitleThatIsLink_linkIsSetAndTitleIsNot( $link ) {
60
		$parser = LocationParser::newInstance( new NullGeocoder() );
61
		$location = $parser->parse( '4,2~link:' . $link );
62
63
		$this->assertTitleAndLinkAre( $location, '', $link );
64
	}
65
66
	public function linkProvider() {
67
		return [
68
			[ 'https://semantic-mediawiki.org' ],
69
			[ 'http://www.semantic-mediawiki.org' ],
70
			[ 'irc://freenode.net' ],
71
		];
72
	}
73
74
//	/**
75
//	 * @dataProvider titleLinkProvider
76
//	 */
77
//	public function testGivenPageTitleAsLink_pageTitleIsTurnedIntoUrl( $link ) {
78
//		$parser = new LocationParser();
79
//		$location = $parser->parse( '4,2~link:' . $link );
80
//
81
//		$linkUrl = Title::newFromText( $link )->getFullURL();
82
//		$this->assertTitleAndLinkAre( $location, '', $linkUrl );
83
//	}
84
//
85
//	public function titleLinkProvider() {
86
//		return array(
87
//			array( 'Foo' ),
88
//			array( 'Some_Page' ),
89
//		);
90
//	}
91
92
	public function testGivenAddressAndNoTitle_addressIsSetAsTitle() {
93
		$geocoder = new StubGeocoder( new LatLongValue( 4, 2 ) );
94
95
		$parser = LocationParser::newInstance( $geocoder, true );
96
		$location = $parser->parse( 'Tempelhofer Ufer 42' );
97
98
		$this->assertSame( 'Tempelhofer Ufer 42', $location->getTitle() );
99
	}
100
101
	public function testGivenAddressAndTitle_addressIsNotUsedAsTitle() {
102
		$geocoder = new StubGeocoder( new LatLongValue( 4, 2 ) );
103
104
		$parser = LocationParser::newInstance( $geocoder, true );
105
		$location = $parser->parse( 'Tempelhofer Ufer 42~Great title of doom' );
106
107
		$this->assertSame( 'Great title of doom', $location->getTitle() );
108
	}
109
110
	public function testGivenCoordinatesAndNoTitle_noTitleIsSet() {
111
		$parser = LocationParser::newInstance( new NullGeocoder(), true );
112
		$location = $parser->parse( '4,2' );
113
114
		$this->assertFalse( $location->getOptions()->hasOption( 'title' ) );
115
	}
116
117
}
118