SnowflakeTest.php 832 B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace Kra8\Snowflake\Test;
  3. use Kra8\Snowflake\Snowflake;
  4. class SnowflakeTest extends AbstractTestCase
  5. {
  6. public function testNextId()
  7. {
  8. $now = strtotime(date('Y-m-d H:i:s'));
  9. $epoch = strtotime(config('snowflake.epoch')) * 1000;
  10. $id = app(Snowflake::class)->next();
  11. $timestamp = $id >> 22;
  12. $timestamp = (int) round(($timestamp + $epoch) / 1000);
  13. $this->assertTrue($timestamp - $now < 3);
  14. }
  15. public function testShortId()
  16. {
  17. $now = strtotime(date('Y-m-d H:i:s'));
  18. $epoch = strtotime(config('snowflake.epoch')) * 1000;
  19. $id = app(Snowflake::class)->short();
  20. $timestamp = $id >> 12;
  21. $timestamp = (int) round(($timestamp + $epoch) / 1000);
  22. $this->assertTrue($timestamp - $now < 3);
  23. }
  24. }