Skip to content

Commit

Permalink
Fixes all tests and configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Italo Israel Baeza Cabrera committed Sep 5, 2024
1 parent 7ab5cee commit 9fc3d11
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/TokenActionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public function register(): void
$this->mergeConfigFrom(static::CONFIG, 'token-action');

$this->app->singleton(Store::class, static function (Application $app): Store {
/** @var \Illuminate\Contracts\Config\Repository $config */
$config = $app->make('config');

return new Store(
$app->make('cache'),
$config->get('token-action.store'),
$config->get('token-action.default'),
$config->get('token-action.prefix'),
);
});
Expand Down
6 changes: 3 additions & 3 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

class BuilderTest extends TestCase
{
protected Store&Mockery\MockInterface $store;
protected Mockery\MockInterface|Store $store;
protected Builder $builder;

protected function setUp(): void
{
$this->afterApplicationCreated(function () {
$this->store = Mockery::mock(Store::class);
$this->builder = new Builder($this->store);
$this->store = $this->mock(Store::class);
$this->builder = $this->app->make(Builder::class);
$this->freezeSecond();
});

Expand Down
102 changes: 66 additions & 36 deletions tests/StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,59 @@

class StoreTest extends TestCase
{
protected Repository&Mockery\MockInterface $cacheStore;
protected FactoryContract&Mockery\MockInterface $cache;
protected Mockery\MockInterface|Repository $cacheStore;
protected Mockery\MockInterface|FactoryContract $cache;
protected Store $store;

protected function setUp(): void
{
$this->afterApplicationCreated(function () {
$this->cacheStore = Mockery::mock(Repository::class);
$this->cache = Mockery::mock(FactoryContract::class);
$this->store = new Store($this->cache, null, 'test_prefix');
$this->cacheStore = $this->mock(Repository::class);
$this->cache = $this->app->instance('cache', $this->mock(FactoryContract::class));
$this->store = $this->app->make(Store::class);

$this->freezeSecond();
});

parent::setUp();
}

public function test_uses_custom_store(): void
public function test_uses_config_store(): void
{
$this->app->make('config')->set('token-action.default', 'test_store');

$this->cacheStore->expects('getMultiple')
->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn(['token-action|test_id' => null, 'token-action|test_id:tries' => null]);

$this->app->forgetInstance(Store::class);

$this->cache->expects('store')->with('test_store')->andReturn($this->cacheStore);

static::assertNull($this->app->make(Store::class)->get('test_id'));
}

public function test_uses_config_prefix(): void
{
$this->app->make('config')->set('token-action.prefix', 'test_prefix');

$this->cacheStore->expects('getMultiple')
->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
->andReturn(['test_prefix|test_id' => null, 'test_prefix|test_id:tries' => null]);

$this->app->forgetInstance(Store::class);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);

static::assertNull($this->app->make(Store::class)->get('test_id'));
}

public function test_uses_custom_store(): void
{
$this->cacheStore->expects('getMultiple')
->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn(['token-action|test_id' => null, 'token-action|test_id:tries' => null]);

$this->cache->expects('store')->with('test_store')->andReturn($this->cacheStore);

static::assertNull($this->store->store('test_store')->get('test_id'));
Expand All @@ -52,13 +82,13 @@ public function test_get_retrieves_token(): void
{
$this->cacheStore
->expects('getMultiple')
->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => null,
'expires_at' => now()->getTimestamp()
],
'test_prefix|test_id:tries' => 1
'token-action|test_id:tries' => 1
]);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);
Expand All @@ -75,10 +105,10 @@ public function test_get_returns_null_when_token_doesnt_exists(): void
{
$this->cacheStore
->expects('getMultiple')
->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn([
'test_prefix|test_id' => null,
'test_prefix|test_id:tries' => null
'token-action|test_id' => null,
'token-action|test_id:tries' => null
]);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);
Expand All @@ -90,16 +120,16 @@ public function test_get_returns_null_and_destroys_token_when_token_tries_are_ze
{
$this->cacheStore
->expects('getMultiple')
->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => null,
'expires_at' => now()->getTimestamp()
],
'test_prefix|test_id:tries' => 0
'token-action|test_id:tries' => 0
]);

$this->cacheStore->expects('deleteMultiple')->with(['test_prefix|test_id', 'test_prefix|test_id:tries']);
$this->cacheStore->expects('deleteMultiple')->with(['token-action|test_id', 'token-action|test_id:tries']);

$this->cache->expects('store')->with(null)->twice()->andReturn($this->cacheStore);

Expand All @@ -110,16 +140,16 @@ public function test_get_returns_null_and_destroys_token_when_token_tries_are_be
{
$this->cacheStore
->expects('getMultiple')
->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => null,
'expires_at' => now()->getTimestamp()
],
'test_prefix|test_id:tries' => -1
'token-action|test_id:tries' => -1
]);

$this->cacheStore->expects('deleteMultiple')->with(['test_prefix|test_id', 'test_prefix|test_id:tries']);
$this->cacheStore->expects('deleteMultiple')->with(['token-action|test_id', 'token-action|test_id:tries']);

$this->cache->expects('store')->with(null)->twice()->andReturn($this->cacheStore);

Expand All @@ -131,11 +161,11 @@ public function test_puts_token(): void
$token = new Token($this->store, now()->addMinute()->toImmutable(), 'test_id', 10);

$this->cacheStore->expects('setMultiple')->with([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => null,
'expires_at' => $token->expiresAt->getTimestamp()
],
'test_prefix|test_id:tries' => 10
'token-action|test_id:tries' => 10
], Mockery::type(DateInterval::class));

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);
Expand All @@ -148,11 +178,11 @@ public function test_puts_token_with_payload(): void
$token = new Token($this->store, now()->addMinute()->toImmutable(), 'test_id', 10, true);

$this->cacheStore->expects('setMultiple')->with([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => true,
'expires_at' => $token->expiresAt->getTimestamp()
],
'test_prefix|test_id:tries' => 10
'token-action|test_id:tries' => 10
], Mockery::type(DateInterval::class));

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);
Expand All @@ -164,7 +194,7 @@ public function test_destroys_token(): void
{
$token = new Token($this->store, now()->addMinute()->toImmutable(), 'test_id', 10, true);

$this->cacheStore->expects('deleteMultiple')->with(['test_prefix|test_id', 'test_prefix|test_id:tries']);
$this->cacheStore->expects('deleteMultiple')->with(['token-action|test_id', 'token-action|test_id:tries']);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);

Expand All @@ -173,7 +203,7 @@ public function test_destroys_token(): void

public function test_destroys_token_by_its_id(): void
{
$this->cacheStore->expects('deleteMultiple')->with(['test_prefix|test_id', 'test_prefix|test_id:tries']);
$this->cacheStore->expects('deleteMultiple')->with(['token-action|test_id', 'token-action|test_id:tries']);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);

Expand All @@ -182,7 +212,7 @@ public function test_destroys_token_by_its_id(): void

public function test_consumes(): void
{
$this->cacheStore->expects('decrement')->with('test_prefix|test_id:tries', 1);
$this->cacheStore->expects('decrement')->with('token-action|test_id:tries', 1);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);

Expand All @@ -193,7 +223,7 @@ public function test_consumes_by_its_id(): void
{
$token = new Token($this->store, now()->addMinute()->toImmutable(), 'test_id', 10, true);

$this->cacheStore->expects('decrement')->with('test_prefix|test_id:tries', 1);
$this->cacheStore->expects('decrement')->with('token-action|test_id:tries', 1);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);

Expand All @@ -217,7 +247,7 @@ public function test_serializes_model_without_relations(): void

$this->cacheStore->expects('setMultiple')->withArgs(function (array $keys): bool {
/** @var \Illuminate\Contracts\Database\ModelIdentifier $identifier */
$identifier = $keys['test_prefix|test_id']['payload'];
$identifier = $keys['token-action|test_id']['payload'];

static::assertInstanceOf(ModelIdentifier::class, $identifier);
static::assertSame(User::class, $identifier->class);
Expand All @@ -236,13 +266,13 @@ public function test_unserializes_model(): void
{
$identifier = new ModelIdentifier(User::class, 1, [], null, null);

$this->cacheStore->expects('getMultiple')->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
$this->cacheStore->expects('getMultiple')->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => $identifier,
'expires_at' => now()->getTimestamp()
],
'test_prefix|test_id:tries' => 1
'token-action|test_id:tries' => 1
]);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);
Expand Down Expand Up @@ -294,7 +324,7 @@ public function test_serializes_eloquent_collection(): void

$this->cacheStore->expects('setMultiple')->withArgs(function (array $keys): bool {
/** @var \Illuminate\Contracts\Database\ModelIdentifier $identifier */
$identifier = $keys['test_prefix|test_id']['payload'];
$identifier = $keys['token-action|test_id']['payload'];

static::assertInstanceOf(ModelIdentifier::class, $identifier);
static::assertSame(User::class, $identifier->class);
Expand All @@ -313,13 +343,13 @@ public function test_unserializes_collection(): void
{
$identifier = new ModelIdentifier(User::class, [1], [], null, null);

$this->cacheStore->expects('getMultiple')->with(['test_prefix|test_id', 'test_prefix|test_id:tries'])
$this->cacheStore->expects('getMultiple')->with(['token-action|test_id', 'token-action|test_id:tries'])
->andReturn([
'test_prefix|test_id' => [
'token-action|test_id' => [
'payload' => $identifier,
'expires_at' => now()->getTimestamp()
],
'test_prefix|test_id:tries' => 1
'token-action|test_id:tries' => 1
]);

$this->cache->expects('store')->with(null)->andReturn($this->cacheStore);
Expand Down
4 changes: 2 additions & 2 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

class TokenTest extends TestCase
{
protected Store&Mockery\MockInterface $store;
protected Mockery\MockInterface|Store $store;

protected function setUp(): void
{
$this->afterApplicationCreated(function (): void {
$this->store = Mockery::mock(Store::class);
$this->store = $this->mock(Store::class);
});

parent::setUp();
Expand Down

0 comments on commit 9fc3d11

Please sign in to comment.