Files
wp-donations/tests/Unit/TierPolicyTest.php
T
shadowdaoandClaude Sonnet 5.5 4c379a0245
Test and Release / test (push) Successful in 1m21s
Test and Release / release (push) Successful in 4s
Tiered goals: ordered milestones with unlocks, tier-aware goal bar, TIER UNLOCKED alerts, next-unlock line
- Tiers\TierPolicy (pure), TierRepository (atomic reached marking), Tiers\Recorder hooked before close-at-target
- Admin tiers screen; goal target derived from the final tier
- Overlay goal widget shows Tier N of M and fills within the tier; alert pill; donate form 'Next unlock'
- Installer DB_VERSION 4 (sg_goal_tiers)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 13:09:18 -08:00

143 lines
7.4 KiB
PHP

<?php
declare( strict_types=1 );
namespace StreamGoals\Tests\Unit;
use PHPUnit\Framework\TestCase;
use StreamGoals\Tiers\TierPolicy;
final class TierPolicyTest extends TestCase {
private function tiers( array $reachedIds = array() ): array {
$rows = array( array( 1, 10000, 'Giveaway' ), array( 2, 25000, 'Microphone' ), array( 3, 50000, '24h stream' ) );
$out = array();
foreach ( $rows as [ $id, $t, $title ] ) {
$out[] = array( 'id' => $id, 'target_minor' => $t, 'title' => $title, 'description' => '', 'reached_at' => in_array( $id, $reachedIds, true ) ? '2026-01-01 00:00:00' : null );
}
return array_reverse( $out ); // unsorted on purpose
}
public function test_current_tier_and_index(): void {
$this->assertSame( 1, TierPolicy::currentTier( $this->tiers() )['id'] );
$this->assertSame( 1, TierPolicy::currentIndex( $this->tiers() ) );
$this->assertSame( 3, TierPolicy::currentTier( $this->tiers( array( 1, 2 ) ) )['id'] );
$this->assertSame( 3, TierPolicy::currentIndex( $this->tiers( array( 1, 2 ) ) ) );
$this->assertNull( TierPolicy::currentTier( $this->tiers( array( 1, 2, 3 ) ) ) );
$this->assertSame( 3, TierPolicy::currentIndex( $this->tiers( array( 1, 2, 3 ) ) ) );
$this->assertTrue( TierPolicy::allReached( $this->tiers( array( 1, 2, 3 ) ) ) );
$this->assertFalse( TierPolicy::allReached( $this->tiers( array( 1 ) ) ) );
$this->assertFalse( TierPolicy::allReached( array() ) );
$this->assertSame( 0, TierPolicy::currentIndex( array() ) );
}
public function test_floor(): void {
$this->assertSame( 0, TierPolicy::floorMinor( $this->tiers() ) );
$this->assertSame( 10000, TierPolicy::floorMinor( $this->tiers( array( 1 ) ) ) );
$this->assertSame( 25000, TierPolicy::floorMinor( $this->tiers( array( 1, 2 ) ) ) );
$this->assertSame( 50000, TierPolicy::floorMinor( $this->tiers( array( 1, 2, 3 ) ) ), 'all reached: top target' );
}
public function test_percent_within_tier_clamped(): void {
$this->assertSame( 0, TierPolicy::percent( 0, 0, 10000 ) );
$this->assertSame( 50, TierPolicy::percent( 5000, 0, 10000 ) );
$this->assertSame( 50, TierPolicy::percent( 17500, 10000, 25000 ) );
$this->assertSame( 0, TierPolicy::percent( 10000, 10000, 25000 ), 'exactly at the floor' );
$this->assertSame( 0, TierPolicy::percent( 500, 10000, 25000 ), 'raised below the floor clamps to 0' );
$this->assertSame( 100, TierPolicy::percent( 25000, 10000, 25000 ), 'exactly at the target' );
$this->assertSame( 100, TierPolicy::percent( 99999, 10000, 25000 ), 'above clamps to 100' );
$this->assertSame( 0, TierPolicy::percent( 5, 100, 100 ), 'degenerate range, not reached' );
$this->assertSame( 100, TierPolicy::percent( 100, 100, 100 ), 'degenerate range, reached' );
}
public function test_percent_for_walks_the_ladder(): void {
$this->assertSame( 25, TierPolicy::percentFor( $this->tiers(), 2500 ) );
$this->assertSame( 0, TierPolicy::percentFor( $this->tiers( array( 1 ) ), 10000 ) );
$this->assertSame( 50, TierPolicy::percentFor( $this->tiers( array( 1 ) ), 17500 ) );
$this->assertSame( 100, TierPolicy::percentFor( $this->tiers( array( 1, 2, 3 ) ), 60000 ) );
$this->assertSame( 0, TierPolicy::percentFor( array(), 500 ) );
$this->assertSame( 7500, TierPolicy::remainingMinor( $this->tiers(), 2500 ) );
$this->assertSame( 0, TierPolicy::remainingMinor( $this->tiers( array( 1, 2, 3 ) ), 60000 ) );
}
public function test_newly_reached_several_in_one_donation_in_order(): void {
$ids = array_column( TierPolicy::newlyReached( $this->tiers(), 30000 ), 'id' );
$this->assertSame( array( 1, 2 ), $ids );
$this->assertSame( array( 1, 2, 3 ), array_column( TierPolicy::newlyReached( $this->tiers(), 50000 ), 'id' ), 'exactly at the last target counts' );
$this->assertSame( array(), TierPolicy::newlyReached( $this->tiers(), 9999 ) );
$this->assertSame( array( 2 ), array_column( TierPolicy::newlyReached( $this->tiers( array( 1 ) ), 30000 ), 'id' ), 'reached ones are skipped' );
}
public function test_replay_is_idempotent_at_policy_level(): void {
$tiers = $this->tiers();
$first = TierPolicy::newlyReached( $tiers, 30000 );
foreach ( $tiers as &$t ) {
if ( in_array( $t['id'], array_column( $first, 'id' ), true ) ) {
$t['reached_at'] = '2026-02-02 00:00:00';
}
}
unset( $t );
$this->assertSame( array(), TierPolicy::newlyReached( $tiers, 30000 ) );
$this->assertSame( array(), TierPolicy::newlyReached( $tiers, 12000 ), 'a lower total (refund) never un-reaches or re-reaches' );
}
public function test_derived_target_is_highest(): void {
$this->assertSame( 50000, TierPolicy::derivedGoalTarget( $this->tiers() ) );
$this->assertSame( 0, TierPolicy::derivedGoalTarget( array() ) );
}
public function test_status(): void {
$t = $this->tiers( array( 1 ) );
$by = array();
foreach ( $t as $row ) {
$by[ $row['id'] ] = TierPolicy::status( $t, $row );
}
$this->assertSame( array( 3 => 'upcoming', 2 => 'current', 1 => 'reached' ), $by );
}
/* ------------------------------------------------------------ validation */
private function v( ?int $target, string $title = 'Mic', string $desc = '', ?int $edit = null, int $raised = 0, ?array $tiers = null ): array {
return TierPolicy::validate( $tiers ?? $this->tiers( array( 1 ) ), $edit, $target, $title, $desc, $raised, 100, '$1.00' );
}
public function test_valid_new_tier(): void {
$r = $this->v( 70000, ' New desk ', ' nice ', null, 12000 );
$this->assertNull( $r['error'] );
$this->assertSame( array( 'target_minor' => 70000, 'title' => 'New desk', 'description' => 'nice' ), $r['data'] );
}
public function test_rejects_bad_values(): void {
$this->assertNotNull( $this->v( null )['error'] );
$this->assertNotNull( $this->v( 0 )['error'] );
$this->assertStringContainsString( 'minimum', (string) $this->v( 50 )['error'] );
$this->assertStringContainsString( 'title', (string) $this->v( 70000, ' ' )['error'] . ' title' );
$this->assertNotNull( $this->v( 70000, ' ' )['error'] );
$this->assertNotNull( $this->v( 70000, str_repeat( 'a', 121 ) )['error'] );
$this->assertNull( $this->v( 70000, str_repeat( 'a', 120 ) )['error'] );
$this->assertNotNull( $this->v( 70000, 'x', str_repeat( 'd', 256 ) )['error'] );
$this->assertNull( $this->v( 70000, 'x', str_repeat( 'd', 255 ) )['error'] );
}
public function test_target_must_be_unique_and_above_raised(): void {
$this->assertStringContainsString( 'already', (string) $this->v( 25000 )['error'] );
$this->assertStringContainsString( 'already raised', (string) $this->v( 20000, 'x', '', null, 20000 )['error'], 'equal to raised unlocks instantly' );
$this->assertNull( $this->v( 20001, 'x', '', null, 20000 )['error'] );
}
public function test_reached_tier_target_is_locked_but_text_editable(): void {
$r = $this->v( 999999, 'Renamed', 'd', 1, 12000 );
$this->assertNull( $r['error'] );
$this->assertSame( 10000, $r['data']['target_minor'] );
$this->assertSame( 'Renamed', $r['data']['title'] );
$r2 = $this->v( null, 'Renamed', '', 1, 12000 );
$this->assertSame( 10000, $r2['data']['target_minor'] );
}
public function test_edit_unreached_keeps_or_moves_target(): void {
$this->assertNull( $this->v( 25000, 'Mic2', '', 2, 12000 )['error'], 'unchanged target is fine' );
$this->assertNull( $this->v( 30000, 'Mic', '', 2, 12000 )['error'] );
$this->assertStringContainsString( 'already', (string) $this->v( 50000, 'Mic', '', 2, 12000 )['error'] );
$this->assertStringContainsString( 'no longer', (string) $this->v( 30000, 'Mic', '', 99, 12000 )['error'] );
}
}