Completed
Push — master ( 21f028...112b84 )
by Jeroen De
03:13
created

LocationParserTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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