Skip to content

Commit 4f7cdc0

Browse files
SpacePossumsebastianbergmann
authored andcommitted
Intro of WARNING (3), better header handling.
1 parent c7a7330 commit 4f7cdc0

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

src/Differ.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function diffToArray($from, $to, LongestCommonSubsequenceCalculator $lcs
111111
if ($this->detectUnmatchedLineEndings($fromMatches, $toMatches)) {
112112
$diff[] = [
113113
'#Warning: Strings contain different line endings!',
114-
0
114+
3
115115
];
116116
}
117117

src/Output/DiffOnlyOutputBuilder.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ public function __construct(string $header = "--- Original\n+++ New\n")
2828
public function getDiff(array $diff): string
2929
{
3030
$buffer = \fopen('php://memory', 'r+b');
31-
\fwrite($buffer, $this->header);
31+
32+
if ('' !== $this->header) {
33+
\fwrite($buffer, $this->header);
34+
if ("\n" !== \substr($this->header, -1, 1)) {
35+
\fwrite($buffer, "\n");
36+
}
37+
}
3238

3339
foreach ($diff as $diffEntry) {
3440
if ($diffEntry[1] === 1 /* ADDED */) {
3541
\fwrite($buffer, '+' . $diffEntry[0] . "\n");
3642
} elseif ($diffEntry[1] === 2 /* REMOVED */) {
3743
\fwrite($buffer, '-' . $diffEntry[0] . "\n");
38-
}
44+
} elseif ($diffEntry[1] === 3 /* WARNING */) {
45+
\fwrite($buffer, ' ' . $diffEntry[0] . "\n");
46+
} // else { /* Not changed (old) 0 */
3947
}
4048

4149
$diff = \stream_get_contents($buffer, -1, 0);

src/Output/UnifiedDiffOutputBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@ public function getDiff(array $diff): string
4040
}
4141
}
4242

43-
$buffer = $this->header;
43+
$buffer = '';
44+
if ('' !== $this->header) {
45+
$buffer = $this->header;
46+
if ("\n" !== \substr($this->header, -1, 1)) {
47+
$buffer .= "\n";
48+
}
49+
}
4450

4551
if (!isset($old[$start])) {
4652
$buffer = $this->getDiffBufferElementNew($diff, $buffer, $start);
@@ -88,7 +94,7 @@ private function getDiffBufferElement(array $diff, string $buffer, int $diffInde
8894
$buffer .= '+' . $diff[$diffIndex][0] . "\n";
8995
} elseif ($diff[$diffIndex][1] === 2 /* REMOVED */) {
9096
$buffer .= '-' . $diff[$diffIndex][0] . "\n";
91-
} else {
97+
} else { /* Not changed (OLD) 0 or Warning 3 */
9298
$buffer .= ' ' . $diff[$diffIndex][0] . "\n";
9399
}
94100

tests/DifferTest.php

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*/
3131
final class DifferTest extends TestCase
3232
{
33+
const WARNING = 3;
3334
const REMOVED = 2;
3435
const ADDED = 1;
3536
const OLD = 0;
@@ -61,7 +62,7 @@ public function testArrayRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsI
6162
* @param string $to
6263
* @dataProvider textProvider
6364
*/
64-
public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation($expected, $from, $to)
65+
public function testTextRepresentationOfDiffCanBeRenderedUsingTimeEfficientLcsImplementation(string $expected, string $from, string $to)
6566
{
6667
$this->assertSame($expected, $this->differ->diff($from, $to, new TimeEfficientLongestCommonSubsequenceCalculator));
6768
}
@@ -88,16 +89,53 @@ public function testTextRepresentationOfDiffCanBeRenderedUsingMemoryEfficientLcs
8889
$this->assertSame($expected, $this->differ->diff($from, $to, new MemoryEfficientLongestCommonSubsequenceCalculator));
8990
}
9091

91-
public function testCustomHeaderCanBeUsed()
92+
/**
93+
* @param string $expected
94+
* @param string $from
95+
* @param string $to
96+
* @param string $header
97+
* @dataProvider headerProvider
98+
*/
99+
public function testCustomHeaderCanBeUsed(string $expected, string $from, string $to, string $header)
92100
{
93-
$differ = new Differ(new UnifiedDiffOutputBuilder('CUSTOM HEADER'));
101+
$differ = new Differ(new UnifiedDiffOutputBuilder($header));
94102

95103
$this->assertSame(
96-
"CUSTOM HEADER@@ @@\n-a\n+b\n",
97-
$differ->diff('a', 'b')
104+
$expected,
105+
$differ->diff($from, $to)
98106
);
99107
}
100108

109+
public function headerProvider()
110+
{
111+
return [
112+
[
113+
"CUSTOM HEADER\n@@ @@\n-a\n+b\n",
114+
'a',
115+
'b',
116+
'CUSTOM HEADER'
117+
],
118+
[
119+
"CUSTOM HEADER\n@@ @@\n-a\n+b\n",
120+
'a',
121+
'b',
122+
"CUSTOM HEADER\n"
123+
],
124+
[
125+
"CUSTOM HEADER\n\n@@ @@\n-a\n+b\n",
126+
'a',
127+
'b',
128+
"CUSTOM HEADER\n\n"
129+
],
130+
[
131+
"@@ @@\n-a\n+b\n",
132+
'a',
133+
'b',
134+
''
135+
],
136+
];
137+
}
138+
101139
public function testTypesOtherThanArrayAndStringCanBePassed()
102140
{
103141
$this->assertSame(
@@ -245,7 +283,7 @@ public function arrayProvider(): array
245283
[
246284
[
247285
'#Warning: Strings contain different line endings!',
248-
self::OLD,
286+
self::WARNING,
249287
],
250288
[
251289
'<?php',
@@ -363,25 +401,49 @@ public function diffProvider(): array
363401
* @param string $expected
364402
* @param string $from
365403
* @param string $to
404+
* @param string $header
366405
* @dataProvider textForNoNonDiffLinesProvider
367406
*/
368-
public function testDiffDoNotShowNonDiffLines(string $expected, string $from, string $to)
407+
public function testDiffDoNotShowNonDiffLines(string $expected, string $from, string $to, string $header = '')
369408
{
370-
$differ = new Differ(new DiffOnlyOutputBuilder(''));
409+
$differ = new Differ(new DiffOnlyOutputBuilder($header));
410+
371411
$this->assertSame($expected, $differ->diff($from, $to));
372412
}
373413

374414
public function textForNoNonDiffLinesProvider(): array
375415
{
376416
return [
377417
[
378-
'', 'a', 'a'
418+
' #Warning: Strings contain different line endings!
419+
-A
420+
+B
421+
',
422+
"A\r\n",
423+
"B\n",
424+
],
425+
[
426+
'',
427+
'a',
428+
'a'
379429
],
380430
[
381431
"-A\n+C\n",
382432
"A\n\n\nB",
383433
"C\n\n\nB",
384434
],
435+
[
436+
"header\n",
437+
'a',
438+
'a',
439+
'header'
440+
],
441+
[
442+
"header\n",
443+
'a',
444+
'a',
445+
"header\n"
446+
],
385447
];
386448
}
387449

0 commit comments

Comments
 (0)