Test Failed
Push — extract ( e8a5db...d01209 )
by Jeroen De
09:33
created

GitHubParserHookTest::testRenderingWithLangBash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
3
namespace GitHub\Tests\System;
4
5
use FileFetcher\FileFetcher;
6
use GitHub\GitHubFetcher;
7
use GitHub\GitHubParserHook;
8
use ParamProcessor\ProcessedParam;
9
use ParamProcessor\ProcessingResult;
10
use PHPUnit\Framework\TestCase;
11
12
/**
13
 * @covers GitHub\GitHubParserHook
14
 *
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class GitHubParserHookTest extends TestCase {
19
20
	private $file;
21
	private $repo;
22
	private $branch;
23
	private $lang;
24
25
	public function setUp() {
26
		$this->file = 'README.md';
27
		$this->repo = 'JeroenDeDauw/GitHub';
28
		$this->branch = 'master';
29
		$this->lang = '';
30
	}
31
32
	public function testUrlGetsBuildCorrectly() {
33
		$fileFetcher = $this->createMock( FileFetcher::class );
34
35
		$fileFetcher->expects( $this->once() )
36
			->method( 'fetchFile' )
37
			->with( 'https://cdn.rawgit.com/JeroenDeDauw/GitHub/master/README.md' );
38
39
		$this->runHookWithFileFetcher( $fileFetcher );
40
	}
41
42
	private function runHookWithFileFetcher( FileFetcher $fileFetcher ) {
43
		$parserHook = new GitHubParserHook( new GitHubFetcher( $fileFetcher, 'https://cdn.rawgit.com' ) );
44
45
		$parser = $this->createMock( 'Parser' );
46
		$params = $this->newParams();
47
48
		return $parserHook->handle( $parser, $params );
49
	}
50
51
	private function newParams() {
52
		return new ProcessingResult( array(
53
			'file' => new ProcessedParam( 'file', $this->file, false ),
54
			'repo' => new ProcessedParam( 'repo', $this->repo, false ),
55
			'branch' => new ProcessedParam( 'branch', $this->branch, false ),
56
			'lang' => new ProcessedParam( 'lang', $this->lang, false ),
57
			'line' => new ProcessedParam( 'line', false, true ),
58
			'start' => new ProcessedParam( 'start', 1, true ),
59
			'highlight' => new ProcessedParam( 'highlight', '', true ),
60
			'inline' => new ProcessedParam( 'inline', false, true ),
61
		) );
62
	}
63
64
	/**
65
	 * @dataProvider makrdownProvider
66
	 */
67
	public function testRenderWithMakrkdownFile( $markdown, $html ) {
68
		$this->assertFileContentRendersAs( $markdown, $html );
69
	}
70
71
	public function makrdownProvider() {
72
		return array(
73
			array(
74
				'# Ohai there!',
75
				"<h1>Ohai there!</h1>\n"
76
			),
77
			array(
78
				'foo bar baz',
79
				"<p>foo bar baz</p>\n"
80
			)
81
		);
82
	}
83
84
	private function assertFileContentRendersAs( $fileContent, $expectedRenderedResult ) {
85
		$fileFetcher = $this->createMock( FileFetcher::class );
86
87
		$fileFetcher->expects( $this->once() )
88
			->method( 'fetchFile' )
89
			->will( $this->returnValue( $fileContent ) );
90
91
		$renderResult = $this->runHookWithFileFetcher( $fileFetcher );
92
93
		$this->assertSame( $expectedRenderedResult, $renderResult );
94
	}
95
96
	public function nonMdProvider() {
97
		return array(
98
			array(
99
				'foo bar baz',
100
				'Foo.php',
101
			),
102
			array(
103
				'# Ohai there!',
104
				'README.wikitext',
105
			),
106
			array(
107
				'{ "you": { "can": "haz", "a": "json!" } }',
108
				'composer.json',
109
			),
110
			array(
111
				'{ "you": { "can": "haz", "a": "json!" } }',
112
				'someFileWithoutExtension',
113
			),
114
		);
115
	}
116
117
	/**
118
	 * @dataProvider nonMdProvider
119
	 */
120
	public function testRenderingWithNonMdFileAsIs( $notMd, $fileName ) {
121
		$this->file = $fileName;
122
		$this->assertFileContentRendersAs( $notMd, $notMd );
123
	}
124
125
	public function testRenderingWithLangBash() {
126
		$this->file = 'hi.sh';
127
		$this->lang = 'bash';
128
129
		$fileFetcher = $this->createMock( FileFetcher::class );
130
131
		$fileFetcher->expects( $this->once() )
132
			->method( 'fetchFile' )
133
			->will( $this->returnValue( '# Ohai there!' ) );
134
135
		$parserHook = new GitHubParserHook( new GitHubFetcher( $fileFetcher, 'https://cdn.rawgit.com' ) );
136
137
		$parser = $this->createMock( 'Parser' );
138
139
		$parser->expects( $this->once() )
140
			->method( 'recursiveTagParse' )
141
			->with( $this->equalTo( '<syntaxhighlight lang="bash" start="1"># Ohai there!</syntaxhighlight>' ) )
142
			->willReturn( null );
143
144
		$this->assertSame( '', $parserHook->handle( $parser, $this->newParams() ) );
145
	}
146
147
	// TODO: syntaxhighlight: prevent content from terminating syntaxhighlight and embedding evil stuff
148
149
}
150