ComparisonFailure.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. public function getActual()
  73. {
  74. return $this->actual;
  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(new UnifiedDiffOutputBuilder("\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. }