1<?php
2/*
3 * This file is part of the Comparator package.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace SebastianBergmann\Comparator;
12
13use SebastianBergmann\Diff\Differ;
14
15/**
16 * Thrown when an assertion for string equality failed.
17 */
18class ComparisonFailure extends \RuntimeException
19{
20    /**
21     * Expected value of the retrieval which does not match $actual.
22     * @var mixed
23     */
24    protected $expected;
25
26    /**
27     * Actually retrieved value which does not match $expected.
28     * @var mixed
29     */
30    protected $actual;
31
32    /**
33     * The string representation of the expected value
34     * @var string
35     */
36    protected $expectedAsString;
37
38    /**
39     * The string representation of the actual value
40     * @var string
41     */
42    protected $actualAsString;
43
44    /**
45     * @var bool
46     */
47    protected $identical;
48
49    /**
50     * Optional message which is placed in front of the first line
51     * returned by toString().
52     * @var string
53     */
54    protected $message;
55
56    /**
57     * Initialises with the expected value and the actual value.
58     *
59     * @param mixed  $expected         Expected value retrieved.
60     * @param mixed  $actual           Actual value retrieved.
61     * @param string $expectedAsString
62     * @param string $actualAsString
63     * @param bool   $identical
64     * @param string $message          A string which is prefixed on all returned lines
65     *                                 in the difference output.
66     */
67    public function __construct($expected, $actual, $expectedAsString, $actualAsString, $identical = false, $message = '')
68    {
69        $this->expected         = $expected;
70        $this->actual           = $actual;
71        $this->expectedAsString = $expectedAsString;
72        $this->actualAsString   = $actualAsString;
73        $this->message          = $message;
74    }
75
76    /**
77     * @return mixed
78     */
79    public function getActual()
80    {
81        return $this->actual;
82    }
83
84    /**
85     * @return mixed
86     */
87    public function getExpected()
88    {
89        return $this->expected;
90    }
91
92    /**
93     * @return string
94     */
95    public function getActualAsString()
96    {
97        return $this->actualAsString;
98    }
99
100    /**
101     * @return string
102     */
103    public function getExpectedAsString()
104    {
105        return $this->expectedAsString;
106    }
107
108    /**
109     * @return string
110     */
111    public function getDiff()
112    {
113        if (!$this->actualAsString && !$this->expectedAsString) {
114            return '';
115        }
116
117        $differ = new Differ("\n--- Expected\n+++ Actual\n");
118
119        return $differ->diff($this->expectedAsString, $this->actualAsString);
120    }
121
122    /**
123     * @return string
124     */
125    public function toString()
126    {
127        return $this->message . $this->getDiff();
128    }
129}
130