Completed
Push — vishual ( 715e64...38235a )
by Jeroen De
05:33
created

JsonFileParserTest::newJsonFileParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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