Files
wp-donations/tests/Unit/RulesPureTest.php
T

82 lines
3.5 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Rules\AddTimeRule;
use StreamGoals\Rules\BonusRule;
use StreamGoals\Rules\CounterRule;
use StreamGoals\Rules\Donation;
final class RulesPureTest extends TestCase {
private function d( int $base, int $fee = 0, string $cur = 'USD' ): Donation {
return new Donation( 1, 1, $base, $fee, $base + $fee, $cur );
}
public function test_counter_carry_over(): void {
$r = new CounterRule( 1, 1000 );
$a = $r->apply( $this->d( 2500 ), 0, 0 );
$this->assertSame( array( 2, 2, 500 ), array( $a['delta'], $a['counter'], $a['carry'] ) );
$b = $r->apply( $this->d( 500 ), $a['counter'], $a['carry'] );
$this->assertSame( array( 1, 3, 0 ), array( $b['delta'], $b['counter'], $b['carry'] ) );
}
public function test_counter_without_carry_discards_remainder(): void {
$a = ( new CounterRule( 1, 1000, false ) )->apply( $this->d( 2500 ), 0, 700 );
$this->assertSame( array( 2, 0 ), array( $a['delta'], $a['carry'] ) );
}
public function test_counter_reverse_floors_at_zero_and_rolls_carry(): void {
$r = new CounterRule( 1, 1000 );
$x = $r->reverse( $this->d( 2500 ), 2, 1, 500 );
$this->assertSame( array( 0, 0 ), array( $x['counter'], $x['carry'] ) );
}
public function test_add_time_proportional_and_caps(): void {
$r = new AddTimeRule( 60, 100, 300, 400 );
$this->assertSame( 150, ( new AddTimeRule( 60, 100 ) )->apply( $this->d( 250 ), 0, 0, 1000 )['delta'] );
$this->assertSame( 300, $r->apply( $this->d( 10000 ), 0, 0, 1000 )['delta'] ); // per-donation cap
$this->assertSame( 100, $r->apply( $this->d( 10000 ), 0, 300, 1000 )['delta'] ); // total cap
$this->assertSame( 0, $r->apply( $this->d( 10000 ), 0, 400, 1000 )['delta'] );
}
public function test_add_time_expired_starts_from_now_and_running_extends(): void {
$r = new AddTimeRule( 60, 100 );
$this->assertSame( 1000 + 120, $r->apply( $this->d( 200 ), 500, 0, 1000 )['ends_at'] );
$this->assertSame( 2000 + 120, $r->apply( $this->d( 200 ), 2000, 0, 1000 )['ends_at'] );
}
public function test_add_time_reverse_floors_at_now(): void {
$r = new AddTimeRule( 60, 100 );
$this->assertSame( 1000, $r->reverse( 500, 1200, 500, 1000 )['ends_at'] );
$this->assertSame( 500, $r->reverse( 100, 500, 100, 1000 )['ends_at'] ); // expired: untouched
}
public function test_basis_base_vs_total(): void {
$d = $this->d( 1000, 100 );
$this->assertSame( 600, ( new AddTimeRule( 60, 100, null, null, 'base' ) )->apply( $d, 0, 0, 0 )['delta'] );
$this->assertSame( 660, ( new AddTimeRule( 60, 100, null, null, 'total' ) )->apply( $d, 0, 0, 0 )['delta'] );
$this->assertSame( 1, ( new CounterRule( 1, 1100, true, 'total' ) )->apply( $d, 0, 0 )['delta'] );
$this->assertSame( 0, ( new CounterRule( 1, 1100, true, 'base' ) )->apply( $d, 0, 0 )['delta'] );
}
public function test_zero_decimal_currency(): void {
// 500 JPY = 500 minor; 1 minute per 100 yen.
$d = $this->d( 500, 0, 'JPY' );
$this->assertSame( 300, ( new AddTimeRule( 60, 100 ) )->apply( $d, 0, 0, 0 )['delta'] );
$this->assertSame( 5, ( new CounterRule( 1, 100 ) )->apply( $d, 0, 0 )['delta'] );
}
public function test_bonus_threshold(): void {
$r = new BonusRule( 5000, 'big_donation' );
$this->assertSame( 0, $r->apply( $this->d( 4999 ) ) );
$this->assertSame( 1, $r->apply( $this->d( 5000 ) ) );
}
public function test_invalid_config_throws(): void {
$this->expectException( \InvalidArgumentException::class );
AddTimeRule::fromConfig( array( 'seconds_per_unit' => 1, 'unit_minor' => 0 ) );
}
}