HelpersTest.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. namespace Luolongfei\Tests\Libs;
  4. use GuzzleHttp\Cookie\SetCookie;
  5. use Luolongfei\Tests\TestCase;
  6. final class HelpersTest extends TestCase
  7. {
  8. public function testLocalizedNumberFormattingAndRandomUserAgent(): void
  9. {
  10. $this->loadFixtureEnv(['CUSTOM_LANGUAGE' => 'en']);
  11. $this->assertSame('1st', get_local_num(1));
  12. $this->assertSame('4th', get_local_num(4));
  13. $this->loadFixtureEnv(['CUSTOM_LANGUAGE' => 'zh']);
  14. $this->assertSame('2', get_local_num(2));
  15. $this->assertSame('121.0.0.0', get_random_user_agent());
  16. }
  17. public function testSleepTimeAndAwsWafCookieHelpers(): void
  18. {
  19. $this->assertSame(20, getSleepTime(1));
  20. $this->assertSame(24, getSleepTime(6, 4, 20));
  21. $cookie = buildAwsWafCookie('token-value');
  22. $this->assertInstanceOf(SetCookie::class, $cookie);
  23. $this->assertSame('aws-waf-token', $cookie->getName());
  24. $this->assertSame('token-value', $cookie->getValue());
  25. $this->assertSame('.my.freenom.com', $cookie->getDomain());
  26. }
  27. public function testGlobalValueAndTaskLockHelpers(): void
  28. {
  29. setGlobalValue('foo', 'bar');
  30. $this->assertTrue(hasGlobalValue('foo'));
  31. $this->assertSame('bar', getGlobalValue('foo'));
  32. delGlobalValue('foo');
  33. $this->assertFalse(hasGlobalValue('foo'));
  34. $taskName = 'phpunit_' . uniqid('', true);
  35. $lockFile = APP_PATH . DS . 'num_limit' . DS . date('Y-m-d') . DS . $taskName . '.lock';
  36. $this->assertFalse(is_locked($taskName));
  37. $this->assertTrue(lock_task($taskName));
  38. $this->assertTrue(is_locked($taskName));
  39. if (file_exists($lockFile)) {
  40. unlink($lockFile);
  41. }
  42. }
  43. }