Files

219 lines
9.1 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Rules\Donation;
use StreamGoals\Rules\Engine;
use StreamGoals\Rules\RuleDef;
final class RulesEngineTest extends TestCase {
private FakeStateRepository $state;
private FakeEventStore $events;
private int $now = 1000;
private array $fired = array();
private function engine( array $rules ): Engine {
$this->state = new FakeStateRepository();
$this->events = new FakeEventStore();
return new Engine(
new FakeRuleStore( $rules ),
$this->events,
$this->state,
fn(): int => $this->now,
function ( string $e, RuleDef $r, Donation $d, bool $rev ): void {
$this->fired[] = array( $e, $d->id, $rev );
}
);
}
private function d( int $id, int $base, string $cur = 'USD' ): Donation {
return new Donation( $id, 1, $base, 0, $base, $cur );
}
private function timer( array $extra = array() ): RuleDef {
return new RuleDef( 1, 1, 'add_time', $extra + array( 'seconds_per_unit' => 60, 'unit_minor' => 100 ) );
}
private function counter( array $extra = array() ): RuleDef {
return new RuleDef( 2, 1, 'counter', $extra + array( 'step' => 1, 'per_minor' => 1000, 'carry_over' => true ) );
}
public function test_apply_persists_timer_counter_and_events(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$e->applyDonation( $this->d( 10, 2500 ) );
$this->assertSame( 1000 + 1500, $this->state->getInt( 1, 'timer_ends_at' ) );
$this->assertSame( 2, $this->state->getInt( 1, 'counter:2' ) );
$this->assertSame( 500, $this->state->getInt( 1, 'carry:2' ) );
$this->assertSame( 1500, $this->events->events['1:10'][0] );
}
public function test_replay_is_idempotent(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );
$e->applyDonation( $d );
$snapshot = $this->state->data;
$e->applyDonation( $d );
$e->applyDonation( $d );
$this->assertSame( $snapshot, $this->state->data );
}
public function test_expired_timer_restarts_from_now(): void {
$e = $this->engine( array( $this->timer() ) );
$this->state->setInt( 1, 'timer_ends_at', 10 );
$e->applyDonation( $this->d( 10, 100 ) );
$this->assertSame( 1060, $this->state->getInt( 1, 'timer_ends_at' ) );
}
public function test_running_timer_extends(): void {
$e = $this->engine( array( $this->timer() ) );
$this->state->setInt( 1, 'timer_ends_at', 5000 );
$e->applyDonation( $this->d( 10, 100 ) );
$this->assertSame( 5060, $this->state->getInt( 1, 'timer_ends_at' ) );
}
public function test_max_total_across_donations(): void {
$e = $this->engine( array( $this->timer( array( 'max_total_seconds' => 100 ) ) ) );
$e->applyDonation( $this->d( 1, 100 ) );
$e->applyDonation( $this->d( 2, 100 ) );
$this->assertSame( 1000 + 100, $this->state->getInt( 1, 'timer_ends_at' ) );
}
private function ends(): int {
return $this->state->getInt( 1, 'timer_ends_at' );
}
public function test_max_timer_clamps_and_records_actual_delta(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 1200 ) ) ) ); // now=1000
$this->state->setInt( 1, 'timer_ends_at', 2000 ); // 1000s remaining
$e->applyDonation( $this->d( 1, 1000 ) ); // wants +600, room 200
$this->assertSame( 2200, $this->ends() );
$this->assertSame( 200, $this->events->events['1:1'][0] );
$this->assertSame( 200, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_exactly_at_cap_adds_zero(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 1000 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 2000 );
$e->applyDonation( $this->d( 1, 100 ) );
$this->assertSame( 2000, $this->ends() );
$this->assertSame( 0, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_lowered_below_existing_timer_does_not_shorten(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 100 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 9000 );
$e->applyDonation( $this->d( 1, 100 ) );
$this->assertSame( 9000, $this->ends() );
$this->assertSame( 0, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_uncapped_by_default_and_null(): void {
foreach ( array( array(), array( 'max_timer_seconds' => null ) ) as $extra ) {
$e = $this->engine( array( $this->timer( $extra ) ) );
$this->state->setInt( 1, 'timer_ends_at', 10000000 );
$e->applyDonation( $this->d( 1, 100000 ) );
$this->assertSame( 10000000 + 60000, $this->ends() );
}
}
public function test_max_timer_with_other_caps_most_restrictive_wins(): void {
// per-donation 50 is the tightest
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 500, 'cap_seconds_per_donation' => 50, 'max_total_seconds' => 400 ) ) ) );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1050, $this->ends() );
// max_total tightest: 30 left of 40 total, clock has room
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 500, 'max_total_seconds' => 40 ) ) ) );
$this->state->setInt( 1, 'timer_added:1', 10 );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1030, $this->ends() );
$this->assertSame( 40, $this->state->getInt( 1, 'timer_added:1' ) );
// clock cap tightest: only 20 of room
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 120, 'cap_seconds_per_donation' => 300, 'max_total_seconds' => 900 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 1100 );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1120, $this->ends() );
$this->assertSame( 20, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_with_expired_timer_starts_from_now(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 100 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 10 );
$e->applyDonation( $this->d( 1, 1000 ) ); // wants 600
$this->assertSame( 1100, $this->ends() );
}
public function test_max_timer_replay_idempotent_and_reversal_uses_clamped_delta(): void {
$e = $this->engine( array( $this->timer( array( 'max_timer_seconds' => 1200 ) ) ) );
$this->state->setInt( 1, 'timer_ends_at', 2000 );
$d = $this->d( 1, 1000 );
$e->applyDonation( $d );
$e->applyDonation( $d );
$this->assertSame( 2200, $this->ends() );
$e->reverseDonation( $d );
$e->reverseDonation( $d );
$this->assertSame( 2000, $this->ends() );
$this->assertSame( 0, $this->state->getInt( 1, 'timer_added:1' ) );
}
public function test_max_timer_invalid_value_rejected_by_rule(): void {
$this->expectException( \InvalidArgumentException::class );
\StreamGoals\Rules\AddTimeRule::fromConfig( array( 'seconds_per_unit' => 60, 'unit_minor' => 100, 'max_timer_seconds' => 0 ) );
}
public function test_reversal_undoes_and_is_not_double_applied(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );
$e->applyDonation( $d );
$e->reverseDonation( $d );
$e->reverseDonation( $d );
$this->assertSame( 1000, $this->state->getInt( 1, 'timer_ends_at' ) );
$this->assertSame( 0, $this->state->getInt( 1, 'counter:2' ) );
$this->assertSame( 0, $this->state->getInt( 1, 'carry:2' ) );
}
public function test_reversal_never_below_floor(): void {
$e = $this->engine( array( $this->timer(), $this->counter() ) );
$d = $this->d( 10, 2500 );
$e->applyDonation( $d );
$this->state->setInt( 1, 'counter:2', 1 ); // already lower than the delta
$this->now = 5000; // timer long expired
$e->reverseDonation( $d );
$this->assertSame( 0, $this->state->getInt( 1, 'counter:2' ) );
$this->assertSame( 2500, $this->state->getInt( 1, 'timer_ends_at' ) ); // untouched, not pushed below
}
public function test_reverse_on_refund_false_is_honored(): void {
$e = $this->engine( array( $this->counter( array( 'reverse_on_refund' => false ) ) ) );
$d = $this->d( 10, 2000 );
$e->applyDonation( $d );
$e->reverseDonation( $d );
$this->assertSame( 2, $this->state->getInt( 1, 'counter:2' ) );
}
public function test_reverse_unapplied_donation_is_noop(): void {
$e = $this->engine( array( $this->counter() ) );
$this->state->setInt( 1, 'counter:2', 5 );
$e->reverseDonation( $this->d( 99, 5000 ) );
$this->assertSame( 5, $this->state->getInt( 1, 'counter:2' ) );
}
public function test_bonus_fires_once_and_zero_decimal_currency(): void {
$bonus = new RuleDef( 3, 1, 'bonus', array( 'min_minor' => 5000, 'event' => 'big_donation' ) );
$e = $this->engine( array( $bonus, $this->counter( array( 'per_minor' => 1000 ) ) ) );
$d = $this->d( 7, 6000, 'JPY' );
$e->applyDonation( $d );
$e->applyDonation( $d );
$this->assertSame( array( array( 'big_donation', 7, false ) ), $this->fired );
$this->assertSame( 6, $this->state->getInt( 1, 'counter:2' ) );
}
public function test_bad_config_does_not_block_other_rules(): void {
$bad = new RuleDef( 9, 1, 'add_time', array( 'seconds_per_unit' => 1, 'unit_minor' => 0 ) );
$e = $this->engine( array( $bad, $this->counter() ) );
$e->applyDonation( $this->d( 1, 1000 ) );
$this->assertSame( 1, $this->state->getInt( 1, 'counter:2' ) );
}
}