Files
wp-donations/tests/Unit/ActionMatcherTest.php
T
shadowdaoandClaude Sonnet 5.5 b0b5012698
Test and Release / test (push) Successful in 1m43s
Test and Release / release (push) Successful in 7s
Add donation actions (rewards): menu editor + queue, donate-form buttons, overlay menu widget, alert lines
- Actions\Matcher (exact / every), ActionRepository, Recorder (idempotent via unique key)
- Admin: Rewards screen with live preview, queue with Mark done, menu badge
- Donate form shows actions as payment buttons; overlay 'actions' widget; alerts list triggered actions
- Installer DB_VERSION 2 (sg_actions, sg_action_events)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 12:36:28 -08:00

79 lines
3.3 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Actions\Matcher;
final class ActionMatcherTest extends TestCase {
private function a( int $id, int $amount, string $mode, string $title = '' ): array {
return array( 'id' => $id, 'title' => '' === $title ? "A$id" : $title, 'amount_minor' => $amount, 'mode' => $mode );
}
public function test_exact_matches_only_the_same_amount(): void {
$acts = array( $this->a( 1, 2500, 'exact' ) );
$this->assertSame( array( array( 'action_id' => 1, 'title' => 'A1', 'times' => 1 ) ), Matcher::match( 2500, $acts ) );
$this->assertSame( array(), Matcher::match( 2501, $acts ) );
$this->assertSame( array(), Matcher::match( 5000, $acts ) );
}
public function test_every_uses_floor_of_multiples(): void {
$acts = array( $this->a( 1, 500, 'every' ) );
$this->assertSame( 5, Matcher::match( 2500, $acts )[0]['times'] );
$this->assertSame( 2, Matcher::match( 1099, $acts )[0]['times'], 'floor, not round' );
$this->assertSame( 1, Matcher::match( 500, $acts )[0]['times'] );
}
public function test_below_amount_never_matches(): void {
$this->assertSame( array(), Matcher::match( 499, array( $this->a( 1, 500, 'every' ), $this->a( 2, 500, 'exact' ) ) ) );
}
public function test_zero_and_negative_donations_match_nothing(): void {
$acts = array( $this->a( 1, 100, 'every' ) );
$this->assertSame( array(), Matcher::match( 0, $acts ) );
$this->assertSame( array(), Matcher::match( -500, $acts ) );
}
public function test_zero_and_negative_action_amounts_never_match(): void {
$this->assertSame( array(), Matcher::match( 500, array( $this->a( 1, 0, 'every' ), $this->a( 2, -5, 'exact' ), $this->a( 3, -500, 'every' ) ) ) );
}
public function test_several_actions_at_once_in_input_order(): void {
$acts = array( $this->a( 7, 2500, 'exact', '25 pushups' ), $this->a( 3, 500, 'every', 'Eat a bean' ), $this->a( 9, 1000, 'exact' ) );
$this->assertSame(
array(
array( 'action_id' => 7, 'title' => '25 pushups', 'times' => 1 ),
array( 'action_id' => 3, 'title' => 'Eat a bean', 'times' => 5 ),
),
Matcher::match( 2500, $acts )
);
}
public function test_ordering_is_stable_not_resorted(): void {
$acts = array( $this->a( 5, 100, 'every' ), $this->a( 2, 100, 'every' ), $this->a( 9, 100, 'every' ) );
$this->assertSame( array( 5, 2, 9 ), array_column( Matcher::match( 300, $acts ), 'action_id' ) );
}
public function test_bad_rows_are_ignored(): void {
$acts = array(
'junk',
null,
array(),
array( 'id' => 1, 'title' => 'x', 'amount_minor' => 100 ),
array( 'id' => 2, 'title' => 'x', 'amount_minor' => 100, 'mode' => 'weird' ),
array( 'id' => 0, 'title' => 'x', 'amount_minor' => 100, 'mode' => 'every' ),
array( 'id' => 'abc', 'title' => 'x', 'amount_minor' => 100, 'mode' => 'every' ),
array( 'id' => 3, 'title' => 'x', 'amount_minor' => 'lots', 'mode' => 'every' ),
array( 'id' => 4, 'title' => array(), 'amount_minor' => 100, 'mode' => 'every' ),
array( 'id' => '5', 'title' => 'Good', 'amount_minor' => '100', 'mode' => 'every' ),
);
$this->assertSame( array( array( 'action_id' => 5, 'title' => 'Good', 'times' => 2 ) ), Matcher::match( 200, $acts ) );
}
public function test_empty_menu(): void {
$this->assertSame( array(), Matcher::match( 1000, array() ) );
}
}