ScalarComparator.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 First value to compare
  36. * @param mixed $actual Second value to compare
  37. * @param float $delta Allowed numerical distance between two values to consider them equal
  38. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  39. * @param bool $ignoreCase Case is ignored when set to true
  40. *
  41. * @throws ComparisonFailure
  42. */
  43. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
  44. {
  45. $expectedToCompare = $expected;
  46. $actualToCompare = $actual;
  47. // always compare as strings to avoid strange behaviour
  48. // otherwise 0 == 'Foobar'
  49. if (is_string($expected) || is_string($actual)) {
  50. $expectedToCompare = (string) $expectedToCompare;
  51. $actualToCompare = (string) $actualToCompare;
  52. if ($ignoreCase) {
  53. $expectedToCompare = strtolower($expectedToCompare);
  54. $actualToCompare = strtolower($actualToCompare);
  55. }
  56. }
  57. if ($expectedToCompare != $actualToCompare) {
  58. if (is_string($expected) && is_string($actual)) {
  59. throw new ComparisonFailure(
  60. $expected,
  61. $actual,
  62. $this->exporter->export($expected),
  63. $this->exporter->export($actual),
  64. false,
  65. 'Failed asserting that two strings are equal.'
  66. );
  67. }
  68. throw new ComparisonFailure(
  69. $expected,
  70. $actual,
  71. // no diff is required
  72. '',
  73. '',
  74. false,
  75. sprintf(
  76. 'Failed asserting that %s matches expected %s.',
  77. $this->exporter->export($actual),
  78. $this->exporter->export($expected)
  79. )
  80. );
  81. }
  82. }
  83. }