ScalarComparator.php 3.4 KB

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