-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApprovals.php
executable file
·97 lines (86 loc) · 3.28 KB
/
Approvals.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
class Approvals {
private static $reporter = null;
public static function approveString($received) {
self::approve(new TextWriter($received, 'txt'), new PHPUnitNamer(), self::getReporter('txt'));
}
public static function getReporter($extensionWithoutDot) {
if (is_null(self::$reporter)) {
switch($extensionWithoutDot) {
case 'html':
case 'pdf':
return new OpenReceivedFileReporter();
break;
default:
return new PHPUnitReporter();
}
}
return self::$reporter;
}
public static function useReporter(Reporter $reporter) {
self::$reporter = $reporter;
}
public static function approve(Writer $writer, $namer, Reporter $reporter) {
$extension = $writer->getExtensionWithoutDot();
$approvedFilename = $namer->getApprovedFile($extension);
$receivedFilename = $writer->write($namer->getReceivedFile($extension));
if (!file_exists($approvedFilename)) {
$approvedContents = null;
} else {
$approvedContents = file_get_contents($approvedFilename);
}
$receivedContents = file_get_contents($receivedFilename);
if ($approvedContents === $receivedContents) {
unlink($receivedFilename);
} else {
$hint = "\n------ To Approve, use the following command ------\n";
$hint .= 'mv -v "' . addslashes($receivedFilename) . '" "' . addslashes($approvedFilename) . "\"\n";
$hint .= "\n\n";
$reporter->reportFailure($approvedFilename, $receivedFilename);
throw new RuntimeException('Approval File Mismatch: ' . $receivedFilename . ' does not match ' . $approvedFilename . $hint);
}
}
/**
* @todo Refactor - Too much duplication with approve().
*/
public static function approvePartial(Writer $writer, $namer, Reporter $reporter) {
$extension = $writer->getExtensionWithoutDot();
$approvedFilename = $namer->getApprovedFile($extension);
$receivedFilename = $writer->write($namer->getReceivedFile($extension));
if (!file_exists($approvedFilename)) {
$approvedContents = null;
} else {
$approvedContents = file_get_contents($approvedFilename);
}
$receivedContents = file_get_contents($receivedFilename);
if (strstr($receivedContents, $approvedContents)) {
unlink($receivedFilename);
} else {
$hint = "\n------ To Approve, use the following command ------\n";
$hint .= 'mv -v "' . addslashes($receivedFilename) . '" "' . addslashes($approvedFilename) . "\"\n";
$hint .= "\n\n";
$reporter->reportFailure($approvedFilename, $receivedFilename);
throw new RuntimeException('Approval File Mismatch: ' . $receivedFilename . ' does not contain ' . $approvedFilename . $hint);
}
}
public static function approveTransformedList(array $list, $callbackObject, $methodName) {
$string = '';
foreach($list as $item) {
$string .= $item . ' -> ' . $callbackObject->$methodName($item) . "\n";
}
self::approveString($string);
}
public static function approveList(array $list) {
$string = '';
foreach($list as $key => $item) {
$string .= '[' . $key . '] -> ' . $item . "\n";
}
self::approveString($string);
}
public static function approveHtml($html) {
self::approve(new TextWriter($html, 'html'), new PHPUnitNamer(), self::getReporter('html'));
}
public static function approvePartialHtml($html) {
self::approvePartial(new TextWriter($html, 'html'), new PHPUnitNamer(), self::getReporter('html'));
}
}