summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Maps/tests/Integration/DataAccess/GeoJsonFetcherTest.php
blob: a3edbb87be68c9f211e519015b3cf362c3505ba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

namespace Maps\Tests\Integration\DataAccess;

use FileFetcher\FileFetcher;
use FileFetcher\NullFileFetcher;
use FileFetcher\SimpleFileFetcher;
use FileFetcher\StubFileFetcher;
use FileFetcher\ThrowingFileFetcher;
use Maps\DataAccess\GeoJsonFetcher;
use Maps\MapsFactory;
use Maps\MediaWiki\Content\GeoJsonContent;
use PHPUnit\Framework\TestCase;
use PHPUnit4And6Compat;
use Title;

/**
 * @covers \Maps\DataAccess\GeoJsonFetcher
 * @licence GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class GeoJsonFetcherTest extends TestCase {
	use PHPUnit4And6Compat;

	private const VALID_FILE_JSON = [
		'type' => 'FeatureCollection',
		'features' => []
	];

	private const VALID_PAGE_JSON = [
		'type' => 'FeatureCollection',
		'features' => []
	];

	private const EXISTING_GEO_JSON_PAGE = 'Test Such';
	private const EXISTING_GEO_JSON_PAGE_WITH_PREFIX = 'GeoJson:Test Such';
	private const NON_EXISTING_GEO_JSON_PAGE = 'GeoJson:Test Nope';

	/**
	 * @var FileFetcher
	 */
	private $fileFetcher;

	public function setUp() {
		$this->fileFetcher = new StubFileFetcher( json_encode( self::VALID_FILE_JSON ) );

		$page = new \WikiPage( Title::newFromText( self::EXISTING_GEO_JSON_PAGE_WITH_PREFIX ) );
		$page->doEditContent( new GeoJsonContent( json_encode( self::VALID_PAGE_JSON ) ), '' );
	}

	private function newJsonFileParser(): GeoJsonFetcher {
		return MapsFactory::newDefault()->newGeoJsonFetcher( $this->fileFetcher );
	}

	public function testWhenFileRetrievalFails_emptyJsonIsReturned() {
		$this->fileFetcher = new ThrowingFileFetcher();

		$this->assertSame(
			[],
			$this->newJsonFileParser()->parse( 'http://such.a/file' )
		);
	}

	public function testWhenFileHasValidJson_jsonIsReturned() {
		$this->fileFetcher = new StubFileFetcher( json_encode( self::VALID_FILE_JSON ) );

		$this->assertEquals(
			self::VALID_FILE_JSON,
			$this->newJsonFileParser()->parse( 'http://such.a/file' )
		);
	}

	public function testWhenFileIsEmpty_emptyJsonIsReturned() {
		$this->fileFetcher = new NullFileFetcher();

		$this->assertSame(
			[],
			$this->newJsonFileParser()->parse( 'http://such.a/file' )
		);
	}

	public function testWhenFileLocationIsNotUrl_emptyJsonIsReturned() {
		$this->fileFetcher = new SimpleFileFetcher();

		$jsonFilePath = __DIR__ . '/../../../composer.json';
		$this->assertFileExists( $jsonFilePath );

		$this->assertSame( [], $this->newJsonFileParser()->parse( $jsonFilePath ) );
	}

	public function testWhenPageExists_itsContentsIsReturned() {
		$this->assertSame(
			self::VALID_PAGE_JSON,
			$this->newJsonFileParser()->parse( self::EXISTING_GEO_JSON_PAGE_WITH_PREFIX )
		);
	}

	public function testWhenPageDoesNotExist_emptyJsonIsReturned() {
		$this->assertSame(
			[],
			$this->newJsonFileParser()->parse( self::NON_EXISTING_GEO_JSON_PAGE )
		);
	}

	public function testWhenExistingPageIsSpecifiedWithoutPrefix_itsContentsIsReturned() {
		$this->assertSame(
			self::VALID_PAGE_JSON,
			$this->newJsonFileParser()->parse( self::EXISTING_GEO_JSON_PAGE )
		);
	}

	public function testPageIsReturnedAsSource() {
		$this->assertSame(
			self::EXISTING_GEO_JSON_PAGE,
			$this->newJsonFileParser()->fetch( self::EXISTING_GEO_JSON_PAGE_WITH_PREFIX )->getTitleValue()->getText()
		);
	}

}