Skip to content

Port changes from upstream #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/Chunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class Chunk
private $endRange;

/**
* @var array
* @var Line[]
*/
private $lines;

Expand Down Expand Up @@ -66,13 +66,25 @@ public function getEndRange()
return $this->endRange;
}

/**
* @return Line[]
*/
public function getLines()
{
return $this->lines;
}

/**
* @param Line[] $lines
*/
public function setLines(array $lines)
{
foreach ($lines as $line) {
if (!$line instanceof Line) {
throw new InvalidArgumentException;
}
}

$this->lines = $lines;
}
}
15 changes: 11 additions & 4 deletions tests/ChunkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

/**
* @covers PhpCsFixer\Diff\Chunk
*
* @uses PhpCsFixer\Diff\Line
*/
final class ChunkTest extends TestCase
{
Expand All @@ -27,6 +29,11 @@ protected function setUp()
$this->chunk = new Chunk;
}

public function testHasInitiallyNoLines()
{
$this->assertSame([], $this->chunk->getLines());
}

public function testCanBeCreatedWithoutArguments()
{
$this->assertInstanceOf(Chunk::class, $this->chunk);
Expand Down Expand Up @@ -59,10 +66,10 @@ public function testLinesCanBeRetrieved()

public function testLinesCanBeSet()
{
$this->assertSame([], $this->chunk->getLines());
$lines = [new Line(Line::ADDED, 'added'), new Line(Line::REMOVED, 'removed')];

$this->chunk->setLines($lines);

$testValue = ['line0', 'line1'];
$this->chunk->setLines($testValue);
$this->assertSame($testValue, $this->chunk->getLines());
$this->assertSame($lines, $this->chunk->getLines());
}
}
2 changes: 1 addition & 1 deletion tests/LongestCommonSubsequenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class LongestCommonSubsequenceTest extends TestCase
protected function setUp()
{
$this->memoryLimit = \ini_get('memory_limit');
\ini_set('memory_limit', '256M');
\ini_set('memory_limit', '-1');

$this->implementation = $this->createImplementation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @uses PhpCsFixer\Diff\Differ
* @uses PhpCsFixer\Diff\TimeEfficientLongestCommonSubsequenceCalculator
* @uses PhpCsFixer\Diff\MemoryEfficientLongestCommonSubsequenceCalculator
*
* @requires OS Linux
*/
Expand Down
2 changes: 2 additions & 0 deletions tests/Output/StrictUnifiedDiffOutputBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @covers PhpCsFixer\Diff\Output\StrictUnifiedDiffOutputBuilder
*
* @uses PhpCsFixer\Diff\Differ
* @uses PhpCsFixer\Diff\TimeEfficientLongestCommonSubsequenceCalculator
* @uses PhpCsFixer\Diff\ConfigurationException
*/
final class StrictUnifiedDiffOutputBuilderTest extends TestCase
{
Expand Down
13 changes: 4 additions & 9 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ public function testParse()

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

$this->assertInternalType('array', $diffs);
$this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
$this->assertCount(1, $diffs);

$chunks = $diffs[0]->getChunks();
$this->assertInternalType('array', $chunks);
$this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);

$this->assertCount(1, $chunks);
Expand Down Expand Up @@ -75,23 +73,21 @@ public function testParseWithMultipleChunks()

public function testParseWithRemovedLines()
{
$content = <<<A
$content = <<<END
diff --git a/Test.txt b/Test.txt
index abcdefg..abcdefh 100644
--- a/Test.txt
+++ b/Test.txt
@@ -49,9 +49,8 @@
A
-B
A;
END;
$diffs = $this->parser->parse($content);
$this->assertInternalType('array', $diffs);
$this->assertContainsOnlyInstancesOf(Diff::class, $diffs);
$this->assertCount(1, $diffs);

$chunks = $diffs[0]->getChunks();

$this->assertInternalType('array', $chunks);
$this->assertContainsOnlyInstancesOf(Chunk::class, $chunks);
$this->assertCount(1, $chunks);

Expand All @@ -102,7 +98,6 @@ public function testParseWithRemovedLines()
$this->assertSame(8, $chunk->getEndRange());

$lines = $chunk->getLines();
$this->assertInternalType('array', $lines);
$this->assertContainsOnlyInstancesOf(Line::class, $lines);
$this->assertCount(2, $lines);

Expand All @@ -118,7 +113,7 @@ public function testParseWithRemovedLines()

public function testParseDiffForMulitpleFiles()
{
$content = <<<A
$content = <<<END
diff --git a/Test.txt b/Test.txt
index abcdefg..abcdefh 100644
--- a/Test.txt
Expand All @@ -134,7 +129,7 @@ public function testParseDiffForMulitpleFiles()
@@ -1,2 +1,3 @@
A
+B
A;
END;
$diffs = $this->parser->parse($content);
$this->assertCount(2, $diffs);

Expand Down
3 changes: 3 additions & 0 deletions tests/Utils/UnifiedDiffAssertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function assertValidUnifiedDiffFormat($diff)

// test diff ends with a line break
$last = \substr($diff, -1);

if ("\n" !== $last && "\r" !== $last) {
throw new \UnexpectedValueException(\sprintf('Expected diff to end with a line break, got "%s".', $last));
}
Expand Down Expand Up @@ -130,6 +131,7 @@ public function assertValidUnifiedDiffFormat($diff)
}

$previousType = $this->unifiedDiffAssertLinePrefix($lines[$lineNumber - 2], \sprintf('Preceding line of "\\ No newline at end of file" of unexpected format. Line %d.', $lineNumber));

if (isset($endOfLineTypes[$previousType])) {
throw new \UnexpectedValueException(\sprintf('Unexpected "\\ No newline at end of file", "%s" was already closed. Line %d.', $type, $lineNumber));
}
Expand Down Expand Up @@ -190,6 +192,7 @@ private function unifiedDiffAssertLinePrefix($line, $message)
private function unifiedDiffAssertStrLength($line, $min, $message)
{
$length = \strlen($line);

if ($length < $min) {
throw new \UnexpectedValueException(\sprintf('Expected string length of minimal %d, got %d. %s', $min, $length, $message));
}
Expand Down