4
0

StatsDAOTest.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\TestCase;
  4. final class StatsDAOTest extends TestCase {
  5. /**
  6. * @return array{0:FreshRSS_StatsDAOSQLite,1:Minz_PdoSqlite}
  7. */
  8. private static function createSQLiteDao(): array {
  9. $pdo = new Minz_PdoSqlite('sqlite::memory:');
  10. $pdo->setPrefix('');
  11. $pdo->exec('CREATE TABLE entry (id INTEGER PRIMARY KEY, date INTEGER)');
  12. $dao = new FreshRSS_StatsDAOSQLite('test', $pdo);
  13. return [$dao, $pdo];
  14. }
  15. public static function testCalculateEntryCountIncludesTodayWithSQLite(): void {
  16. [$dao, $pdo] = self::createSQLiteDao();
  17. $midnight = mktime(0, 0, 0) ?: 0;
  18. for ($day = 0; $day <= 30; $day++) {
  19. $pdo->exec('INSERT INTO entry(date) VALUES (' . ($midnight - $day * 86400 + 12 * 3600) . ')');
  20. }
  21. self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount());
  22. }
  23. public static function testCalculateDayOfWeekRepartitionWithSQLite(): void {
  24. [$dao, $pdo] = self::createSQLiteDao();
  25. $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday
  26. $dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek();
  27. self::assertCount(7, $dayOfWeek);
  28. self::assertSame(1, $dayOfWeek[0]);
  29. self::assertSame(1, array_sum($dayOfWeek));
  30. }
  31. public static function testCalculateMonthRepartitionWithSQLite(): void {
  32. [$dao, $pdo] = self::createSQLiteDao();
  33. $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 1, 15, 2024) . ')');
  34. $pdo->exec('INSERT INTO entry(date) VALUES (' . mktime(12, 0, 0, 12, 15, 2024) . ')');
  35. $months = $dao->calculateEntryRepartitionPerFeedPerMonth();
  36. self::assertCount(12, $months);
  37. self::assertSame(1, $months[0]); // January
  38. self::assertSame(1, $months[11]); // December
  39. self::assertSame(2, array_sum($months));
  40. }
  41. public static function testPostgreSQLRepartitions(): void {
  42. $dsn = getenv('FRESHRSS_TEST_PGSQL_DSN');
  43. if ($dsn === false || $dsn === '') {
  44. self::markTestSkipped('FRESHRSS_TEST_PGSQL_DSN is not configured.');
  45. }
  46. $pdo = new Minz_PdoPgsql($dsn, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
  47. $prefix = 'stats_test_' . getmypid() . '_';
  48. $pdo->setPrefix($prefix);
  49. $pdo->exec("CREATE TABLE {$prefix}entry (id BIGSERIAL PRIMARY KEY, date BIGINT)");
  50. try {
  51. $dao = new FreshRSS_StatsDAOPGSQL('test', $pdo);
  52. $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 1, 7, 2024) . ')'); // 2024-01-07 is a Sunday
  53. $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . mktime(12, 0, 0, 12, 15, 2024) . ')');
  54. $dayOfWeek = $dao->calculateEntryRepartitionPerFeedPerDayOfWeek();
  55. self::assertCount(7, $dayOfWeek);
  56. self::assertSame(2, $dayOfWeek[0]); // Both 2024-01-07 and 2024-12-15 are Sundays
  57. self::assertSame(2, array_sum($dayOfWeek));
  58. $months = $dao->calculateEntryRepartitionPerFeedPerMonth();
  59. self::assertCount(12, $months);
  60. self::assertSame(1, $months[0]); // January
  61. self::assertSame(1, $months[11]); // December
  62. $midnight = mktime(0, 0, 0) ?: 0;
  63. for ($day = 0; $day <= 30; $day++) {
  64. $pdo->exec("INSERT INTO {$prefix}entry(date) VALUES (" . ($midnight - $day * 86400 + 12 * 3600) . ')');
  65. }
  66. self::assertSame(array_fill(0, 31, 1), $dao->calculateEntryCount());
  67. } finally {
  68. $pdo->exec("DROP TABLE {$prefix}entry");
  69. }
  70. }
  71. public static function testCalculateEntryAverageFromRepartition(): void {
  72. self::assertSame(2.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([0, 2, 4]));
  73. }
  74. public static function testCalculateEntryAverageFromEmptyRepartition(): void {
  75. self::assertSame(0.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition([]));
  76. }
  77. public static function testCalculateEntryAverageFromZeroRepartition(): void {
  78. self::assertSame(0.0, FreshRSS_StatsDAO::calculateEntryAverageFromRepartition(array_fill(0, 24, 0)));
  79. }
  80. }