added float conversion, avoided error/warning
Details
Preparations
- ensure that PHP version 7.x is used
- ensure that Measures System US/UK is selected (it is default for clean install)
Test
- login to the adn.console
- go to the Website & Content → Products section
- press New Product button the Products grid's toolbar, choose Tangible
- enter values for all required fields (Title, SKU)
- enter weight like this: pounds field - 1; ounces field - empty
- press Save button in the Adding Product... form's toolbar
- confirm, that product is created and it's weight is 1 pound and 0 ounces
- press New Product button the Products grid's toolbar, choose Tangible
- enter values for all required fields (Title, SKU)
- enter weight like this: pounds field - empty; ounces field - 1
- press Save button in the Adding Product... form's toolbar
- confirm, that product is created and it's weight is 0 pounds and 1 ounce
- go to the Logs & Reports → System Log section
- confirm, that no “A non-numeric value encountered” warning records in the System Log created while testing
- confirm, that no InvalidArgumentException error records in the System Log created while testing
- run tests testValidKg2Pounds, testExceptionKg2Pounds, testValidPounds2Kg, testExceptionPounds2Kg from kUtilTest class, confirm that all tests are passed
- 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
Let's implement this the other way around (sender does type casting, but recipient throws an exception on error on any PHP version):
- rollback current changes
- make the kUtil::Pounds2Kg and kUtil::Kg2Pounds methods throw the InvalidArgumentException, when either of a given arguments, that should be numeric aren't numeric
- 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)
- change the kUnitFormatter to typecast any values, that it gives to the kUtil::Pounds2Kg and kUtil::Kg2Pounds methods
- adjust test plan
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):
|
72 ↗ | (On Diff #1392) | Please:
|
79 ↗ | (On Diff #1392) |
|
80 ↗ | (On Diff #1392) |
|
85–109 ↗ | (On Diff #1392) | Please split this into different tests (use data provider as necessary):
|
96 ↗ | (On Diff #1392) | Please:
|
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. |
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. |
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. |