You are here

public function RecipeUnitTest::testIngredientQuantityConversion in Recipe 7.2

Same name and namespace in other branches
  1. 7 src/Tests/RecipeUnitTest.php \Drupal\recipe\Tests\RecipeUnitTest::testIngredientQuantityConversion()

Test ingredient quantity conversion functions.

File

src/Tests/RecipeUnitTest.php, line 34

Class

RecipeUnitTest
Tests isolated Recipe module functions.

Namespace

Drupal\recipe\Tests

Code

public function testIngredientQuantityConversion() {
  $quantities = array(
    // Test a couple of whole numbers.
    '1',
    '10',
    // Test a couple of mixed numbers.
    '1 1/2',
    '10 1/2',
    // Test the fractions which convert to repeating decimals that are
    // converted by recipe_ingredient_quantity_from_decimal().
    '1/3',
    '2/3',
    '1/6',
    '5/6',
    '1/9',
    '2/9',
    '4/9',
    '5/9',
    '7/9',
    '8/9',
    '1/12',
    '5/12',
    '7/12',
    '11/12',
  );
  foreach ($quantities as $quantity) {

    // Convert the fraction quantity to a decimal.
    $decimal = recipe_ingredient_quantity_from_fraction($quantity);

    // Convert the decimal quantity back to a fraction string.
    $fraction = recipe_ingredient_quantity_from_decimal($decimal);

    // Replace the '⁄' in the fraction string with '/'.
    $fraction = str_replace('⁄', '/', $fraction);

    // Verify the fraction result is the same as the original quantity.
    $this
      ->assertEqual($quantity, $fraction);
  }
}