Skip to content

ext/bcmath: Add missing error test cases for bcdivmod() - #24639

Open
spidervirus wants to merge 2 commits into
php:PHP-8.4from
spidervirus:test-bcdivmod-errors
Open

spidervirus wants to merge 2 commits into
php:PHP-8.4from
spidervirus:test-bcdivmod-errors

Conversation

@spidervirus

Copy link
Copy Markdown

Description

This PR adds missing error test coverage for bcdivmod():

  • Adds ext/bcmath/tests/bcdivmod_error.phpt to verify that passing non-well-formed numeric strings throws a ValueError.
  • Adds a bcdivmod() negative scale test case to ext/bcmath/tests/negative_scale.phpt.

Add test for ValueError on malformed string inputs to bcdivmod(), and add bcdivmod() negative scale check to negative_scale.phpt.

@jorgsowa jorgsowa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check small nits for the PR. You can apply convention of $e::class, ': ', for whole negative_scale.phpt test case.

echo $e->getMessage() . \PHP_EOL;
}
try {
bcdivmod('1','2',-1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bcdivmod('1','2',-1);
bcdivmod('1', '2', -1);

Comment thread ext/bcmath/tests/negative_scale.phpt Outdated
try {
bcdivmod('1','2',-1);
} catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo $e->getMessage() . \PHP_EOL;
echo $e::class, ': ', $e->getMessage() . \PHP_EOL;

Comment thread ext/bcmath/tests/negative_scale.phpt Outdated
bcsqrt(): Argument #2 ($scale) must be between 0 and 2147483647
bccomp(): Argument #3 ($scale) must be between 0 and 2147483647
bcscale(): Argument #1 ($scale) must be between 0 and 2147483647

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Comment thread ext/bcmath/tests/bcdivmod_error.phpt Outdated
try {
bcdivmod('a', '1');
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo $e->getMessage() . PHP_EOL;
echo $e::class, ': ', $e->getMessage() . PHP_EOL;

Comment thread ext/bcmath/tests/bcdivmod_error.phpt Outdated
try {
bcdivmod('1', 'a');
} catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo $e->getMessage() . PHP_EOL;
echo $e::class, ': ', $e->getMessage() . PHP_EOL;

Comment thread ext/bcmath/tests/bcdivmod_error.phpt Outdated

try {
bcdivmod('a', '1');
} catch (\ValueError $e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And elsewhere.

Suggested change
} catch (\ValueError $e) {
} catch (\Throwable $e) {

Update tests to catch \Throwable and prefix output with $e::class per review feedback.
@spidervirus

Copy link
Copy Markdown
Author

Thanks @jorgsowa and @Girgias! Applied both suggestions: updated to catch \Throwable and standardized on $e::class, ': ' across bcdivmod_error.phpt and the entire negative_scale.phpt file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants