ScalarComparator.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  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. *
  22. * @return bool
  23. *
  24. * @since Method available since Release 3.6.0
  25. */
  26. public function accepts($expected, $actual)
  27. {
  28. return ((\is_scalar($expected) xor null === $expected) &&
  29. (\is_scalar($actual) xor null === $actual))
  30. // allow comparison between strings and objects featuring __toString()
  31. || (\is_string($expected) && \is_object($actual) && \method_exists($actual, '__toString'))
  32. || (\is_object($expected) && \method_exists($expected, '__toString') && \is_string($actual));
  33. }
  34. /**
  35. * Asserts that two values are equal.
  36. *
  37. * @param mixed $expected First value to compare
  38. * @param mixed $actual Second value to compare
  39. * @param float $delta Allowed numerical distance between two values to consider them equal
  40. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  41. * @param bool $ignoreCase Case is ignored when set to true
  42. *
  43. * @throws ComparisonFailure
  44. */
  45. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
  46. {
  47. $expectedToCompare = $expected;
  48. $actualToCompare = $actual;
  49. // always compare as strings to avoid strange behaviour
  50. // otherwise 0 == 'Foobar'
  51. if (\is_string($expected) || \is_string($actual)) {
  52. $expectedToCompare = (string) $expectedToCompare;
  53. $actualToCompare = (string) $actualToCompare;
  54. if ($ignoreCase) {
  55. $expectedToCompare = \strtolower($expectedToCompare);
  56. $actualToCompare = \strtolower($actualToCompare);
  57. }
  58. }
  59. if ($expectedToCompare !== $actualToCompare && \is_string($expected) && \is_string($actual)) {
  60. throw new ComparisonFailure(
  61. $expected,
  62. $actual,
  63. $this->exporter->export($expected),
  64. $this->exporter->export($actual),
  65. false,
  66. 'Failed asserting that two strings are equal.'
  67. );
  68. }
  69. if ($expectedToCompare != $actualToCompare) {
  70. throw new ComparisonFailure(
  71. $expected,
  72. $actual,
  73. // no diff is required
  74. '',
  75. '',
  76. false,
  77. \sprintf(
  78. 'Failed asserting that %s matches expected %s.',
  79. $this->exporter->export($actual),
  80. $this->exporter->export($expected)
  81. )
  82. );
  83. }
  84. }
  85. }