- 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>
114 lines
5.9 KiB
PHP
114 lines
5.9 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use StreamGoals\Tiers\TierRepository;
|
|
|
|
final class TierRepositoryTest extends TestCase {
|
|
|
|
private function fake( array $rows = array(), $var = null ): object {
|
|
if ( ! defined( 'ARRAY_A' ) ) {
|
|
define( 'ARRAY_A', 'ARRAY_A' );
|
|
}
|
|
return new class( $rows, $var ) {
|
|
public string $prefix = 'wp_';
|
|
public array $sql = array();
|
|
public int $insert_id = 9;
|
|
public array $queryResults = array();
|
|
public function __construct( public array $rows, public $var ) {}
|
|
public function prepare( string $q, ...$args ): string {
|
|
$i = 0;
|
|
return (string) preg_replace_callback( '/%[dsf]/', static function ( $m ) use ( &$i, $args ) {
|
|
$v = $args[ $i++ ];
|
|
return '%s' === $m[0] ? "'" . $v . "'" : (string) $v;
|
|
}, $q );
|
|
}
|
|
public function query( string $q ) { $this->sql[] = $q; return array_shift( $this->queryResults ) ?? 1; }
|
|
public function get_results( string $q, $o = null ): array { $this->sql[] = $q; return $this->rows; }
|
|
public function get_var( string $q ) { $this->sql[] = $q; return $this->var; }
|
|
public function insert( string $t, array $d, array $f ) { $this->sql[] = 'INSERT ' . $t . ' ' . json_encode( $d ); return 1; }
|
|
};
|
|
}
|
|
|
|
private function row( int $id, int $target, ?string $reached = null ): array {
|
|
return array( 'id' => (string) $id, 'goal_id' => '7', 'target_minor' => (string) $target, 'title' => "T$id", 'description' => '', 'reached_at' => $reached, 'reached_donation_id' => null, 'created_at' => '2026-01-01 00:00:00' );
|
|
}
|
|
|
|
public function test_for_goal_orders_by_target_and_casts(): void {
|
|
$db = $this->fake( array( $this->row( 1, 10000 ) ) );
|
|
$rows = ( new TierRepository( $db ) )->forGoal( 7 );
|
|
$this->assertStringContainsString( 'WHERE goal_id = 7 ORDER BY target_minor ASC', $db->sql[0] );
|
|
$this->assertSame( 10000, $rows[0]['target_minor'] );
|
|
$this->assertNull( $rows[0]['reached_at'] );
|
|
}
|
|
|
|
public function test_mark_reached_is_atomic_per_tier_and_only_for_reachable(): void {
|
|
$db = $this->fake( array( $this->row( 1, 10000 ), $this->row( 2, 25000 ), $this->row( 3, 50000 ), $this->row( 4, 5000, '2026-01-01 00:00:00' ) ) );
|
|
$marked = ( new TierRepository( $db ) )->markReached( 7, 30000, 55 );
|
|
$this->assertSame( array( 1, 2 ), $marked );
|
|
$updates = array_values( array_filter( $db->sql, static fn( $q ) => str_starts_with( $q, 'UPDATE' ) ) );
|
|
$this->assertCount( 2, $updates );
|
|
foreach ( $updates as $q ) {
|
|
$this->assertStringContainsString( 'reached_at IS NULL', $q );
|
|
$this->assertStringContainsString( 'reached_donation_id = 55', $q );
|
|
$this->assertStringContainsString( 'UPDATE wp_sg_goal_tiers', $q );
|
|
}
|
|
}
|
|
|
|
public function test_mark_reached_replay_loses_race_and_reports_nothing(): void {
|
|
$db = $this->fake( array( $this->row( 1, 10000 ) ) );
|
|
$db->queryResults = array( 0 ); // someone else already marked it
|
|
$this->assertSame( array(), ( new TierRepository( $db ) )->markReached( 7, 30000, 55 ) );
|
|
}
|
|
|
|
public function test_mark_reached_ignores_invalid_ids(): void {
|
|
$db = $this->fake( array( $this->row( 1, 10000 ) ) );
|
|
$this->assertSame( array(), ( new TierRepository( $db ) )->markReached( 0, 30000, 55 ) );
|
|
$this->assertSame( array(), ( new TierRepository( $db ) )->markReached( 7, 30000, 0 ) );
|
|
$this->assertSame( array(), $db->sql );
|
|
}
|
|
|
|
public function test_reached_by_donation_is_one_query_and_grouped(): void {
|
|
$db = $this->fake( array( array( 'reached_donation_id' => '5', 'title' => 'A' ), array( 'reached_donation_id' => '5', 'title' => 'B' ), array( 'reached_donation_id' => '6', 'title' => 'C' ) ) );
|
|
$out = ( new TierRepository( $db ) )->reachedByDonation( array( 5, 6, 6, 0, -1 ) );
|
|
$this->assertCount( 1, $db->sql );
|
|
$this->assertStringContainsString( 'reached_donation_id IN (5,6)', $db->sql[0] );
|
|
$this->assertSame( array( 5 => array( array( 'title' => 'A' ), array( 'title' => 'B' ) ), 6 => array( array( 'title' => 'C' ) ) ), $out );
|
|
$this->assertSame( array(), ( new TierRepository( $this->fake() ) )->reachedByDonation( array() ) );
|
|
}
|
|
|
|
public function test_sync_goal_target_sets_highest_and_skips_untiered(): void {
|
|
$db = $this->fake( array(), '50000' );
|
|
$this->assertTrue( ( new TierRepository( $db ) )->syncGoalTarget( 7 ) );
|
|
$this->assertStringContainsString( 'MAX(target_minor)', $db->sql[0] );
|
|
$this->assertStringContainsString( 'UPDATE wp_sg_goals SET target_minor = 50000 WHERE id = 7', $db->sql[1] );
|
|
$none = $this->fake( array(), null );
|
|
$this->assertFalse( ( new TierRepository( $none ) )->syncGoalTarget( 7 ) );
|
|
$this->assertCount( 1, $none->sql, 'no UPDATE when the goal has no tiers' );
|
|
}
|
|
|
|
public function test_installer_defines_tier_table_unique_key_and_version(): void {
|
|
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Core/Installer.php' );
|
|
$this->assertSame( 1, preg_match( "/DB_VERSION = '(\\d+)'/", $src, $m ) );
|
|
$this->assertGreaterThanOrEqual( 4, (int) $m[1] );
|
|
$this->assertStringContainsString( 'CREATE TABLE {$p}goal_tiers (', $src );
|
|
$this->assertStringContainsString( 'UNIQUE KEY goal_target (goal_id,target_minor)', $src );
|
|
$this->assertStringContainsString( 'reached_donation_id BIGINT UNSIGNED NULL', $src );
|
|
}
|
|
|
|
public function test_donation_store_marks_tiers_before_close_at_target(): void {
|
|
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/PayPal/DonationStore.php' );
|
|
$start = strpos( $src, 'function notify_applied' );
|
|
$body = substr( $src, $start, strpos( $src, 'function close_goal_if_target_reached' ) - $start );
|
|
$a = strpos( $body, 'Recorder::forDonation' );
|
|
$tier = strpos( $body, 'Tiers\\Recorder::forDonation' );
|
|
$close = strpos( $body, 'self::close_goal_if_target_reached' );
|
|
$this->assertNotFalse( $tier );
|
|
$this->assertNotFalse( $a );
|
|
$this->assertLessThan( $tier, strpos( $body, 'call_engine' ), 'engine first' );
|
|
$this->assertLessThan( $close, $tier, 'tiers before the close check' );
|
|
}
|
|
}
|