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