Completed
Push — MediaWikiFileUrlFinderTest ( d58bee...394f02 )
by Jeroen De
08:48
created

LocationParserTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 98
rs 10
c 0
b 0
f 0

9 Methods

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