Page MenuHomeIn-Portal Phabricator

INP-1900 Ensure numeric parameter values inside "kUtil::Pounds2Kg" method
ClosedPublic

Authored by erik on Feb 10 2025, 4:55 AM.

Details

Summary

added float conversion, avoided error/warning

Test Plan

Preparations

  1. ensure that PHP version 7.x is used
  2. ensure that Measures System US/UK is selected (it is default for clean install)

Test

  1. login to the adn.console
  2. go to the Website & ContentProducts section
  3. press New Product button the Products grid's toolbar, choose Tangible
  4. enter values for all required fields (Title, SKU)
  5. enter weight like this: pounds field - 1; ounces field - empty
  6. press Save button in the Adding Product... form's toolbar
  7. confirm, that product is created and it's weight is 1 pound and 0 ounces
  8. press New Product button the Products grid's toolbar, choose Tangible
  9. enter values for all required fields (Title, SKU)
  10. enter weight like this: pounds field - empty; ounces field - 1
  11. press Save button in the Adding Product... form's toolbar
  12. confirm, that product is created and it's weight is 0 pounds and 1 ounce
  13. go to the Logs & ReportsSystem Log section
  14. confirm, that no “A non-numeric value encountered” warning records in the System Log created while testing
  15. confirm, that no InvalidArgumentException error records in the System Log created while testing
  16. run tests testValidKg2Pounds, testExceptionKg2Pounds, testValidPounds2Kg, testExceptionPounds2Kg from kUtilTest class, confirm that all tests are passed
  17. run all tests from kUnitFormatterTest class, confirm that all tests are passed

Diff Detail

Repository
rINP In-Portal
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

erik created this revision.Feb 10 2025, 4:55 AM
erik requested review of this revision.Feb 10 2025, 4:55 AM
erik edited the test plan for this revision. (Show Details)Feb 10 2025, 4:58 AM
alex edited the test plan for this revision. (Show Details)Apr 29 2025, 8:00 AM
alex accepted this revision.Apr 29 2025, 8:03 AM
This revision is now accepted and ready to land.Apr 29 2025, 8:03 AM
alex requested changes to this revision.Mon, Sep 1, 3:46 AM

Let's implement this the other way around (sender does type casting, but recipient throws an exception on error on any PHP version):

  1. rollback current changes
  2. make the kUtil::Pounds2Kg and kUtil::Kg2Pounds methods throw the InvalidArgumentException, when either of a given arguments, that should be numeric aren't numeric
  3. add tests to the /core/tests/Unit/kernel/kUtilTest.php file, that will:
    • cover original method logic (that conversion actually works correctly)
    • cover new logic (that exception thrown when non-numeric arguments are given)
  4. change the kUnitFormatter to typecast any values, that it gives to the kUtil::Pounds2Kg and kUtil::Kg2Pounds methods
  5. adjust test plan
This revision now requires changes to proceed.Mon, Sep 1, 3:46 AM
erik updated this revision to Diff 1392.Mon, Sep 1, 1:33 PM
erik edited the test plan for this revision. (Show Details)

Changed ligic, added unit tests.

erik edited the test plan for this revision. (Show Details)Mon, Sep 1, 1:38 PM
alex requested changes to this revision.Tue, Sep 2, 5:33 AM

Also try to modify tested code to confirm, that related tests would fail.

core/kernel/globals.php
364–373 ↗(On Diff #1392)

Please also auto-reformat method DocBlock and add missing/remove unnecessary parts to solve any CS-related issues.

377 ↗(On Diff #1392)

Please replace the exception message to:

The $kg argument isn't a number.
380–387 ↗(On Diff #1392)

Please also auto-reformat method body (few changes really) to solve any CS-related issues.

403 ↗(On Diff #1392)

Please replace the exception message to:

Either the $pounds or $ounces argument isn't a number.
core/kernel/utility/formatters/unit_formatter.php
90 ↗(On Diff #1392)

add test (the kUnitFormatterTest test class) to cover at least this change (non-numeric values in $major/$minor are treated as 0 in pound2kg conversion code when provided via kUnitFormatter)

121 ↗(On Diff #1392)

add test (the kUnitFormatterTest test class) to cover at least this change (non-numeric $value is treated as 0 in kg2pounds conversion code when provided via kUnitFormatter)

core/tests/Unit/kernel/kUtilTest.php
59–83 ↗(On Diff #1392)

Please split this into different tests (use data provider as necessary):

  • checking valid conversion logic
  • checking, that exception is thrown on incorrect parameter value (for each supported parameter, that is protected by exception)
72 ↗(On Diff #1392)

Please:

  1. replace the 'InvalidArgumentException' with the InvalidArgumentException::class
  2. also check the exception message
79 ↗(On Diff #1392)
  • set key to the 'pounds_only=false'
  • replace the kUtil::POUND_TO_KG * 1.5 with the final number

  • dataset contents isn't displayed for a failed test and that's why we'd better use dataset array keys to show user friendly value
  • no need to replicate method internals in test
80 ↗(On Diff #1392)
  • set key to the 'pounds_only=true'
  • replace the kUtil::POUND_TO_KG * 1.5 with the final number

  • dataset contents isn't displayed for a failed test and that's why we'd better use dataset array keys to show user friendly value
  • no need to replicate method internals in test
85–109 ↗(On Diff #1392)

Please split this into different tests (use data provider as necessary):

  • checking valid conversion logic
  • checking, that exception is thrown on incorrect parameter value (for each supported parameter, that is protected by exception)
96 ↗(On Diff #1392)

Please:

  1. replace the 'InvalidArgumentException' with the InvalidArgumentException::class
  2. also check the exception message
103–104 ↗(On Diff #1392)

Please add missing data provider entry, that would cover case, when pounds=0, but ounces<>0, but is a valid number.

This revision now requires changes to proceed.Tue, Sep 2, 5:33 AM
erik updated this revision to Diff 1393.Tue, Sep 2, 7:33 AM
erik marked 13 inline comments as done.

Made all requested fixes. Created tests for kUbitFormatter class.

erik edited the test plan for this revision. (Show Details)Tue, Sep 2, 7:37 AM
alex requested changes to this revision.Tue, Sep 2, 8:02 AM
alex added inline comments.
core/tests/Unit/kernel/kUtilTest.php
65 ↗(On Diff #1393)

In all added tests use assertEquals when comparing numbers instead of assertSame.


Comparing 0 to null would pass with assertEquals.

97 ↗(On Diff #1393)

Please also add the 4th case, when both pounds and ounces are zero.

This revision now requires changes to proceed.Tue, Sep 2, 8:02 AM
erik added inline comments.Tue, Sep 2, 9:05 AM
core/tests/Unit/kernel/kUtilTest.php
65 ↗(On Diff #1393)

Yes, assertEquals is used there and in the all new tests, added in this revision.

erik updated this revision to Diff 1394.Tue, Sep 2, 9:09 AM

Added test case when pounds and ounces both are zero.

alex requested changes to this revision.Wed, Sep 3, 4:14 AM
alex added inline comments.
core/tests/Unit/kernel/kUtilTest.php
65 ↗(On Diff #1393)

Ah, I've misplaced the method names.

I've meant use assertSame (works as a === b) instead of assertEquals (works as a == b) to prevent 0 === null comparison from passing the test.

This revision now requires changes to proceed.Wed, Sep 3, 4:14 AM
erik updated this revision to Diff 1395.Wed, Sep 3, 5:28 AM

Replaced assertEquels with assertSame in all tests, added in this revision.

alex updated this revision to Diff 1399.Wed, Sep 3, 10:08 AM

Rename test file/class to make Phabricator understand PHPUnit test results/coverage.

alex accepted this revision.Wed, Sep 3, 10:17 AM
This revision is now accepted and ready to land.Wed, Sep 3, 10:17 AM
This revision was landed with ongoing or failed builds.Wed, Sep 3, 11:03 AM
This revision was automatically updated to reflect the committed changes.