Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace SebastianBergmann\Comparator; Chris@0: Chris@0: use SebastianBergmann\Diff\Differ; Chris@14: use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder; Chris@0: Chris@0: /** Chris@0: * Thrown when an assertion for string equality failed. Chris@0: */ Chris@0: class ComparisonFailure extends \RuntimeException Chris@0: { Chris@0: /** Chris@0: * Expected value of the retrieval which does not match $actual. Chris@14: * Chris@0: * @var mixed Chris@0: */ Chris@0: protected $expected; Chris@0: Chris@0: /** Chris@0: * Actually retrieved value which does not match $expected. Chris@14: * Chris@0: * @var mixed Chris@0: */ Chris@0: protected $actual; Chris@0: Chris@0: /** Chris@0: * The string representation of the expected value Chris@14: * Chris@0: * @var string Chris@0: */ Chris@0: protected $expectedAsString; Chris@0: Chris@0: /** Chris@0: * The string representation of the actual value Chris@14: * Chris@0: * @var string Chris@0: */ Chris@0: protected $actualAsString; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: protected $identical; Chris@0: Chris@0: /** Chris@0: * Optional message which is placed in front of the first line Chris@0: * returned by toString(). Chris@14: * Chris@0: * @var string Chris@0: */ Chris@0: protected $message; Chris@0: Chris@0: /** Chris@0: * Initialises with the expected value and the actual value. Chris@0: * Chris@0: * @param mixed $expected Expected value retrieved. Chris@0: * @param mixed $actual Actual value retrieved. Chris@0: * @param string $expectedAsString Chris@0: * @param string $actualAsString Chris@0: * @param bool $identical Chris@0: * @param string $message A string which is prefixed on all returned lines Chris@0: * in the difference output. Chris@0: */ Chris@0: public function __construct($expected, $actual, $expectedAsString, $actualAsString, $identical = false, $message = '') Chris@0: { Chris@0: $this->expected = $expected; Chris@0: $this->actual = $actual; Chris@0: $this->expectedAsString = $expectedAsString; Chris@0: $this->actualAsString = $actualAsString; Chris@0: $this->message = $message; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return mixed Chris@0: */ Chris@0: public function getActual() Chris@0: { Chris@0: return $this->actual; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return mixed Chris@0: */ Chris@0: public function getExpected() Chris@0: { Chris@0: return $this->expected; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getActualAsString() Chris@0: { Chris@0: return $this->actualAsString; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getExpectedAsString() Chris@0: { Chris@0: return $this->expectedAsString; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function getDiff() Chris@0: { Chris@0: if (!$this->actualAsString && !$this->expectedAsString) { Chris@0: return ''; Chris@0: } Chris@0: Chris@14: $differ = new Differ(new UnifiedDiffOutputBuilder("\n--- Expected\n+++ Actual\n")); Chris@0: Chris@0: return $differ->diff($this->expectedAsString, $this->actualAsString); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return string Chris@0: */ Chris@0: public function toString() Chris@0: { Chris@0: return $this->message . $this->getDiff(); Chris@0: } Chris@0: }