| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- namespace Tests\Unit;
- use App\Support\PromotionAmountFormatter;
- use Illuminate\Support\Collection;
- use PHPUnit\Framework\TestCase;
- class PromotionAmountFormatterTest extends TestCase
- {
- /** @dataProvider amounts */
- public function test_amounts_use_two_truncated_decimals_but_hide_a_zero_fraction($input, $expected): void
- {
- $this->assertSame($expected, PromotionAmountFormatter::amount($input));
- }
- public static function amounts(): array
- {
- return [['200.00000000', '200'], ['200.12000000', '200.12'], ['200.10000000', '200.10'],
- ['200.129', '200.12'], ['199.9999999999', '199.99'], ['200', '200'], [200, '200'],
- ['0.0000000000', '0'], ['0.009999', '0'], ['-0.009', '0'], ['-200.129', '-200.12'],
- ['99999999999999.1299999999', '99999999999999.12'], [null, null], ['invalid', 'invalid']];
- }
- public function test_only_monetary_fields_are_formatted_and_count_time_probability_and_snapshots_are_preserved(): void
- {
- $input = ['activity_id' => 200, 'spins' => 2, 'rules' => ['min_deposit' => '200.00000000', 'completion_probability' => '20.00'],
- 'valid_member_policy' => ['min_deposit' => '200.0000000000', 'min_effective_bet' => '300.1200000000'],
- 'tasks' => [
- ['key' => 'deposit', 'value' => '200.1299999999', 'target' => '3000.00'],
- ['key' => 'online', 'value' => 200, 'target' => 3600, 'remaining_seconds' => 3400],
- ['key' => 'invite', 'value' => 1, 'target' => 1],
- ], 'rule_snapshot' => '{"amount":"200.00000000"}', 'budget' => null];
- $actual = PromotionAmountFormatter::response($input);
- $this->assertSame('200', $actual['rules']['min_deposit']);
- $this->assertSame('200.12', $actual['tasks'][0]['value']); $this->assertSame('3000', $actual['tasks'][0]['target']);
- $this->assertSame('200', $actual['valid_member_policy']['min_deposit']);
- $this->assertSame('300.12', $actual['valid_member_policy']['min_effective_bet']);
- $this->assertSame($input['activity_id'], $actual['activity_id']); $this->assertSame($input['spins'], $actual['spins']);
- $this->assertSame('20.00', $actual['rules']['completion_probability']);
- $this->assertSame($input['tasks'][1], $actual['tasks'][1]); $this->assertSame($input['tasks'][2], $actual['tasks'][2]);
- $this->assertSame($input['rule_snapshot'], $actual['rule_snapshot']); $this->assertNull($actual['budget']);
- $this->assertSame('200.1299999999', $input['tasks'][0]['value']);
- }
- public function test_admin_record_collections_are_presented_without_mutating_the_original_rows(): void
- {
- $row = (object) ['amount' => '200.00000000', 'required_flow' => '400.12000000', 'user_id' => 106];
- $actual = PromotionAmountFormatter::response(['total' => 1, 'data' => new Collection([$row])]);
- $this->assertSame('200', $actual['data'][0]->amount);
- $this->assertSame('400.12', $actual['data'][0]->required_flow);
- $this->assertSame(106, $actual['data'][0]->user_id);
- $this->assertSame('200.00000000', $row->amount);
- }
- }
|