77 lines
3.5 KiB
PHP
77 lines
3.5 KiB
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\Tests\Unit;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use StreamGoals\Actions\ActionRepository;
|
|
|
|
final class ActionRepositoryTest extends TestCase {
|
|
|
|
private function fake(): object {
|
|
if ( ! defined( 'ARRAY_A' ) ) {
|
|
define( 'ARRAY_A', 'ARRAY_A' );
|
|
}
|
|
return new class() {
|
|
public string $prefix = 'wp_';
|
|
public array $sql = array();
|
|
public array $rows = array();
|
|
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 1; }
|
|
public function get_results( string $q, $o = null ): array { $this->sql[] = $q; return $this->rows; }
|
|
};
|
|
}
|
|
|
|
public function test_record_events_is_insert_ignore(): void {
|
|
$db = $this->fake();
|
|
( new ActionRepository( $db ) )->recordEvents( 10, array( array( 'action_id' => 2, 'title' => 'Eat', 'times' => 5 ), array( 'action_id' => 3, 'title' => 'Push', 'times' => 1 ) ) );
|
|
$this->assertCount( 2, $db->sql );
|
|
foreach ( $db->sql as $q ) {
|
|
$this->assertStringStartsWith( 'INSERT IGNORE INTO wp_sg_action_events', $q );
|
|
}
|
|
$this->assertStringContainsString( "VALUES (10, 2, 5, 'Eat', 'queued'", $db->sql[0] );
|
|
}
|
|
|
|
public function test_record_events_skips_invalid_matches(): void {
|
|
$db = $this->fake();
|
|
$repo = new ActionRepository( $db );
|
|
$repo->recordEvents( 10, array( array( 'action_id' => 0, 'title' => 'x', 'times' => 1 ), array( 'action_id' => 2, 'title' => 'x', 'times' => 0 ) ) );
|
|
$repo->recordEvents( 0, array( array( 'action_id' => 2, 'title' => 'x', 'times' => 1 ) ) );
|
|
$this->assertSame( array(), $db->sql );
|
|
}
|
|
|
|
public function test_enabled_for_goal_filter_and_row_shape(): void {
|
|
$db = $this->fake();
|
|
$db->rows = array( array( 'id' => '4', 'goal_id' => '0', 'title' => 'T', 'description' => 'D', 'amount_minor' => '500', 'mode' => 'every', 'enabled' => '1', 'sort' => '10', 'created_at' => '2026-01-01 00:00:00' ) );
|
|
$rows = ( new ActionRepository( $db ) )->enabledForGoal( 7 );
|
|
$q = $db->sql[0];
|
|
$this->assertStringContainsString( 'enabled = 1', $q );
|
|
$this->assertStringContainsString( 'goal_id IN (0, 7)', $q );
|
|
$this->assertStringContainsString( 'ORDER BY sort ASC, amount_minor ASC', $q );
|
|
$this->assertSame( 4, $rows[0]['id'] );
|
|
$this->assertSame( 500, $rows[0]['amount_minor'] );
|
|
$this->assertSame( array( 'id', 'goal_id', 'title', 'description', 'amount_minor', 'mode', 'enabled', 'sort', 'created_at' ), array_keys( $rows[0] ) );
|
|
}
|
|
|
|
public function test_mark_done_only_touches_queued_rows(): void {
|
|
$db = $this->fake();
|
|
$this->assertTrue( ( new ActionRepository( $db ) )->markDone( 5 ) );
|
|
$this->assertStringContainsString( "AND status = 'queued'", $db->sql[0] );
|
|
}
|
|
|
|
public function test_installer_schema_defines_both_tables_and_unique_key(): void {
|
|
$src = (string) file_get_contents( dirname( __DIR__, 2 ) . '/includes/Core/Installer.php' );
|
|
$this->assertSame( 1, preg_match( "/DB_VERSION = '(\\d+)'/", $src, $m ), 'DB_VERSION constant not found' );
|
|
$this->assertGreaterThanOrEqual( 2, (int) $m[1], 'the actions tables arrived in schema version 2; later versions must keep them' );
|
|
$this->assertStringContainsString( 'CREATE TABLE {$p}actions (', $src );
|
|
$this->assertStringContainsString( 'CREATE TABLE {$p}action_events (', $src );
|
|
$this->assertStringContainsString( 'UNIQUE KEY donation_action (donation_id,action_id)', $src );
|
|
}
|
|
}
|