-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide trait to query flagged enums with Eloquent
- Loading branch information
Showing
6 changed files
with
117 additions
and
0 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
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,30 @@ | ||
<?php | ||
|
||
namespace BenSampo\Enum\Traits; | ||
|
||
use BenSampo\Enum\FlaggedEnum; | ||
|
||
trait FlaggedEnumQueries | ||
{ | ||
/** | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $column | ||
* @param int $flag | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeHasFlag($query, $column, $flag) | ||
{ | ||
return $query->whereRaw("$column & ? > 0", [$flag]); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Database\Eloquent\Builder $query | ||
* @param string $column | ||
* @param int $flag | ||
* @return \Illuminate\Database\Eloquent\Builder | ||
*/ | ||
public function scopeNotHasFlag($query, $column, $flag) | ||
{ | ||
return $query->whereRaw("not $column & ? > 0", [$flag]); | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
namespace BenSampo\Enum\Tests; | ||
|
||
use BenSampo\Enum\Tests\Enums\SuperPowers; | ||
use BenSampo\Enum\Tests\Models\ModelWithFlaggedQueries; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Foundation\Application; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class FlaggedEnumQueriesTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpDatabase($this->app); | ||
} | ||
|
||
protected function setUpDatabase(Application $app) | ||
{ | ||
$app['db']->connection()->getSchemaBuilder()->create('test_models', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->integer('superpowers'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_constrain_query_by_flagged_enum() | ||
{ | ||
ModelWithFlaggedQueries::create([ | ||
'superpowers' => SuperPowers::flags([SuperPowers::Flight, SuperPowers::Immortality]) | ||
]); | ||
|
||
ModelWithFlaggedQueries::create([ | ||
'superpowers' => SuperPowers::flags([SuperPowers::Strength, SuperPowers::Immortality]) | ||
]); | ||
|
||
$this->assertEquals(2, ModelWithFlaggedQueries::query()->hasFlag('superpowers', SuperPowers::Immortality)->count()); | ||
$this->assertEquals(1, ModelWithFlaggedQueries::query()->hasFlag('superpowers', SuperPowers::Flight)->count()); | ||
$this->assertEquals(0, ModelWithFlaggedQueries::query()->hasFlag('superpowers', SuperPowers::Invisibility)->count()); | ||
|
||
$this->assertEquals(0, ModelWithFlaggedQueries::query()->notHasFlag('superpowers', SuperPowers::Immortality)->count()); | ||
$this->assertEquals(1, ModelWithFlaggedQueries::query()->notHasFlag('superpowers', SuperPowers::Flight)->count()); | ||
$this->assertEquals(2, ModelWithFlaggedQueries::query()->notHasFlag('superpowers', SuperPowers::Invisibility)->count()); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
|
||
namespace BenSampo\Enum\Tests\Models; | ||
|
||
use BenSampo\Enum\Traits\FlaggedEnumQueries; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ModelWithFlaggedQueries extends Model | ||
{ | ||
use FlaggedEnumQueries; | ||
|
||
public $table = 'test_models'; | ||
|
||
protected $guarded = []; | ||
} |