-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
d90ef51
commit 7c66a9d
Showing
6 changed files
with
219 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace DirectoryTree\ActiveRedis\Tests\Fixtures; | ||
|
||
use DirectoryTree\ActiveRedis\Model; | ||
|
||
class ModelStubWithQueryable extends Model | ||
{ | ||
protected array $queryable = ['user_id', 'company_id']; | ||
} |
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 | ||
|
||
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithQueryable; | ||
use Illuminate\Support\Facades\Redis; | ||
|
||
beforeEach(fn () => Redis::flushall()); | ||
|
||
it('generates model hash with queryable attributes when present', function () { | ||
$hash = (new ModelStubWithQueryable([ | ||
'user_id' => 1, | ||
'company_id' => 2, | ||
]))->getHashKey(); | ||
|
||
expect($hash)->toBe('model_stub_with_queryables:id:null:company_id:2:user_id:1'); | ||
}); | ||
|
||
it('generates model hash with null queryable attributes when missing', function () { | ||
$hash = (new ModelStubWithQueryable)->getHashKey(); | ||
|
||
expect($hash)->toBe('model_stub_with_queryables:id:null:company_id:null:user_id:null'); | ||
}); | ||
|
||
it('creates model with null queryable values when missing', function () { | ||
$model = ModelStubWithQueryable::create(); | ||
|
||
expect($model->getHashKey())->toBe( | ||
"model_stub_with_queryables:id:{$model->getKey()}:company_id:null:user_id:null" | ||
); | ||
|
||
expect(ModelStubWithQueryable::first()->is($model))->toBeTrue(); | ||
}); | ||
|
||
it('creates model with queryable values when present', function () { | ||
$model = ModelStubWithQueryable::create([ | ||
'user_id' => 1, | ||
'company_id' => 2, | ||
]); | ||
|
||
expect($model->getHashKey())->toBe( | ||
"model_stub_with_queryables:id:{$model->getKey()}:company_id:2:user_id:1" | ||
); | ||
|
||
expect(ModelStubWithQueryable::first()->is($model))->toBeTrue(); | ||
}); | ||
|
||
it('can query for queryable attributes', function () { | ||
ModelStubWithQueryable::create([ | ||
'user_id' => 1, | ||
'company_id' => 2, | ||
]); | ||
|
||
ModelStubWithQueryable::create([ | ||
'user_id' => 1, | ||
'company_id' => 2, | ||
]); | ||
|
||
expect(ModelStubWithQueryable::exists())->toBeTrue(); | ||
|
||
$models = ModelStubWithQueryable::query() | ||
->where('user_id', 1) | ||
->where('company_id', 2) | ||
->get(); | ||
|
||
expect($models->count())->toBe(2); | ||
}); |
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,38 @@ | ||
<?php | ||
|
||
use DirectoryTree\ActiveRedis\AttributeNotQueryableException; | ||
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub; | ||
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomKey; | ||
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithQueryable; | ||
|
||
it('generates query using where clauses', function () { | ||
$query = query(new ModelStubWithQueryable); | ||
|
||
expect($query->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:*:user_id:*'); | ||
|
||
$query->where('user_id', '1'); | ||
|
||
expect($query->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:*:user_id:1'); | ||
|
||
$query->where('company_id', '2'); | ||
|
||
expect($query->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:2:user_id:1'); | ||
}); | ||
|
||
it('throws exception with where clause for attribute that is not queryable', function () { | ||
$query = query(new ModelStub); | ||
|
||
$query->where('foo', '1'); | ||
})->throws(AttributeNotQueryableException::class, 'The attribute [foo] is not queryable on the model [DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub].'); | ||
|
||
it('generates query using model prefix', function () { | ||
expect(query(new ModelStub)->getQuery())->toBe('model_stubs:id:*'); | ||
}); | ||
|
||
it('generates query using custom key', function () { | ||
expect(query(new ModelStubWithCustomKey)->getQuery())->toBe('model_stub_with_custom_keys:custom:*'); | ||
}); | ||
|
||
it('generates query using queryable', function () { | ||
expect(query(new ModelStubWithQueryable)->getQuery())->toBe('model_stub_with_queryables:id:*:company_id:*:user_id:*'); | ||
}); |
This file was deleted.
Oops, something went wrong.