76 lines
4.0 KiB
PHP
76 lines
4.0 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Tests\Unit;
|
||
|
|
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use StreamGoals\Alerts\Resolver;
|
||
|
|
|
||
|
|
final class AlertResolverTest extends TestCase {
|
||
|
|
|
||
|
|
private function def( int $id, array $o = array() ): array {
|
||
|
|
return $o + array( 'id' => $id, 'goal_id' => 0, 'trigger_type' => 'amount', 'min_minor' => 100, 'action_id' => 0, 'enabled' => 1, 'sort' => 0 );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_highest_matching_amount_wins(): void {
|
||
|
|
$defs = array( $this->def( 1, array( 'min_minor' => 100 ) ), $this->def( 2, array( 'min_minor' => 1000 ) ), $this->def( 3, array( 'min_minor' => 5000 ) ) );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 1000, array(), $defs )['id'] );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 4999, array(), $defs )['id'] );
|
||
|
|
$this->assertSame( 3, Resolver::pick( 1, 99999, array(), $defs )['id'] );
|
||
|
|
$this->assertSame( 1, Resolver::pick( 1, 100, array(), $defs )['id'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_none_when_below_every_minimum_or_no_defs(): void {
|
||
|
|
$this->assertNull( Resolver::pick( 1, 99, array(), array( $this->def( 1 ) ) ) );
|
||
|
|
$this->assertNull( Resolver::pick( 1, 5000, array(), array() ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_reward_beats_amount(): void {
|
||
|
|
$defs = array( $this->def( 1, array( 'min_minor' => 100 ) ), $this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 9 ) ) );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 5000, array( 9 ), $defs )['id'] );
|
||
|
|
$this->assertSame( 1, Resolver::pick( 1, 5000, array( 8 ), $defs )['id'], 'other reward does not match' );
|
||
|
|
$this->assertSame( 1, Resolver::pick( 1, 5000, array(), $defs )['id'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_reward_def_matches_even_when_base_is_tiny(): void {
|
||
|
|
$defs = array( $this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 9, 'min_minor' => 0 ) ) );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 1, array( 9 ), $defs )['id'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_several_rewards_highest_reward_amount_then_sort_then_id(): void {
|
||
|
|
$defs = array(
|
||
|
|
$this->def( 1, array( 'trigger_type' => 'reward', 'action_id' => 5, 'sort' => 0 ) ),
|
||
|
|
$this->def( 2, array( 'trigger_type' => 'reward', 'action_id' => 6, 'sort' => 9 ) ),
|
||
|
|
);
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 5000, array( 5, 6 ), $defs, array( 5 => 500, 6 => 2500 ) )['id'] );
|
||
|
|
$this->assertSame( 1, Resolver::pick( 1, 5000, array( 5, 6 ), $defs, array( 5 => 500, 6 => 500 ) )['id'], 'tie: lower sort' );
|
||
|
|
$defs[1]['sort'] = 0;
|
||
|
|
$this->assertSame( 1, Resolver::pick( 1, 5000, array( 5, 6 ), $defs )['id'], 'tie: lower id' );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_goal_scoping(): void {
|
||
|
|
$defs = array( $this->def( 1, array( 'goal_id' => 7, 'min_minor' => 100 ) ), $this->def( 2, array( 'goal_id' => 0, 'min_minor' => 100, 'sort' => 5 ) ) );
|
||
|
|
$this->assertSame( 1, Resolver::pick( 7, 500, array(), $defs )['id'] );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 8, 500, array(), $defs )['id'], 'goal 8 never sees goal 7 defs' );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 0, 500, array(), $defs )['id'], 'test donations (goal 0) use goal-0 defs only' );
|
||
|
|
$only7 = array( $defs[0] );
|
||
|
|
$this->assertNull( Resolver::pick( 0, 500, array(), $only7 ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_disabled_ignored(): void {
|
||
|
|
$defs = array( $this->def( 1, array( 'enabled' => 0, 'min_minor' => 1000 ) ), $this->def( 2, array( 'min_minor' => 100 ) ),
|
||
|
|
$this->def( 3, array( 'trigger_type' => 'reward', 'action_id' => 9, 'enabled' => 0 ) ) );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 5000, array( 9 ), $defs )['id'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_string_columns_from_db_are_handled_and_unknown_trigger_ignored(): void {
|
||
|
|
$defs = array( array( 'id' => '4', 'goal_id' => '0', 'trigger_type' => 'amount', 'min_minor' => '500', 'enabled' => '1', 'sort' => '0' ),
|
||
|
|
array( 'id' => '5', 'trigger_type' => 'bogus', 'enabled' => 1 ) );
|
||
|
|
$this->assertSame( 4, Resolver::pick( 3, 500, array(), $defs )['id'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_equal_minimum_tie_breaks_by_sort_then_id(): void {
|
||
|
|
$defs = array( $this->def( 1, array( 'sort' => 5 ) ), $this->def( 2, array( 'sort' => 1 ) ) );
|
||
|
|
$this->assertSame( 2, Resolver::pick( 1, 500, array(), $defs )['id'] );
|
||
|
|
}
|
||
|
|
}
|