Completed
Push — styling ( 65b228 )
by Jeroen De
04:06
created

GeoJsonFetcherTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

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

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A newJsonFileParser() 0 3 1
A testWhenFileRetrievalFails_emptyJsonIsReturned() 0 8 1
A testWhenFileHasValidJson_jsonIsReturned() 0 8 1
A testWhenFileIsEmpty_emptyJsonIsReturned() 0 8 1
A testWhenFileLocationIsNotUrl_emptyJsonIsReturned() 0 8 1
A testWhenPageExists_itsContentsIsReturned() 0 6 1
A testWhenPageDoesNotExist_emptyJsonIsReturned() 0 6 1
A testWhenExistingPageIsSpecifiedWithoutPrefix_itsContentsIsReturned() 0 6 1
A testPageIsReturnedAsSource() 0 6 1
1
<?php
2
3
namespace Maps\Tests\Integration\DataAccess;
4
5
use FileFetcher\FileFetcher;
6
use FileFetcher\NullFileFetcher;
7
use FileFetcher\SimpleFileFetcher;
8
use FileFetcher\StubFileFetcher;
9
use FileFetcher\ThrowingFileFetcher;
10
use Maps\DataAccess\GeoJsonFetcher;
11
use Maps\MapsFactory;
12
use Maps\MediaWiki\Content\GeoJsonContent;
13
use PHPUnit\Framework\TestCase;
14
use PHPUnit4And6Compat;
15
use Title;
16
17
/**
18
 * @covers \Maps\DataAccess\GeoJsonFetcher
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class GeoJsonFetcherTest extends TestCase {
23
	use PHPUnit4And6Compat;
24
25
	private const VALID_FILE_JSON = [
26
		'type' => 'FeatureCollection',
27
		'features' => []
28
	];
29
30
	private const VALID_PAGE_JSON = [
31
		'type' => 'FeatureCollection',
32
		'features' => []
33
	];
34
35
	private const EXISTING_GEO_JSON_PAGE = 'Test Such';
36
	private const EXISTING_GEO_JSON_PAGE_WITH_PREFIX = 'GeoJson:Test Such';
37
	private const NON_EXISTING_GEO_JSON_PAGE = 'GeoJson:Test Nope';
38
39
	/**
40
	 * @var FileFetcher
41
	 */
42
	private $fileFetcher;
43
44
	public function setUp() {
45
		$this->fileFetcher = new StubFileFetcher( json_encode( self::VALID_FILE_JSON ) );
46
47
		$page = new \WikiPage( Title::newFromText( self::EXISTING_GEO_JSON_PAGE_WITH_PREFIX ) );
48
		$page->doEditContent( new GeoJsonContent( json_encode( self::VALID_PAGE_JSON ) ), '' );
49
	}
50
51
	private function newJsonFileParser(): GeoJsonFetcher {
52
		return MapsFactory::newDefault()->newGeoJsonFetcher( $this->fileFetcher );
53
	}
54
55
	public function testWhenFileRetrievalFails_emptyJsonIsReturned() {
56
		$this->fileFetcher = new ThrowingFileFetcher();
57
58
		$this->assertSame(
59
			[],
60
			$this->newJsonFileParser()->parse( 'http://such.a/file' )
61
		);
62
	}
63
64
	public function testWhenFileHasValidJson_jsonIsReturned() {
65
		$this->fileFetcher = new StubFileFetcher( json_encode( self::VALID_FILE_JSON ) );
66
67
		$this->assertEquals(
68
			self::VALID_FILE_JSON,
69
			$this->newJsonFileParser()->parse( 'http://such.a/file' )
70
		);
71
	}
72
73
	public function testWhenFileIsEmpty_emptyJsonIsReturned() {
74
		$this->fileFetcher = new NullFileFetcher();
75
76
		$this->assertSame(
77
			[],
78
			$this->newJsonFileParser()->parse( 'http://such.a/file' )
79
		);
80
	}
81
82
	public function testWhenFileLocationIsNotUrl_emptyJsonIsReturned() {
83
		$this->fileFetcher = new SimpleFileFetcher();
84
85
		$jsonFilePath = __DIR__ . '/../../../composer.json';
86
		$this->assertFileExists( $jsonFilePath );
87
88
		$this->assertSame( [], $this->newJsonFileParser()->parse( $jsonFilePath ) );
89
	}
90
91
	public function testWhenPageExists_itsContentsIsReturned() {
92
		$this->assertSame(
93
			self::VALID_PAGE_JSON,
94
			$this->newJsonFileParser()->parse( self::EXISTING_GEO_JSON_PAGE_WITH_PREFIX )
95
		);
96
	}
97
98
	public function testWhenPageDoesNotExist_emptyJsonIsReturned() {
99
		$this->assertSame(
100
			[],
101
			$this->newJsonFileParser()->parse( self::NON_EXISTING_GEO_JSON_PAGE )
102
		);
103
	}
104
105
	public function testWhenExistingPageIsSpecifiedWithoutPrefix_itsContentsIsReturned() {
106
		$this->assertSame(
107
			self::VALID_PAGE_JSON,
108
			$this->newJsonFileParser()->parse( self::EXISTING_GEO_JSON_PAGE )
109
		);
110
	}
111
112
	public function testPageIsReturnedAsSource() {
113
		$this->assertSame(
114
			self::EXISTING_GEO_JSON_PAGE,
115
			$this->newJsonFileParser()->fetch( self::EXISTING_GEO_JSON_PAGE_WITH_PREFIX )->getTitleValue()->getText()
116
		);
117
	}
118
119
}
120