PromotionAmountFormatterTest.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Support\PromotionAmountFormatter;
  4. use Illuminate\Support\Collection;
  5. use PHPUnit\Framework\TestCase;
  6. class PromotionAmountFormatterTest extends TestCase
  7. {
  8. /** @dataProvider amounts */
  9. public function test_amounts_use_two_truncated_decimals_but_hide_a_zero_fraction($input, $expected): void
  10. {
  11. $this->assertSame($expected, PromotionAmountFormatter::amount($input));
  12. }
  13. public static function amounts(): array
  14. {
  15. return [['200.00000000', '200'], ['200.12000000', '200.12'], ['200.10000000', '200.10'],
  16. ['200.129', '200.12'], ['199.9999999999', '199.99'], ['200', '200'], [200, '200'],
  17. ['0.0000000000', '0'], ['0.009999', '0'], ['-0.009', '0'], ['-200.129', '-200.12'],
  18. ['99999999999999.1299999999', '99999999999999.12'], [null, null], ['invalid', 'invalid']];
  19. }
  20. public function test_only_monetary_fields_are_formatted_and_count_time_probability_and_snapshots_are_preserved(): void
  21. {
  22. $input = ['activity_id' => 200, 'spins' => 2, 'rules' => ['min_deposit' => '200.00000000', 'completion_probability' => '20.00'],
  23. 'valid_member_policy' => ['min_deposit' => '200.0000000000', 'min_effective_bet' => '300.1200000000'],
  24. 'tasks' => [
  25. ['key' => 'deposit', 'value' => '200.1299999999', 'target' => '3000.00'],
  26. ['key' => 'online', 'value' => 200, 'target' => 3600, 'remaining_seconds' => 3400],
  27. ['key' => 'invite', 'value' => 1, 'target' => 1],
  28. ], 'rule_snapshot' => '{"amount":"200.00000000"}', 'budget' => null];
  29. $actual = PromotionAmountFormatter::response($input);
  30. $this->assertSame('200', $actual['rules']['min_deposit']);
  31. $this->assertSame('200.12', $actual['tasks'][0]['value']); $this->assertSame('3000', $actual['tasks'][0]['target']);
  32. $this->assertSame('200', $actual['valid_member_policy']['min_deposit']);
  33. $this->assertSame('300.12', $actual['valid_member_policy']['min_effective_bet']);
  34. $this->assertSame($input['activity_id'], $actual['activity_id']); $this->assertSame($input['spins'], $actual['spins']);
  35. $this->assertSame('20.00', $actual['rules']['completion_probability']);
  36. $this->assertSame($input['tasks'][1], $actual['tasks'][1]); $this->assertSame($input['tasks'][2], $actual['tasks'][2]);
  37. $this->assertSame($input['rule_snapshot'], $actual['rule_snapshot']); $this->assertNull($actual['budget']);
  38. $this->assertSame('200.1299999999', $input['tasks'][0]['value']);
  39. }
  40. public function test_admin_record_collections_are_presented_without_mutating_the_original_rows(): void
  41. {
  42. $row = (object) ['amount' => '200.00000000', 'required_flow' => '400.12000000', 'user_id' => 106];
  43. $actual = PromotionAmountFormatter::response(['total' => 1, 'data' => new Collection([$row])]);
  44. $this->assertSame('200', $actual['data'][0]->amount);
  45. $this->assertSame('400.12', $actual['data'][0]->required_flow);
  46. $this->assertSame(106, $actual['data'][0]->user_id);
  47. $this->assertSame('200.00000000', $row->amount);
  48. }
  49. }