Completed
Push — master ( 14218a...10a4b7 )
by Jeroen De
03:39
created

GitHubParserHookTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 0 Features 1
Metric Value
wmc 9
c 4
b 0
f 1
lcom 1
cbo 3
dl 0
loc 101
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testUrlGetsBuildCorrectly() 0 9 1
A runHookWithFileFetcher() 0 8 1
A newParams() 0 7 1
A makrdownProvider() 0 12 1
A testRenderWithMakrkdownFile() 0 3 1
A assertFileContentRendersAs() 0 11 1
A nonMdProvider() 0 20 1
A testRenderingWithNonMdFileAsIs() 0 4 1
1
<?php
2
3
namespace GitHub\Tests\System;
4
5
use FileFetcher\FileFetcher;
6
use GitHub\GitHubParserHook;
7
use ParamProcessor\ProcessedParam;
8
use ParamProcessor\ProcessingResult;
9
10
/**
11
 * @covers GitHub\GitHubParserHook
12
 *
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class GitHubParserHookTest extends \PHPUnit_Framework_TestCase {
17
18
	private $file;
19
	private $repo;
20
	private $branch;
21
22
	public function setUp() {
23
		$this->file = 'README.md';
24
		$this->repo = 'JeroenDeDauw/GitHub';
25
		$this->branch = 'master';
26
	}
27
28
	public function testUrlGetsBuildCorrectly() {
29
		$fileFetcher = $this->getMock( 'FileFetcher\FileFetcher' );
30
31
		$fileFetcher->expects( $this->once() )
32
			->method( 'fetchFile' )
33
			->with( 'https://cdn.rawgit.com/JeroenDeDauw/GitHub/master/README.md' );
34
35
		$this->runHookWithFileFetcher( $fileFetcher );
36
	}
37
38
	private function runHookWithFileFetcher( FileFetcher $fileFetcher ) {
39
		$parserHook = new GitHubParserHook( $fileFetcher, 'https://cdn.rawgit.com' );
40
41
		$parser = $this->getMock( 'Parser' );
42
		$params = $this->newParams();
43
44
		return $renderResult = $parserHook->handle( $parser, $params );
0 ignored issues
show
Unused Code introduced by
$renderResult is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
	}
46
47
	private function newParams() {
48
		return $params = new ProcessingResult( array(
0 ignored issues
show
Unused Code introduced by
$params is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
			'file' => new ProcessedParam( 'file', $this->file, false ),
50
			'repo' => new ProcessedParam( 'repo', $this->repo, false ),
51
			'branch' => new ProcessedParam( 'branch', $this->branch, true ),
52
		) );
53
	}
54
55
	public function makrdownProvider() {
56
		return array(
57
			array(
58
				'# Ohai there!',
59
				"<h1>Ohai there!</h1>\n"
60
			),
61
			array(
62
				'foo bar baz',
63
				"<p>foo bar baz</p>\n"
64
			)
65
		);
66
	}
67
68
	/**
69
	 * @dataProvider makrdownProvider
70
	 */
71
	public function testRenderWithMakrkdownFile( $markdown, $html ) {
72
		$this->assertFileContentRendersAs( $markdown, $html );
73
	}
74
75
	private function assertFileContentRendersAs( $fileContent, $expectedRenderedResult ) {
76
		$fileFetcher = $this->getMock( 'FileFetcher\FileFetcher' );
77
78
		$fileFetcher->expects( $this->once() )
79
			->method( 'fetchFile' )
80
			->will( $this->returnValue( $fileContent ) );
81
82
		$renderResult = $this->runHookWithFileFetcher( $fileFetcher );
83
84
		$this->assertEquals( $expectedRenderedResult, $renderResult );
85
	}
86
87
	public function nonMdProvider() {
88
		return array(
89
			array(
90
				'foo bar baz',
91
				'Foo.php',
92
			),
93
			array(
94
				'# Ohai there!',
95
				'README.wikitext',
96
			),
97
			array(
98
				'{ "you": { "can": "haz", "a": "json!" } }',
99
				'composer.json',
100
			),
101
			array(
102
				'{ "you": { "can": "haz", "a": "json!" } }',
103
				'someFileWithoutExtension',
104
			),
105
		);
106
	}
107
108
	/**
109
	 * @dataProvider nonMdProvider
110
	 */
111
	public function testRenderingWithNonMdFileAsIs( $notMd, $fileName ) {
112
		$this->file = $fileName;
113
		$this->assertFileContentRendersAs( $notMd, $notMd );
114
	}
115
116
}
117