You are here

class NumberComparator in Price 2.0.x

Same name and namespace in other branches
  1. 2.x src/Comparator/NumberComparator.php \Drupal\price\Comparator\NumberComparator

Provides a PHPUnit comparator for numbers cast to strings.

In PHPUnit 6, $this->assertEquals('2.0', '2.000') would pass because numerically the two strings were equal. This behavior was removed in PHPUnit 7, and the assert fails. This comparator restores the ability to compare two strings numerically.

Hierarchy

  • class \Drupal\price\Comparator\NumberComparator extends \SebastianBergmann\Comparator\Comparator

Expanded class hierarchy of NumberComparator

File

src/Comparator/NumberComparator.php, line 16

Namespace

Drupal\price\Comparator
View source
class NumberComparator extends Comparator {

  /**
   * {@inheritdoc}
   */
  public function accepts($expected, $actual) {
    return is_string($expected) && is_numeric($expected) && is_string($actual) && is_numeric($actual);
  }

  /**
   * {@inheritdoc}
   */
  public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = FALSE, $ignoreCase = FALSE) {
    if ($expected != $actual) {
      throw new ComparisonFailure($expected, $actual, '', '', FALSE, sprintf('Failed asserting that "%s" matches expected "%s".', $actual, $expected));
    }
  }

}

Members