Skip to content

Commit ca5ec11

Browse files
SpacePossumsebastianbergmann
authored andcommitted
use same file read wrapper function in all tests
1 parent edb896d commit ca5ec11

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

tests/Output/Integration/StrictUnifiedDiffOutputBuilderIntegrationTest.php

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use PHPUnit\Framework\TestCase;
1414
use SebastianBergmann\Diff\Differ;
15+
use SebastianBergmann\Diff\Utils\FileUtils;
1516
use SebastianBergmann\Diff\Utils\UnifiedDiffAssertTrait;
1617
use Symfony\Component\Process\Process;
1718

@@ -37,12 +38,12 @@ final class StrictUnifiedDiffOutputBuilderIntegrationTest extends TestCase
3738

3839
protected function setUp()
3940
{
40-
$this->dir = realpath(__DIR__ . '/../../fixtures/out').'/';
41+
$this->dir = \realpath(__DIR__ . '/../../fixtures/out') . '/';
4142
$this->fileFrom = $this->dir . 'from.txt';
4243
$this->fileTo = $this->dir . 'to.txt';
4344
$this->filePatch = $this->dir . 'diff.patch';
4445

45-
if (!is_dir($this->dir)) {
46+
if (!\is_dir($this->dir)) {
4647
throw new \RuntimeException('Integration test working directory not found.');
4748
}
4849

@@ -64,8 +65,8 @@ protected function setUp()
6465
*/
6566
public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, string $fileTo)
6667
{
67-
$from = self::getFileContent($fileFrom);
68-
$to = self::getFileContent($fileTo);
68+
$from = FileUtils::getFileContent($fileFrom);
69+
$to = FileUtils::getFileContent($fileTo);
6970

7071
$diff = (new Differ(new StrictUnifiedDiffOutputBuilder(['fromFile' => 'Original', 'toFile' => 'New'])))->diff($from, $to);
7172

@@ -94,8 +95,8 @@ public function testIntegrationUsingPHPFileInVendorGitApply(string $fileFrom, st
9495
*/
9596
public function testIntegrationUsingPHPFileInVendorPatch(string $fileFrom, string $fileTo)
9697
{
97-
$from = self::getFileContent($fileFrom);
98-
$to = self::getFileContent($fileTo);
98+
$from = FileUtils::getFileContent($fileFrom);
99+
$to = FileUtils::getFileContent($fileTo);
99100

100101
$diff = (new Differ(new StrictUnifiedDiffOutputBuilder(['fromFile' => 'Original', 'toFile' => 'New'])))->diff($from, $to);
101102

@@ -295,20 +296,4 @@ private static function setDiffFileHeader(string $diff, string $file): string
295296

296297
return \implode('', $diffLines);
297298
}
298-
299-
private static function getFileContent(string $file): string
300-
{
301-
$content = @\file_get_contents($file);
302-
if (false === $content) {
303-
$error = \error_get_last();
304-
305-
throw new \RuntimeException(\sprintf(
306-
'Failed to read content of file "%s".%s',
307-
$file,
308-
$error ? ' ' . $error['message'] : ''
309-
));
310-
}
311-
312-
return $content;
313-
}
314299
}

tests/ParserTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace SebastianBergmann\Diff;
1212

1313
use PHPUnit\Framework\TestCase;
14+
use SebastianBergmann\Diff\Utils\FileUtils;
1415

1516
/**
1617
* @covers SebastianBergmann\Diff\Parser
@@ -33,7 +34,7 @@ protected function setUp()
3334

3435
public function testParse()
3536
{
36-
$content = \file_get_contents(__DIR__ . '/fixtures/patch.txt');
37+
$content = FileUtils::getFileContent(__DIR__ . '/fixtures/patch.txt');
3738

3839
$diffs = $this->parser->parse($content);
3940

@@ -54,7 +55,7 @@ public function testParse()
5455

5556
public function testParseWithMultipleChunks()
5657
{
57-
$content = \file_get_contents(__DIR__ . '/fixtures/patch2.txt');
58+
$content = FileUtils::getFileContent(__DIR__ . '/fixtures/patch2.txt');
5859

5960
$diffs = $this->parser->parse($content);
6061

@@ -166,7 +167,7 @@ public function diffProvider(): array
166167
return [
167168
[
168169
"--- old.txt 2014-11-04 08:51:02.661868729 +0300\n+++ new.txt 2014-11-04 08:51:02.665868730 +0300\n@@ -1,3 +1,4 @@\n+2222111\n 1111111\n 1111111\n 1111111\n@@ -5,10 +6,8 @@\n 1111111\n 1111111\n 1111111\n +1121211\n 1111111\n -1111111\n -1111111\n -2222222\n 2222222\n 2222222\n 2222222\n@@ -17,5 +16,6 @@\n 2222222\n 2222222\n 2222222\n +2122212\n 2222222\n 2222222\n",
169-
\unserialize(\file_get_contents(__DIR__ . '/fixtures/serialized_diff.bin')),
170+
\unserialize(FileUtils::getFileContent(__DIR__ . '/fixtures/serialized_diff.bin')),
170171
],
171172
];
172173
}

tests/Utils/FileUtils.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of sebastian/diff.
4+
*
5+
* (c) Sebastian Bergmann <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace SebastianBergmann\Diff\Utils;
12+
13+
final class FileUtils
14+
{
15+
public static function getFileContent(string $file): string
16+
{
17+
$content = @\file_get_contents($file);
18+
if (false === $content) {
19+
$error = \error_get_last();
20+
21+
throw new \RuntimeException(\sprintf(
22+
'Failed to read content of file "%s".%s',
23+
$file,
24+
$error ? ' ' . $error['message'] : ''
25+
));
26+
}
27+
28+
return $content;
29+
}
30+
}

tests/Utils/UnifiedDiffAssertTraitIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testValidPatches(string $fileFrom, string $fileTo)
7070
)
7171
);
7272

73-
$this->assertValidUnifiedDiffFormat(\file_get_contents($this->filePatch));
73+
$this->assertValidUnifiedDiffFormat(FileUtils::getFileContent($this->filePatch));
7474
}
7575

7676
/**

0 commit comments

Comments
 (0)