|
@@ -4,6 +4,80 @@ use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
final class StatsDAOTest extends TestCase {
|
|
final class StatsDAOTest extends TestCase {
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @return array{0:FreshRSS_StatsDAOSQLite,1:Minz_PdoSqlite}
|
|
|
|
|
+ */
|
|
|
|
|
+ private static function createSQLiteDao(): array {
|
|
|
|
|
+ $pdo = new Minz_PdoSqlite('sqlite::memory:');
|
|
|
|
|
+ $pdo->setPrefix('');
|
|
|
|
|
+ $pdo->exec('CREATE TABLE entry (id INTEGER PRIMARY KEY, date INTEGER)');
|
|
|
|
|
+ $dao = new FreshRSS_StatsDAOSQLite('test', $pdo);
|
|
|
|
|
+ return [$dao, $pdo];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function testCalculateEntryCountIncludesTodayWithSQLite(): void {
|
|
|
|
|
+ [$dao, $pdo] = self::createSQLiteDao();
|
|
|
|
|
+ $midnight = mktime(0, 0, 0) ?: 0;
|
|
|
|
|
+ for ($day = 0; $day <= 30; $day++) {
|
|
|
|
|
+ $pdo->exec('INSERT INTO entry(date) VALUES (' . ($midnight - $day * 86400 + 12 * 3600) . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function testCalculateDayOfWeekRepartitionWithSQLite(): void {
|
|
|
|
|
+ [$dao, $pdo] = self::createSQLiteDao();
|
|
|
|
|
+ $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday
|
|
|
|
|
+ $dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek();
|
|
|
|
|
+ self::assertCount(7, $dayOfWeek);
|
|
|
|
|
+ self::assertSame(1, $dayOfWeek[0]);
|
|
|
|
|
+ self::assertSame(1, array_sum($dayOfWeek));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function testCalculateMonthRepartitionWithSQLite(): void {
|
|
|
|
|
+ [$dao, $pdo] = self::createSQLiteDao();
|
|
|
|
|
+ $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 15, 2024) . ')');
|
|
|
|
|
+ $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 12, 15, 2024) . ')');
|
|
|
|
|
+ $months = $dao->calculateEntryRepartitionPerFeedPerMonth();
|
|
|
|
|
+ self::assertCount(12, $months);
|
|
|
|
|
+ self::assertSame(1, $months[0]); // January
|
|
|
|
|
+ self::assertSame(1, $months[11]); // December
|
|
|
|
|
+ self::assertSame(2, array_sum($months));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function testPostgreSQLRepartitions(): void {
|
|
|
|
|
+ $dsn = getenv('FRESHRSS_TEST_PGSQL_DSN');
|
|
|
|
|
+ if ($dsn === false || $dsn === '') {
|
|
|
|
|
+ self::markTestSkipped('FRESHRSS_TEST_PGSQL_DSN is not configured.');
|
|
|
|
|
+ }
|
|
|
|
|
+ $pdo = new Minz_PdoPgsql($dsn, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
|
|
|
+ $prefix = 'stats_test_' . getmypid() . '_';
|
|
|
|
|
+ $pdo->setPrefix($prefix);
|
|
|
|
|
+ $pdo->exec("CREATE TABLE {$prefix}entry (id BIGSERIAL PRIMARY KEY, date BIGINT)");
|
|
|
|
|
+ try {
|
|
|
|
|
+ $dao = new FreshRSS_StatsDAOPGSQL('test', $pdo);
|
|
|
|
|
+ $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday
|
|
|
|
|
+ $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 12, 15, 2024) . ')');
|
|
|
|
|
+
|
|
|
|
|
+ $dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek();
|
|
|
|
|
+ self::assertCount(7, $dayOfWeek);
|
|
|
|
|
+ self::assertSame(2, $dayOfWeek[0]); // Both 2024-01-07 and 2024-12-15 are Sundays
|
|
|
|
|
+ self::assertSame(2, array_sum($dayOfWeek));
|
|
|
|
|
+
|
|
|
|
|
+ $months = $dao->calculateEntryRepartitionPerFeedPerMonth();
|
|
|
|
|
+ self::assertCount(12, $months);
|
|
|
|
|
+ self::assertSame(1, $months[0]); // January
|
|
|
|
|
+ self::assertSame(1, $months[11]); // December
|
|
|
|
|
+
|
|
|
|
|
+ $midnight = mktime(0, 0, 0) ?: 0;
|
|
|
|
|
+ for ($day = 0; $day <= 30; $day++) {
|
|
|
|
|
+ $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . ($midnight - $day * 86400 + 12 * 3600) . ')');
|
|
|
|
|
+ }
|
|
|
|
|
+ self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount());
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ $pdo->exec("DROP TABLE {$prefix}entry");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static function testCalculateEntryAverageFromRepartition(): void {
|
|
public static function testCalculateEntryAverageFromRepartition(): void {
|
|
|
self::assertSame(2.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([0, 2, 4]));
|
|
self::assertSame(2.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([0, 2, 4]));
|
|
|
}
|
|
}
|