PhpSpec\Formatter\Presenter\Differ\StringEngine::compare PHP Method

compare() public method

public compare ( $expected, $actual )
    public function compare($expected, $actual)
    {
        $expected = explode("\n", (string) $expected);
        $actual = explode("\n", (string) $actual);
        $diff = new \Diff($expected, $actual, array());
        $renderer = new \Diff_Renderer_Text_Unified();
        $text = $diff->render($renderer);
        $lines = array();
        foreach (explode("\n", $text) as $line) {
            if (0 === strpos($line, '-')) {
                $lines[] = sprintf('<diff-del>%s</diff-del>', $line);
            } elseif (0 === strpos($line, '+')) {
                $lines[] = sprintf('<diff-add>%s</diff-add>', $line);
            } else {
                $lines[] = $line;
            }
        }
        return sprintf("<code>%s%s</code>", "\n", implode("\n", $lines));
    }

Usage Example

 function it_converts_objects_to_string_and_diffs_the_result(Exporter $exporter, StringEngine $stringDiffer)
 {
     $exporter->export(Argument::type('DateTime'))->willReturn('DateTime');
     $exporter->export(Argument::type('ArrayObject'))->willReturn('ArrayObject');
     $stringDiffer->compare('DateTime', 'ArrayObject')->willReturn('-DateTime+ArrayObject');
     $diff = $this->compare(new \DateTime(), new \ArrayObject());
     $diff->shouldBe('-DateTime+ArrayObject');
 }
All Usage Examples Of PhpSpec\Formatter\Presenter\Differ\StringEngine::compare