Skip to content

Commit

Permalink
Diff shows only changed lines (#3242)
Browse files Browse the repository at this point in the history
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
k0pernikus authored and Naktibalda committed Jun 20, 2016
1 parent c05618b commit 8192e36
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 42 deletions.
4 changes: 3 additions & 1 deletion composer.json
Expand Up @@ -30,7 +30,9 @@
"symfony/browser-kit": ">=2.7 <4.0",
"symfony/css-selector": ">=2.7 <4.0",
"symfony/dom-crawler": ">=2.7 <4.0",
"behat/gherkin": "~4.4.0"
"behat/gherkin": "~4.4.0",
"sebastian/comparator": "~1.1",
"sebastian/diff": "^1.4"

},
"require-dev": {
Expand Down
38 changes: 38 additions & 0 deletions src/Codeception/Lib/Console/Colorizer.php
@@ -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);
}
}
41 changes: 41 additions & 0 deletions src/Codeception/Lib/Console/DiffFactory.php
@@ -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);
}
}
53 changes: 53 additions & 0 deletions src/Codeception/Lib/Console/MessageFactory.php
@@ -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");
}
}

0 comments on commit 8192e36

Please sign in to comment.