82 lines
3.9 KiB
PHP
82 lines
3.9 KiB
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Tests\Unit;
|
||
|
|
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
use StreamGoals\Overlay\OverlayDataSource;
|
||
|
|
use StreamGoals\Overlay\OverlayService;
|
||
|
|
use StreamGoals\Overlay\Payloads;
|
||
|
|
use StreamGoals\Overlay\RecentDonationsSource;
|
||
|
|
|
||
|
|
final class OverlayRecentModeTest extends TestCase {
|
||
|
|
|
||
|
|
/** @param array<int,array<string,mixed>> $db */
|
||
|
|
private function svc( array &$db ): OverlayService {
|
||
|
|
return new OverlayService( new class( $db ) implements OverlayDataSource, RecentDonationsSource {
|
||
|
|
public function __construct( private array &$db ) {}
|
||
|
|
public function activeGoal(): ?array { return null; }
|
||
|
|
public function raisedMinor( array $goal ): int { return 0; }
|
||
|
|
public function stateValue( int $goalId, string $key ): ?string { return null; }
|
||
|
|
public function counter( int $goalId ): array { return array( 'label' => '', 'value' => 0 ); }
|
||
|
|
public function bonusesFor( array $ids ): array { return array_fill_keys( $ids, array( 'big_donation' ) ); }
|
||
|
|
private function ok(): array {
|
||
|
|
$r = array_filter( $this->db, static fn( $d ) => 'completed' === $d['status'] && 'approved' === $d['moderation'] );
|
||
|
|
usort( $r, static fn( $a, $b ) => $a['id'] <=> $b['id'] );
|
||
|
|
return $r;
|
||
|
|
}
|
||
|
|
public function donations( ?int $sinceId, int $limit ): array {
|
||
|
|
return array_slice( array_values( array_filter( $this->ok(), static fn( $d ) => $d['id'] > (int) $sinceId ) ), 0, $limit );
|
||
|
|
}
|
||
|
|
public function recentDonations( int $limit ): array { return array_slice( $this->ok(), -$limit ); }
|
||
|
|
} );
|
||
|
|
}
|
||
|
|
|
||
|
|
private function row( int $id, array $o = array() ): array {
|
||
|
|
return $o + array( 'id' => $id, 'base_minor' => 500, 'currency' => 'USD', 'donor_name' => 'Al', 'is_anonymous' => 0,
|
||
|
|
'message' => '', 'status' => 'completed', 'moderation' => 'approved' );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_recent_returns_approved_ascending_masked_with_bonuses(): void {
|
||
|
|
$db = array( $this->row( 3 ), $this->row( 1, array( 'is_anonymous' => 1, 'donor_name' => 'Secret' ) ),
|
||
|
|
$this->row( 2, array( 'moderation' => 'pending' ) ), $this->row( 4, array( 'moderation' => 'rejected' ) ) );
|
||
|
|
$out = $this->svc( $db )->payload( 'alerts', 1, null, true );
|
||
|
|
$this->assertSame( array( 1, 3 ), array_column( $out['alerts'], 'id' ) );
|
||
|
|
$this->assertSame( 3, $out['last_id'] );
|
||
|
|
$this->assertSame( 'Anonymous', $out['alerts'][0]['name'] );
|
||
|
|
$this->assertStringNotContainsString( 'Secret', json_encode( $out ) );
|
||
|
|
$this->assertSame( array( 'Big donation' ), $out['alerts'][1]['bonuses'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_recent_caps_at_newest_20(): void {
|
||
|
|
$db = array();
|
||
|
|
for ( $i = 1; $i <= 25; $i++ ) {
|
||
|
|
$db[] = $this->row( $i );
|
||
|
|
}
|
||
|
|
$out = $this->svc( $db )->payload( 'alerts', 1, null, true );
|
||
|
|
$this->assertCount( 20, $out['alerts'] );
|
||
|
|
$this->assertSame( 6, $out['alerts'][0]['id'] );
|
||
|
|
$this->assertSame( 25, $out['alerts'][19]['id'] );
|
||
|
|
$this->assertCount( 20, Payloads::alerts( $db, null, 20, true )['alerts'] );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_late_approval_appears_in_recent_but_not_since(): void {
|
||
|
|
$db = array( $this->row( 1 ), $this->row( 2, array( 'moderation' => 'pending' ) ), $this->row( 3 ) );
|
||
|
|
$svc = $this->svc( $db );
|
||
|
|
$first = $svc->payload( 'alerts', 1, null, true );
|
||
|
|
$this->assertSame( array( 1, 3 ), array_column( $first['alerts'], 'id' ) );
|
||
|
|
$db[1]['moderation'] = 'approved';
|
||
|
|
$this->assertSame( array(), $svc->payload( 'alerts', 1, $first['last_id'] )['alerts'] ); // old cursor misses it
|
||
|
|
$next = $svc->payload( 'alerts', 1, null, true );
|
||
|
|
$this->assertSame( array( 1, 2, 3 ), array_column( $next['alerts'], 'id' ) );
|
||
|
|
}
|
||
|
|
|
||
|
|
public function test_recent_etag_stable_then_changes(): void {
|
||
|
|
$db = array( $this->row( 1 ), $this->row( 2, array( 'moderation' => 'pending' ) ) );
|
||
|
|
$s = $this->svc( $db );
|
||
|
|
$a = Payloads::etag( $s->payload( 'alerts', 1, null, true ) );
|
||
|
|
$this->assertSame( $a, Payloads::etag( $s->payload( 'alerts', 99, null, true ) ) );
|
||
|
|
$db[1]['moderation'] = 'approved';
|
||
|
|
$this->assertNotSame( $a, Payloads::etag( $s->payload( 'alerts', 1, null, true ) ) );
|
||
|
|
}
|
||
|
|
}
|