ScalarComparator.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  12. * Compares scalar or NULL values for equality.
  13. */
  14. class ScalarComparator extends Comparator
  15. {
  16. /**
  17. * Returns whether the comparator can compare two values.
  18. *
  19. * @param mixed $expected The first value to compare
  20. * @param mixed $actual The second value to compare
  21. * @return bool
  22. * @since Method available since Release 3.6.0
  23. */
  24. public function accepts($expected, $actual)
  25. {
  26. return ((is_scalar($expected) xor null === $expected) &&
  27. (is_scalar($actual) xor null === $actual))
  28. // allow comparison between strings and objects featuring __toString()
  29. || (is_string($expected) && is_object($actual) && method_exists($actual, '__toString'))
  30. || (is_object($expected) && method_exists($expected, '__toString') && is_string($actual));
  31. }
  32. /**
  33. * Asserts that two values are equal.
  34. *
  35. * @param mixed $expected The first value to compare
  36. * @param mixed $actual The second value to compare
  37. * @param float $delta The allowed numerical distance between two values to
  38. * consider them equal
  39. * @param bool $canonicalize If set to TRUE, arrays are sorted before
  40. * comparison
  41. * @param bool $ignoreCase If set to TRUE, upper- and lowercasing is
  42. * ignored when comparing string values
  43. * @throws ComparisonFailure Thrown when the comparison
  44. * fails. Contains information about the
  45. * specific errors that lead to the failure.
  46. */
  47. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
  48. {
  49. $expectedToCompare = $expected;
  50. $actualToCompare = $actual;
  51. // always compare as strings to avoid strange behaviour
  52. // otherwise 0 == 'Foobar'
  53. if (is_string($expected) || is_string($actual)) {
  54. $expectedToCompare = (string) $expectedToCompare;
  55. $actualToCompare = (string) $actualToCompare;
  56. if ($ignoreCase) {
  57. $expectedToCompare = strtolower($expectedToCompare);
  58. $actualToCompare = strtolower($actualToCompare);
  59. }
  60. }
  61. if ($expectedToCompare != $actualToCompare) {
  62. if (is_string($expected) && is_string($actual)) {
  63. throw new ComparisonFailure(
  64. $expected,
  65. $actual,
  66. $this->exporter->export($expected),
  67. $this->exporter->export($actual),
  68. false,
  69. 'Failed asserting that two strings are equal.'
  70. );
  71. }
  72. throw new ComparisonFailure(
  73. $expected,
  74. $actual,
  75. // no diff is required
  76. '',
  77. '',
  78. false,
  79. sprintf(
  80. 'Failed asserting that %s matches expected %s.',
  81. $this->exporter->export($actual),
  82. $this->exporter->export($expected)
  83. )
  84. );
  85. }
  86. }
  87. }