diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml index 3c89e0f..1dbb175 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.yml +++ b/.github/ISSUE_TEMPLATE/bug_report.yml @@ -1,5 +1,6 @@ name: Bug report description: Report something that is not working as expected. +title: "[Bug]: " labels: - "type: bug" - "status: needs triage" diff --git a/.github/dependabot.yml b/.github/dependabot.yml index ddafb34..473f3fb 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,6 +5,8 @@ updates: - package-ecosystem: composer directory: "/" target-branch: develop + labels: + - "area: dependencies" schedule: interval: weekly groups: @@ -14,6 +16,8 @@ updates: - package-ecosystem: github-actions directory: "/" target-branch: develop + labels: + - "area: dependencies" schedule: interval: weekly groups: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 41b5373..0687861 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,118 +1,214 @@ name: Build - -on: +'on': push: branches: - - '8.*.x' - - develop + - develop + - 8.*.x paths-ignore: - - 'README.md' - - 'docs/**' - + - README.md + - docs/** pull_request: branches: - - '8.*.x' - - develop - - workflow_dispatch: - -permissions: - contents: read - + - develop + - 8.*.x + workflow_dispatch: null concurrency: - group: ${{ github.workflow }}-${{ github.ref }} + group: build-${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true - +permissions: + contents: read jobs: - php: - name: Resolve PHP version + matrix: runs-on: ubuntu-latest outputs: - version: ${{ steps.php.outputs.version }} - + php: ${{ steps.php.outputs.versions }} + lowest: ${{ steps.php.outputs.lowest }} steps: - - name: Checkout - uses: actions/checkout@v7 - - # Every branch declares exactly one PHP minor (">=8.N <8.N+1"), so the - # version comes from composer.json on any branch. - - name: Detect target PHP version - id: php - shell: bash - run: | - constraint="$(jq -r '.require.php' composer.json)" - - if [[ ! "$constraint" =~ ^\>=(8\.[0-9]+) ]]; then - echo "Cannot read the target PHP minor from require.php: $constraint" >&2 - exit 1 - fi - - echo "version=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT" - - check: - name: Check - needs: php + - name: Checkout + uses: actions/checkout@v7 + - name: Read supported PHP minors + id: php + shell: bash + run: | + constraint="$(jq -r '.require.php' composer.json)" + if [[ ! "$constraint" =~ ^\>=8\.([0-9]+)\ \<8\.([0-9]+)$ ]] || (( BASH_REMATCH[2] != BASH_REMATCH[1] + 1 )); then + echo "require.php must declare exactly one PHP minor as >=8.N <8.N+1, got: $constraint" >&2 + exit 1 + fi + version="8.${BASH_REMATCH[1]}" + echo "versions=$(jq -cn --arg php "$version" '[$php]')" >> "$GITHUB_OUTPUT" + echo "lowest=$version" >> "$GITHUB_OUTPUT" + validate: + needs: + - matrix runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v7 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ needs.php.outputs.version }} - extensions: mbstring, intl - coverage: none - tools: composer:v2 - - - name: Install dependencies - uses: ramsey/composer-install@v4 - - - name: Check - run: composer check - + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Validate + run: composer validate --strict + style: + needs: + - matrix + - validate + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Style + run: composer format:check + analyse: + needs: + - matrix + - style + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Analyse + run: composer analyse tests: - name: Tests - ${{ matrix.os }} - ${{ matrix.dependencies }}${{ matrix.intl && ' - intl' || '' }} - needs: php - runs-on: ${{ matrix.os }} - + name: Tests - PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependencies }}${{ !matrix.intl && ' - without intl' || '' }} + needs: + - matrix + - analyse strategy: - max-parallel: 2 - - fail-fast: false - + fail-fast: true matrix: - os: - - ubuntu-latest - - windows-latest + php: ${{ fromJSON(needs.matrix.outputs.php) }} dependencies: - - lowest - - highest + - lowest + - highest + os: + - ubuntu-latest + - windows-latest intl: - - true + - true include: - # ext-intl is optional; the built-in formatters must work without it. - - os: ubuntu-latest - dependencies: highest - intl: false - + # ext-intl is optional: the built-in formatters must work without it. + - php: ${{ needs.matrix.outputs.lowest }} + dependencies: highest + os: ubuntu-latest + intl: false + runs-on: ${{ matrix.os }} + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ matrix.php }} + extensions: mbstring, ${{ matrix.intl && 'intl' || ':intl' }} + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: ${{ matrix.dependencies }} + - name: Run tests + run: composer test + security: + needs: + - matrix + - tests + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Security audit + run: composer security + archive: + needs: + - matrix + - security + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Setup PHP + uses: shivammathur/setup-php@eb7c497e18156a6bbabfef1d3a82760b9eda3962 # 2.40.0 + with: + php-version: ${{ needs.matrix.outputs.lowest }} + extensions: mbstring, intl + coverage: none + tools: composer:v2 + - name: Install dependencies + uses: ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda # 4.0.0 + with: + dependency-versions: highest + - name: Verify Composer archive + shell: bash + run: | + mkdir -p /tmp/pharaonic-ci-archive + composer archive --format=zip --dir=/tmp/pharaonic-ci-archive --file=package + unzip -t /tmp/pharaonic-ci-archive/package.zip + quality: + name: Quality gate + if: ${{ always() }} + needs: + - matrix + - validate + - style + - analyse + - tests + - security + - archive + runs-on: ubuntu-latest steps: - - name: Checkout - uses: actions/checkout@v7 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ needs.php.outputs.version }} - extensions: mbstring, ${{ matrix.intl && 'intl' || ':intl' }} - coverage: none - tools: composer:v2 - - - name: Install dependencies - uses: ramsey/composer-install@v4 - with: - dependency-versions: ${{ matrix.dependencies }} - - - name: Run tests - run: composer test + - name: Require every stage to succeed + env: + NEEDS_JSON: ${{ toJSON(needs) }} + run: | + python3 - <<'PY_CHECK' + import json, os, sys + results = json.loads(os.environ['NEEDS_JSON']) + failed = {name: job['result'] for name, job in results.items() if job['result'] != 'success'} + if failed: + print('Required stages did not succeed:', failed) + sys.exit(1) + print('Every required stage succeeded.') + PY_CHECK diff --git a/.github/workflows/labels.yml b/.github/workflows/labels.yml index 8bdc79a..d96990d 100644 --- a/.github/workflows/labels.yml +++ b/.github/workflows/labels.yml @@ -22,7 +22,7 @@ jobs: # Creates and updates the labels in .github/labels.yml. Labels that are # not listed are left alone. - name: Sync labels - uses: crazy-max/ghaction-github-labeler@v5 + uses: crazy-max/ghaction-github-labeler@548a7c3603594ec17c819e1239f281a3b801ab4d # v6.0.0 with: yaml-file: .github/labels.yml skip-delete: true diff --git a/CHANGELOG.md b/CHANGELOG.md index a97f9a1..6822452 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to this project are documented in this file. +## 8.5.3 - Unreleased + +### Fixed + +- `Money::format()` with a `$locale` no longer prints a negative zero for currencies whose Intl fraction digits differ from ISO 4217: `Money::format(-0.4, 'IQD', locale: 'en_US')` returned `"-IQD 0"` and now returns `"IQD 0"`. The amount is compared with Intl's own formatted zero instead of being rounded with the ISO 4217 digits. + +### Changed + +- The internal `Support\Assert` and `Support\Excerpt` helpers moved to `Pharaonic\Readable`, and `Support\Intl` became `Pharaonic\Readable\Services\Integration\IntlService`. They stay `@internal` and are not part of the public API. +- The documentation has an upgrade guide. +- CI runs validation, style, static analysis, tests, the security audit and an archive check in order, behind one quality gate. + ## 8.5.2 - 2026-10-11 ### Fixed diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md index cb3afbf..8dbb760 100644 --- a/CODE_OF_CONDUCT.md +++ b/CODE_OF_CONDUCT.md @@ -2,7 +2,7 @@ ## Our standards -Be respectful, constructive, and focused on the technical work. Harassment, discrimination, personal attacks, and intentionally disruptive behavior are not acceptable. +Be respectful, constructive, and focused on the technical work. Respect other people's privacy and never publish their private information. Harassment, discrimination, personal attacks, and intentionally disruptive behavior are not acceptable. ## Scope @@ -11,3 +11,5 @@ This code of conduct applies to repository discussions, issues, pull requests, r ## Enforcement Project maintainers may edit, remove, or reject contributions and participation that violate these standards. + +Report conduct concerns privately to the maintainers rather than in public threads. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8679c19..05cb36f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -36,7 +36,7 @@ git remote add upstream https://github.com/Pharaonic/php-readable.git ## Choose the target branch -Check out the branch that matches the PHP version you are targeting: +Check out the branch that matches the PHP version you are targeting. For a bug fix, use the oldest line that has the bug: ```bash git checkout 8.5.x @@ -88,7 +88,7 @@ All checks must pass. CI also runs the tests with the lowest allowed dependency git push origin fix/compact-rounding ``` -Open the pull request against the version branch you started from, and fill in the pull request template. Do not submit the same change to multiple version branches unless asked to. +Open the pull request against the version branch you started from, and fill in the pull request template. Maintainers port fixes from the oldest affected line to the newer lines, so do not submit the same change to several branches. ## Code of Conduct diff --git a/MAINTAINING.md b/MAINTAINING.md index 92a8069..e9c0d9d 100644 --- a/MAINTAINING.md +++ b/MAINTAINING.md @@ -18,7 +18,7 @@ One branch per PHP minor, each requiring exactly that minor (`"php": ">=8.N <8.N ## Public API invariants -- The public API is the static methods of `Arr`, `Str`, `Number`, `Bytes`, `Money` and `Duration`, and `Exceptions\MissingIntlExtension`. Everything under `Support\` is `@internal`. +- The public API is the static methods of `Arr`, `Str`, `Number`, `Bytes`, `Money` and `Duration`, and `Exceptions\MissingIntlExtension`. `Assert`, `Excerpt` and `Services\Integration\IntlService` are `@internal`, not part of the public API. - Without a `$locale`, output comes from the built-in English formatter and is identical on every machine, with or without `ext-intl`. - With a `$locale`, `ext-intl` is used; without it, `MissingIntlExtension` is thrown. Invalid locales never leak `ValueError` or `IntlException`. - Rounding is half away from zero everywhere, a rounded value carries to the next unit (`999 999 B` is `1 MB`), and no formatter prints negative zero. diff --git a/README.md b/README.md index 42a114c..d805fdb 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,14 @@

PHP Version : 8.5 License - Tests + Build
- Source + Source Packagist Version Packagist Downloads

Human-friendly numbers, sizes, money and durations for PHP.

-
Format numbers, byte sizes, money and durations into readable text with predictable output, plus optional Intl locales.

## Installation diff --git a/composer.json b/composer.json index 240bc99..633bd36 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "require-dev": { "laravel/pint": "^1.20", "pestphp/pest": "^4.0", - "phpstan/phpstan": "^2.1.33" + "phpstan/phpstan": "^2.1.33", + "phpunit/phpunit": "^12.5.8" }, "suggest": { "ext-intl": "Required only when passing a $locale to the Number, Money and Bytes formatters." diff --git a/docs/sections/14-upgrade-guide.md b/docs/sections/14-upgrade-guide.md new file mode 100644 index 0000000..ede6d3b --- /dev/null +++ b/docs/sections/14-upgrade-guide.md @@ -0,0 +1,23 @@ +## Upgrade Guide + +Readable has one release line per PHP version, and every line has the same classes, methods and output. This page covers the `8.5.x` line, for PHP 8.5. + +### Patch Releases + +Patch releases such as `8.5.0` to `8.5.3` never break your code. Update with: + +```bash no-line-numbers +composer update pharaonic/php-readable +``` + +Output changes only to fix a bug, and every such change is listed in the changelog. + +### Moving To Another PHP Version + +No code changes are needed. Composer picks the line that matches your PHP version, so after upgrading PHP run `composer update pharaonic/php-readable`. + +The constraint that `composer require` writes (for example `^8.5`) already allows the newer lines. If you pinned a line with a tilde constraint such as `~8.5.0`, widen it first, or Composer stays on `8.5.x` and cannot install on the new PHP version. + +### Changelog + +Every release is listed in the [changelog](https://github.com/Pharaonic/php-readable/blob/8.5.x/CHANGELOG.md) and on the [GitHub releases](https://github.com/Pharaonic/php-readable/releases) page. diff --git a/docs/sidebar.md b/docs/sidebar.md index d7ab76d..ecb4c1f 100644 --- a/docs/sidebar.md +++ b/docs/sidebar.md @@ -17,3 +17,5 @@ - [Troubleshooting](#troubleshooting) - Community - [Contributors](#contributors) +- Upgrades + - [Upgrade Guide](#upgrade-guide) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 8b80bda..2f64a88 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,11 +7,8 @@ failOnWarning="true" failOnDeprecation="true"> - - tests/Unit - - - tests/Feature + + tests diff --git a/src/Support/Assert.php b/src/Assert.php similarity index 95% rename from src/Support/Assert.php rename to src/Assert.php index d80edcd..51629e9 100644 --- a/src/Support/Assert.php +++ b/src/Assert.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pharaonic\Readable\Support; +namespace Pharaonic\Readable; use InvalidArgumentException; diff --git a/src/Bytes.php b/src/Bytes.php index 86a3fc2..cf7d516 100644 --- a/src/Bytes.php +++ b/src/Bytes.php @@ -5,8 +5,6 @@ namespace Pharaonic\Readable; use InvalidArgumentException; -use Pharaonic\Readable\Support\Assert; -use Pharaonic\Readable\Support\Excerpt; /** * Readable byte sizes. diff --git a/src/Duration.php b/src/Duration.php index 0c98119..72d356f 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -8,7 +8,6 @@ use DateTimeInterface; use Exception; use InvalidArgumentException; -use Pharaonic\Readable\Support\Excerpt; /** * Readable durations in English: "1 hour 1 minute", or "1h 1m" in short form. diff --git a/src/Exceptions/MissingIntlExtension.php b/src/Exceptions/MissingIntlExtension.php index 3bc5177..14eb1c2 100644 --- a/src/Exceptions/MissingIntlExtension.php +++ b/src/Exceptions/MissingIntlExtension.php @@ -4,7 +4,7 @@ namespace Pharaonic\Readable\Exceptions; -use Pharaonic\Readable\Support\Excerpt; +use Pharaonic\Readable\Excerpt; use RuntimeException; /** diff --git a/src/Support/Excerpt.php b/src/Excerpt.php similarity index 93% rename from src/Support/Excerpt.php rename to src/Excerpt.php index 2ea6010..75b4510 100644 --- a/src/Support/Excerpt.php +++ b/src/Excerpt.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Pharaonic\Readable\Support; +namespace Pharaonic\Readable; /** * @internal diff --git a/src/Money.php b/src/Money.php index 0a1e92a..9f6f68a 100644 --- a/src/Money.php +++ b/src/Money.php @@ -5,9 +5,7 @@ namespace Pharaonic\Readable; use InvalidArgumentException; -use Pharaonic\Readable\Support\Assert; -use Pharaonic\Readable\Support\Excerpt; -use Pharaonic\Readable\Support\Intl; +use Pharaonic\Readable\Services\Integration\IntlService; /** * Readable money amounts. Formatting only: no arithmetic and no exchange rates. @@ -47,18 +45,24 @@ public static function format( } if ($locale !== null) { - // Like the built-in path, an amount that rounds to zero must not print as "-$0.00". - if ($amount < 0 && round($amount, $decimals ?? self::fractionDigits($currency)) == 0) { - $amount = 0; + $formatter = IntlService::formatter($locale, 'CURRENCY'); + + if ($decimals !== null) { + IntlService::fractionDigits($formatter, $decimals); } - $formatter = Intl::formatter($locale, 'CURRENCY'); + // Like the built-in path, an amount that rounds to zero must not print as "-$0.00". Intl rounds + // with its own currency digits, which can differ from ISO 4217 (IQD has 0, not 3), so compare + // against Intl's own zero instead of rounding here. + if ($amount < 0) { + $zero = IntlService::result($formatter->formatCurrency(0, $currency), $formatter); - if ($decimals !== null) { - Intl::fractionDigits($formatter, $decimals); + if (IntlService::result($formatter->formatCurrency(abs($amount), $currency), $formatter) === $zero) { + return $zero; + } } - return Intl::result($formatter->formatCurrency($amount, $currency), $formatter); + return IntlService::result($formatter->formatCurrency($amount, $currency), $formatter); } $decimals ??= self::fractionDigits($currency); diff --git a/src/Number.php b/src/Number.php index 729bc8d..8195ea8 100644 --- a/src/Number.php +++ b/src/Number.php @@ -5,8 +5,7 @@ namespace Pharaonic\Readable; use InvalidArgumentException; -use Pharaonic\Readable\Support\Assert; -use Pharaonic\Readable\Support\Intl; +use Pharaonic\Readable\Services\Integration\IntlService; /** * Readable numbers. @@ -48,10 +47,10 @@ public static function format( $number = self::withoutNegativeZero($number, $decimals); if ($locale !== null) { - $formatter = Intl::formatter($locale, 'DECIMAL'); - Intl::fractionDigits($formatter, $decimals, $trimZeros); + $formatter = IntlService::formatter($locale, 'DECIMAL'); + IntlService::fractionDigits($formatter, $decimals, $trimZeros); - return Intl::result($formatter->format($number), $formatter); + return IntlService::result($formatter->format($number), $formatter); } $formatted = number_format($number, $decimals, '.', ','); @@ -100,10 +99,10 @@ public static function percentage(int|float $ratio, int $decimals = 0, ?string $ Assert::decimals($decimals); if ($locale !== null) { - $formatter = Intl::formatter($locale, 'PERCENT'); - Intl::fractionDigits($formatter, $decimals); + $formatter = IntlService::formatter($locale, 'PERCENT'); + IntlService::fractionDigits($formatter, $decimals); - return Intl::result($formatter->format(self::withoutNegativeZero($ratio, $decimals + 2)), $formatter); + return IntlService::result($formatter->format(self::withoutNegativeZero($ratio, $decimals + 2)), $formatter); } $percent = $ratio * 100; @@ -123,9 +122,9 @@ public static function percentage(int|float $ratio, int $decimals = 0, ?string $ public static function ordinal(int $number, ?string $locale = null): string { if ($locale !== null) { - $formatter = Intl::formatter($locale, 'ORDINAL'); + $formatter = IntlService::formatter($locale, 'ORDINAL'); - return Intl::result($formatter->format($number), $formatter); + return IntlService::result($formatter->format($number), $formatter); } $lastTwo = abs($number % 100); @@ -150,8 +149,8 @@ public static function spell(int|float $number, ?string $locale = null): string Assert::finite($number); if ($locale !== null) { - $formatter = Intl::formatter($locale, 'SPELLOUT'); - $words = Intl::result($formatter->format($number), $formatter); + $formatter = IntlService::formatter($locale, 'SPELLOUT'); + $words = IntlService::result($formatter->format($number), $formatter); // In Arabic the conjunction "و" attaches to the following word. return preg_match('/^ar(?:[_-]|$)/i', $locale) === 1 ? str_replace('و ', 'و', $words) : $words; diff --git a/src/Support/Intl.php b/src/Services/Integration/IntlService.php similarity index 93% rename from src/Support/Intl.php rename to src/Services/Integration/IntlService.php index 080cb82..aabc485 100644 --- a/src/Support/Intl.php +++ b/src/Services/Integration/IntlService.php @@ -2,18 +2,19 @@ declare(strict_types=1); -namespace Pharaonic\Readable\Support; +namespace Pharaonic\Readable\Services\Integration; use IntlException; use InvalidArgumentException; use NumberFormatter; use Pharaonic\Readable\Exceptions\MissingIntlExtension; +use Pharaonic\Readable\Excerpt; use ValueError; /** - * @internal + * @internal Integration with ext-intl's NumberFormatter. */ -final class Intl +final class IntlService { /** * Create a NumberFormatter that rounds half away from zero, like number_format(). diff --git a/tests/Feature/.gitkeep b/tests/Feature/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/Unit/MoneyTest.php b/tests/Unit/MoneyTest.php index 4f88be0..7a4aba8 100644 --- a/tests/Unit/MoneyTest.php +++ b/tests/Unit/MoneyTest.php @@ -61,6 +61,23 @@ ->and(Money::format(-0.005, 'USD', null, 'en_US'))->toBe('-$0.01'); })->skip(withoutIntl(), 'ext-intl is not installed.'); +it('never prints negative zero when Intl uses other currency digits than ISO 4217', function (string $currency, string $locale) { + // Intl decides the digits with a locale (IQD has 0 in ICU, 3 in ISO 4217), so take the amount from Intl itself. + $digits = (new NumberFormatter($locale . '@currency=' . $currency, NumberFormatter::CURRENCY)) + ->getAttribute(NumberFormatter::MAX_FRACTION_DIGITS); + $amount = -0.4 / 10 ** $digits; + + expect(Money::format($amount, $currency, null, $locale))->toBe(Money::format(0, $currency, null, $locale)) + ->and(Money::format(-$amount * 2, $currency, null, $locale))->not->toBe(Money::format(0, $currency, null, $locale)); +})->with([ + 'IQD in en_US' => ['IQD', 'en_US'], + 'IQD in de_DE' => ['IQD', 'de_DE'], + 'IQD in ar_EG' => ['IQD', 'ar_EG'], + 'IRR in en_US' => ['IRR', 'en_US'], + 'YER in ar_EG' => ['YER', 'ar_EG'], + 'USD in en_US' => ['USD', 'en_US'], +])->skip(withoutIntl(), 'ext-intl is not installed.'); + it('needs ext-intl to format money with a locale', function () { Money::format(1, 'USD', null, 'en_US'); })->skip(! withoutIntl(), 'ext-intl is installed.')->throws(MissingIntlExtension::class);