您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LocalizationTest.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. use App\Models\Setting\Localization;
  3. use App\Utilities\RateCalculator;
  4. // RateCalculator Basic Operations
  5. it('calculates percentage correctly', function () {
  6. $valueInCents = 100000; // 1000 dollars in cents
  7. $scaledRate = RateCalculator::decimalToScaledRate(0.025); // 2.5%
  8. expect(RateCalculator::calculatePercentage($valueInCents, $scaledRate))
  9. ->toBe(2500); // Should be 25 dollars in cents
  10. });
  11. it('converts between scaled rates and decimals correctly', function (float $decimal, int $scaled) {
  12. // Test decimal to scaled
  13. expect(RateCalculator::decimalToScaledRate($decimal))->toBe($scaled)
  14. ->and(RateCalculator::scaledRateToDecimal($scaled))->toBe($decimal);
  15. })->with([
  16. [0.25, 250000], // 0.25 * 1000000 = 250000
  17. [0.1, 100000], // 0.1 * 1000000 = 100000
  18. [0.01, 10000], // 0.01 * 1000000 = 10000
  19. [0.001, 1000], // 0.001 * 1000000 = 1000
  20. [0.0001, 100], // 0.0001 * 1000000 = 100
  21. ]);
  22. it('handles rate formatting correctly for different computations', function () {
  23. $localization = Localization::firstOrFail();
  24. $localization->update(['language' => 'en']);
  25. // Test fixed amount formatting
  26. expect(rateFormat(100000, 'fixed', 'MYR'))->toBe('$100,000.00 USD');
  27. // Test percentage formatting
  28. $scaledRate = RateCalculator::decimalToScaledRate(0.000025); // 0.25%
  29. expect(rateFormat($scaledRate, 'percentage'))->toBe('25%');
  30. });
  31. // Edge Cases and Error Handling
  32. it('handles edge cases correctly', function () {
  33. $localization = Localization::firstOrFail();
  34. $localization->update(['language' => 'en']);
  35. expect(RateCalculator::formatScaledRate(0))->toBe('0')
  36. ->and(RateCalculator::formatScaledRate(1))->toBe('0.0001')
  37. ->and(RateCalculator::formatScaledRate(10000000))->toBe('1,000')
  38. ->and(RateCalculator::formatScaledRate(-250000))->toBe('-25');
  39. });
  40. // Precision Tests
  41. it('maintains precision correctly', function () {
  42. $localization = Localization::firstOrFail();
  43. $localization->update(['language' => 'en']);
  44. $testCases = [
  45. '1.0000' => '1',
  46. '1.2300' => '1.23',
  47. '1.2340' => '1.234',
  48. '1.2345' => '1.2345',
  49. ];
  50. foreach ($testCases as $input => $expected) {
  51. $scaled = RateCalculator::parseLocalizedRate($input);
  52. expect(RateCalculator::formatScaledRate($scaled))->toBe($expected);
  53. }
  54. });