You are here

class NumberComparator in Commerce Core 8.2

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\commerce_price\Comparator\NumberComparator extends \SebastianBergmann\Comparator\Comparator

Expanded class hierarchy of NumberComparator

4 files declare their use of NumberComparator
CommerceBrowserTestBase.php in tests/src/Functional/CommerceBrowserTestBase.php
CommerceKernelTestBase.php in tests/src/Kernel/CommerceKernelTestBase.php
CommerceWebDriverTestBase.php in tests/src/FunctionalJavascript/CommerceWebDriverTestBase.php
ProductVariationResourceTest.php in modules/product/tests/src/Functional/Jsonapi/ProductVariationResourceTest.php

File

modules/price/src/Comparator/NumberComparator.php, line 16

Namespace

Drupal\commerce_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

Namesort descending Modifiers Type Description Overrides
NumberComparator::accepts public function Returns whether the comparator can compare two values.
NumberComparator::assertEquals public function Asserts that two values are equal.