ComparisonFailure.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. namespace SebastianBergmann\Comparator;
  11. use SebastianBergmann\Diff\Differ;
  12. /**
  13. * Thrown when an assertion for string equality failed.
  14. */
  15. class ComparisonFailure extends \RuntimeException
  16. {
  17. /**
  18. * Expected value of the retrieval which does not match $actual.
  19. * @var mixed
  20. */
  21. protected $expected;
  22. /**
  23. * Actually retrieved value which does not match $expected.
  24. * @var mixed
  25. */
  26. protected $actual;
  27. /**
  28. * The string representation of the expected value
  29. * @var string
  30. */
  31. protected $expectedAsString;
  32. /**
  33. * The string representation of the actual value
  34. * @var string
  35. */
  36. protected $actualAsString;
  37. /**
  38. * @var bool
  39. */
  40. protected $identical;
  41. /**
  42. * Optional message which is placed in front of the first line
  43. * returned by toString().
  44. * @var string
  45. */
  46. protected $message;
  47. /**
  48. * Initialises with the expected value and the actual value.
  49. *
  50. * @param mixed $expected Expected value retrieved.
  51. * @param mixed $actual Actual value retrieved.
  52. * @param string $expectedAsString
  53. * @param string $actualAsString
  54. * @param bool $identical
  55. * @param string $message A string which is prefixed on all returned lines
  56. * in the difference output.
  57. */
  58. public function __construct($expected, $actual, $expectedAsString, $actualAsString, $identical = false, $message = '')
  59. {
  60. $this->expected = $expected;
  61. $this->actual = $actual;
  62. $this->expectedAsString = $expectedAsString;
  63. $this->actualAsString = $actualAsString;
  64. $this->message = $message;
  65. }
  66. /**
  67. * @return mixed
  68. */
  69. public function getActual()
  70. {
  71. return $this->actual;
  72. }
  73. /**
  74. * @return mixed
  75. */
  76. public function getExpected()
  77. {
  78. return $this->expected;
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getActualAsString()
  84. {
  85. return $this->actualAsString;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public function getExpectedAsString()
  91. {
  92. return $this->expectedAsString;
  93. }
  94. /**
  95. * @return string
  96. */
  97. public function getDiff()
  98. {
  99. if (!$this->actualAsString && !$this->expectedAsString) {
  100. return '';
  101. }
  102. $differ = new Differ("\n--- Expected\n+++ Actual\n");
  103. return $differ->diff($this->expectedAsString, $this->actualAsString);
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function toString()
  109. {
  110. return $this->message . $this->getDiff();
  111. }
  112. }