ComparisonFailure.php 3.0 KB

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