dd/tests/Unit/Freenode/ArticleCountTest.php

55 lines
2.1 KiB
PHP

<?php
namespace Tests\Unit\Freenode;
use App\Services\CountrySpeedService;
use Illuminate\Config\Repository;
use Illuminate\Foundation\Application;
use PHPUnit\Framework\TestCase;
final class ArticleCountTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir().'/article-count-'.bin2hex(random_bytes(6));
mkdir($this->dir.'/app/freenode-v2', 0700, true);
$app = new Application(dirname(__DIR__, 3));
$app->useStoragePath($this->dir);
$app->instance('config', new Repository(['freenode' => ['engine' => 'v2']]));
}
protected function tearDown(): void
{
foreach (glob($this->dir.'/app/freenode-v2/*') as $p) unlink($p);
rmdir($this->dir.'/app/freenode-v2'); rmdir($this->dir.'/app'); rmdir($this->dir);
}
private function write(array $override = [], string $name = 'current'): void
{
$m = array_replace(['id'=>'published', 'validated'=>true, 'date'=>date('Y-m-d'),
'created_at'=>time(), 'counts'=>['v2ray'=>2592]], $override);
file_put_contents($this->dir.'/app/freenode-v2/'.$name.'.json', json_encode($m));
}
public function testV2ReadsPublishedCountWithoutLegacyDatabase(): void
{
$this->write(); $this->write(['counts'=>['v2ray'=>9999]], 'pending');
self::assertSame(2592, CountrySpeedService::nodeCount());
}
public function testPendingAloneCannotProduceAnArticleCount(): void
{
$this->write([], 'pending');
$this->expectExceptionMessage('no_today_published_release_for_article');
CountrySpeedService::nodeCount();
}
public function testPreviousDayIsRejectedInsteadOfPublishingZero(): void
{
$this->write(['date'=>date('Y-m-d', time()-86400)]);
$this->expectExceptionMessage('no_today_published_release_for_article');
CountrySpeedService::nodeCount();
}
public function testZeroCountIsRejected(): void
{
$this->write(['counts'=>['v2ray'=>0]]);
$this->expectExceptionMessage('invalid_published_node_count');
CountrySpeedService::nodeCount();
}
}