generated from glhd/laravel-package-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Glhd\Bits\Tests\Unit; | ||
|
||
use Glhd\Bits\IdResolvers\CacheResolver; | ||
use Glhd\Bits\Tests\TestCase; | ||
use Illuminate\Cache\ArrayStore; | ||
use Illuminate\Support\DateFactory; | ||
use Illuminate\Support\Facades\Date; | ||
use RuntimeException; | ||
|
||
class CacheIdResolverTest extends TestCase | ||
{ | ||
public function test_it_increments_sequences(): void | ||
{ | ||
Date::setTestNow(now()); | ||
|
||
$store = new ArrayStore(); | ||
|
||
// Should get 0b00 | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$this->assertEquals(0b00, $resolver->get(1, 1)->first()); | ||
$this->assertEquals(0b00, $resolver->get(1, 1)->second()); | ||
|
||
Date::setTestNow(now()->addMinutes(10)); | ||
|
||
// Should get 0b01 | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$this->assertEquals(0b00, $resolver->get(1, 1)->first()); | ||
$this->assertEquals(0b01, $resolver->get(1, 1)->second()); | ||
|
||
Date::setTestNow(now()->addMinutes(10)); | ||
|
||
// Should get 0b10 | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$this->assertEquals(0b01, $resolver->get(1, 1)->first()); | ||
$this->assertEquals(0b00, $resolver->get(1, 1)->second()); | ||
|
||
Date::setTestNow(now()->addMinutes(10)); | ||
|
||
// Should get 0b11 | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$this->assertEquals(0b01, $resolver->get(1, 1)->first()); | ||
$this->assertEquals(0b01, $resolver->get(1, 1)->second()); | ||
|
||
// Now we've run out of IDs… should throw | ||
$this->assertThrows(function() use ($store) { | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$resolver->get(1, 1); | ||
}, RuntimeException::class); | ||
|
||
Date::setTestNow(now()->addMinutes(30)); | ||
|
||
// 0b00 has not expired, so we should be able to acquire it | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$this->assertEquals(0b00, $resolver->get(1, 1)->first()); | ||
$this->assertEquals(0b00, $resolver->get(1, 1)->second()); | ||
|
||
// But everything else is locked, so we should throw again | ||
$this->assertThrows(function() use ($store) { | ||
$resolver = new CacheResolver($store, app(DateFactory::class), 0b11); | ||
$resolver->get(1, 1); | ||
}, RuntimeException::class); | ||
} | ||
} |