|
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 ); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function newParams() { |
|
48
|
|
|
return $params = new ProcessingResult( array( |
|
|
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.