Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Diff shows only changed lines (#3242)
Diff shows only changed lines The diff used to show both complete strings instead of only the changed lines, I've added a unit test for the case and refactored a bit inside the Console.php
- Loading branch information
1 parent
c05618b
commit 8192e36
Showing
7 changed files
with
339 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
namespace Codeception\Lib\Console; | ||
|
||
/** | ||
* Colorizer | ||
**/ | ||
class Colorizer | ||
{ | ||
/** | ||
* @param string $string | ||
* @return string | ||
*/ | ||
public function colorize($string = '') | ||
{ | ||
$fp = fopen('php://memory', 'r+'); | ||
fwrite($fp, $string); | ||
rewind($fp); | ||
|
||
$colorizedMessage = ''; | ||
while ($line = fgets($fp)) { | ||
$char = $line[0]; | ||
$line = trim($line); | ||
|
||
switch ($char) { | ||
case '+': | ||
$line = "<info>$line</info>"; | ||
break; | ||
case '-': | ||
$line = "<comment>$line</comment>"; | ||
break; | ||
} | ||
|
||
$colorizedMessage .= $line . "\n"; | ||
} | ||
|
||
return trim($colorizedMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
namespace Codeception\Lib\Console; | ||
|
||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use SebastianBergmann\Diff\Differ; | ||
|
||
/** | ||
* DiffFactory | ||
**/ | ||
class DiffFactory | ||
{ | ||
/** | ||
* @param ComparisonFailure $failure | ||
* @return string|null | ||
*/ | ||
public function createDiff(ComparisonFailure $failure) | ||
{ | ||
$diff = $this->getDiff($failure->getExpectedAsString(), $failure->getActualAsString()); | ||
if (!$diff) { | ||
return null; | ||
} | ||
|
||
return $diff; | ||
} | ||
|
||
/** | ||
* @param string $expected | ||
* @param string $actual | ||
* @return string | ||
*/ | ||
private function getDiff($expected = '', $actual = '') | ||
{ | ||
if (!$actual && !$expected) { | ||
return ''; | ||
} | ||
|
||
$differ = new Differ(''); | ||
|
||
return $differ->diff($expected, $actual); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
namespace Codeception\Lib\Console; | ||
|
||
use SebastianBergmann\Comparator\ComparisonFailure; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* MessageFactory | ||
**/ | ||
class MessageFactory | ||
{ | ||
/** | ||
* @var DiffFactory | ||
*/ | ||
protected $diffFactory; | ||
/** | ||
* @var OutputInterface | ||
*/ | ||
private $output; | ||
|
||
/** | ||
* MessageFactory constructor. | ||
* @param Output $output | ||
*/ | ||
public function __construct(Output $output) | ||
{ | ||
$this->output = $output; | ||
$this->diffFactory = new DiffFactory(); | ||
$this->colorizer = new Colorizer(); | ||
} | ||
|
||
/** | ||
* @param string $text | ||
* @return Message | ||
*/ | ||
public function message($text = '') | ||
{ | ||
return new Message($text, $this->output); | ||
} | ||
|
||
/** | ||
* @param ComparisonFailure $failure | ||
* @return Message|null | ||
*/ | ||
public function prepareComparisonFailureMessage(ComparisonFailure $failure) | ||
{ | ||
$diff = $this->diffFactory->createDiff($failure); | ||
$diff = $this->colorizer->colorize($diff); | ||
|
||
return $this->message($diff) | ||
->prepend("<comment>- Expected</comment> | <info>+ Actual</info>\n"); | ||
} | ||
} |
Oops, something went wrong.