The commit in #2749287: Implement a Calculator class and use it in the Price value object broke our tests. At first it just looked like it was due to tests passing floats or integers, instead of strings. But it is more than that.

There were 6 errors:
1) Drupal\Tests\commerce_order\Functional\OrderAdminTest::testCreateOrder
Behat\Mink\Exception\ElementNotFoundException: Button with id|name|label|value "Edit" not found.
/home/travis/build/drupalcommerce/drupal-8/drupal/core/tests/Drupal/Tests/WebAssert.php:76
/home/travis/build/drupalcommerce/drupal-8/drupal/core/tests/Drupal/Tests/BrowserTestBase.php:791
/home/travis/build/drupalcommerce/commerce/modules/order/tests/src/Functional/OrderAdminTest.php:66
2) Drupal\Tests\commerce_order\Kernel\AdjustmentItemTest::testAdjustmentItem
InvalidArgumentException: The provided value "-1" must be a string, not a float.
/home/travis/build/drupalcommerce/commerce/modules/price/src/Calculator.php:193
/home/travis/build/drupalcommerce/commerce/modules/price/src/Price.php:35
/home/travis/build/drupalcommerce/commerce/modules/order/tests/src/Kernel/AdjustmentItemTest.php:72
3) Drupal\Tests\commerce_order\Kernel\Entity\LineItemTest::testLineItem
InvalidArgumentException: The provided value "9.99" must be a string, not a float.
/home/travis/build/drupalcommerce/commerce/modules/price/src/Calculator.php:193
/home/travis/build/drupalcommerce/commerce/modules/price/src/Price.php:35
/home/travis/build/drupalcommerce/commerce/modules/order/tests/src/Kernel/Entity/LineItemTest.php:79
4) Drupal\Tests\commerce_order\Kernel\Entity\OrderTest::testOrder
InvalidArgumentException: The provided value "-1" must be a string, not a float.
/home/travis/build/drupalcommerce/commerce/modules/price/src/Calculator.php:193
/home/travis/build/drupalcommerce/commerce/modules/price/src/Price.php:35
/home/travis/build/drupalcommerce/commerce/modules/order/tests/src/Kernel/Entity/OrderTest.php:201
5) Drupal\Tests\commerce_order\Unit\AdjustmentTest::testValidAdjustmentConstruct
InvalidArgumentException: The provided value "-1" must be a string, not a float.
/home/travis/build/drupalcommerce/commerce/modules/price/src/Calculator.php:193
/home/travis/build/drupalcommerce/commerce/modules/price/src/Price.php:35
/home/travis/build/drupalcommerce/commerce/modules/order/tests/src/Unit/AdjustmentTest.php:64
/home/travis/build/drupalcommerce/drupal-8/drupal/vendor/phpunit/phpunit/phpunit:47
6) Drupal\Tests\commerce_order\Unit\AdjustmentTest::testAdjustmentMethods
InvalidArgumentException: The provided value "-1" must be a string, not a float.
/home/travis/build/drupalcommerce/commerce/modules/price/src/Calculator.php:193
/home/travis/build/drupalcommerce/commerce/modules/price/src/Price.php:35
/home/travis/build/drupalcommerce/commerce/modules/order/tests/src/Unit/AdjustmentTest.php:84
/home/travis/build/drupalcommerce/drupal-8/drupal/vendor/phpunit/phpunit/phpunit:47
FAILURES!
Tests: 146, Assertions: 1069, Errors: 6.
CommentFileSizeAuthor
#3 follow_up_to_calculator-2789651-3.patch7.08 KBmglaman

Comments

mglaman created an issue. See original summary.

mglaman’s picture

Somewhere along the time the amount property in PriceItem becomes a non-string. I'm not sure where, even when applying the following diff we still get exceptions about the amount not being a string.

+use Drupal\commerce_price\Calculator;
 use Drupal\commerce_price\Price;
 use Drupal\Core\Field\FieldItemBase;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
@@ -92,6 +93,11 @@ class PriceItem extends FieldItemBase {
         'currency_code' => $price->getCurrencyCode(),
       ];
     }
+
+    if (isset($values['amount'])) {
+      Calculator::assertNumberFormat($values['amount']);
+    }
+
     parent::setValue($values, $notify);
   }
 

The only viable fix seems to be ensuring that the PriceItem field casts the amount property to string when constructing a new price object. This fixes tests (and is also a DX win)

    *   The Price value object.
    */
   public function toPrice() {
-    return new Price($this->amount, $this->currency_code);
+    return new Price((string) $this->amount, $this->currency_code);
   }
 
 }
mglaman’s picture

Status: Active » Needs review
StatusFileSize
new7.08 KB
mglaman’s picture

Status: Needs review » Needs work

One failure, on the order create form: Drupal\Tests\commerce_order\Functional\OrderAdminTest::testCreateOrder.

mglaman’s picture

Also, can't save or add line items in the UI

 InvalidArgumentException: The provided value "" is not a numeric value. in Drupal\commerce_price\Calculator::assertNumberFormat() 
mglaman’s picture

There is also an issue on \Drupal\commerce_order\Entity\OrderInterface::getTotalPrice when the field is empty, it throws an error for calling toPrice on null.

mglaman’s picture

Priority: Normal » Major
Status: Needs work » Needs review

Order UI is borked because of this. Made an adjustment and pushed

The error seems to be that the empty price field is non-numeric, so we need to convert it to a numeric value (empty string is zero.)

diff --git a/modules/price/src/Element/Price.php b/modules/price/src/Element/Price.php
index 7fb4726..e9ba015 100644
--- a/modules/price/src/Element/Price.php
+++ b/modules/price/src/Element/Price.php
@@ -62,6 +62,10 @@ class Price extends Textfield {
    */
   public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
     if ($input !== FALSE && $input !== NULL) {
+      // Convert empty string value to numeric empty value.
+      if ($input['amount'] === '') {
+        $input['amount'] = '0';
+      }
       return new PriceValue($input['amount'], $input['currency_code']);
     }
     return NULL;

  • mglaman authored 30b3091 on 8.x-2.x
    Issue #2789651 by mglaman: Follow up to Calculator class, #2749287
    
    
mglaman’s picture

Status: Needs review » Fixed

Tests are now passing!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.