From 386d6e7c81565ce28dc7c67905ced139cbc7b7d9 Mon Sep 17 00:00:00 2001 From: ell Date: Wed, 4 Jan 2017 14:01:33 +0300 Subject: [PATCH] Add Closed Inverse flag --- CHANGELOG.md | 8 +- CONTRIBUTING.md | 4 +- README.md | 144 ++++++++++++------ composer.json | 8 +- src/Scopes/Inverse/ClosedFlagScope.php | 123 +++++++++++++++ src/Traits/Inverse/HasClosedFlag.php | 32 ++++ src/Traits/Inverse/HasExpiredFlag.php | 2 +- .../factories/EntityWithClosedFlagFactory.php | 17 +++ ...create_entity_with_accepted_flag_table.php | 3 +- ...0_create_entity_with_active_flag_table.php | 3 +- ...create_entity_with_approved_flag_table.php | 3 +- ...0_create_entity_with_closed_flag_table.php | 44 ++++++ ..._create_entity_with_expired_flag_table.php | 3 +- ...750_create_entity_with_kept_flag_table.php | 3 +- ...reate_entity_with_published_flag_table.php | 3 +- ...create_entity_with_verified_flag_table.php | 3 +- .../Models/Inverse/EntityWithClosedFlag.php | 50 ++++++ .../Scopes/Inverse/ClosedFlagScopeTest.php | 111 ++++++++++++++ 18 files changed, 505 insertions(+), 59 deletions(-) create mode 100644 src/Scopes/Inverse/ClosedFlagScope.php create mode 100644 src/Traits/Inverse/HasClosedFlag.php create mode 100644 tests/database/factories/EntityWithClosedFlagFactory.php create mode 100644 tests/database/migrations/2016_09_25_182750_create_entity_with_closed_flag_table.php create mode 100644 tests/stubs/Models/Inverse/EntityWithClosedFlag.php create mode 100644 tests/unit/Scopes/Inverse/ClosedFlagScopeTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f2be214..3277a78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,18 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file. +## 2.1.0 - 2016-01-04 + +- `is_closed` inverse boolean flag added. + ## 2.0.0 - 2016-01-04 -#### Breaking changes +### Breaking changes - Namespaces of flag's traits received `Classic` at the end: `Cog\Flag\Traits\Classic`. - Namespaces of flag's scopes received `Classic` at the end: `Cog\Flag\Scopes\Classic`. -#### Added +### Added - `Inverse Logic` flags group. Hides entities if flag not set. - `is_expired` inverse boolean flag added. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 8500379..cd61d09 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -21,7 +21,7 @@ Due to time constraints, we are not always able to respond as quickly as we woul This project comes with a configuration file for php-cs-fixer (.php_cs) that you can use to (re)format your sourcecode for compliance with this project's coding guidelines: -```shell +```sh vendor/bin/php-cs-fixer fix ``` @@ -29,7 +29,7 @@ vendor/bin/php-cs-fixer fix The phpunit script can be used to invoke the PHPUnit test runner: -```shell +```sh vendor/bin/phpunit ``` diff --git a/README.md b/README.md index 1325314..04f2c29 100644 --- a/README.md +++ b/README.md @@ -18,15 +18,16 @@ Eloquent flagged attributes behavior. Add commonly used flags to models very qui ## Available flags list -| Trait name | Database columns | Flag type | Logic | -| ---------- | ---------------- | --------- | ----- | -| `HasAcceptedFlag` | `is_accepted` | Boolean | Classic | -| `HasActiveFlag` | `is_active` | Boolean | Classic | -| `HasApprovedFlag` | `is_approved` | Boolean | Classic | -| `HasExpiredInverseFlag` | `is_expired` | Boolean | Inverse | -| `HasKeptFlag` | `is_kept` | Boolean | Classic | -| `HasPublishedFlag` | `is_published` | Boolean | Classic | -| `HasVerifiedFlag` | `is_verified` | Boolean | Classic | +| Trait name | Logic | Database columns | Flag type | +| ---------- | ----- | ---------------- | --------- | +| `HasAcceptedFlag` | Classic | `is_accepted` | Boolean | +| `HasActiveFlag` | Classic | `is_active` | Boolean | +| `HasApprovedFlag` | Classic | `is_approved` | Boolean | +| `HasClosedFlag` | Inverse | `is_closed` | Boolean | +| `HasExpiredFlag` | Inverse | `is_expired` | Boolean | +| `HasKeptFlag` | Classic | `is_kept` | Boolean | +| `HasPublishedFlag` | Classic | `is_published` | Boolean | +| `HasVerifiedFlag` | Classic | `is_verified` | Boolean | ## How it works @@ -81,32 +82,32 @@ class Post extends Model #### Get only active models -```shell +```php Post::all(); Post::withoutInactive(); ``` #### Get only inactive models -```shell +```php Post::onlyInactive(); ``` #### Get active + inactive models -```shell +```php Post::withInactive(); ``` #### Activate model -```shell +```php Post::where('id', 4)->activate(); ``` #### Deactivate model -```shell +```php Post::where('id', 4)->deactivate(); ``` @@ -132,32 +133,32 @@ class Post extends Model #### Get only accepted models -```shell +```php Post::all(); Post::withoutUnaccepted(); ``` #### Get only unaccepted models -```shell +```php Post::onlyUnaccepted(); ``` #### Get accepted + unaccepted models -```shell +```php Post::withUnaccepted(); ``` #### Accept model -```shell +```php Post::where('id', 4)->accept(); ``` #### Deactivate model -```shell +```php Post::where('id', 4)->unaccept(); ``` @@ -183,32 +184,32 @@ class Post extends Model #### Get only approved models -```shell +```php Post::all(); Post::withoutUnapproved(); ``` #### Get only unapproved models -```shell +```php Post::onlyUnapproved(); ``` #### Get approved + unapproved models -```shell +```php Post::withUnapproved(); ``` #### Approve model -```shell +```php Post::where('id', 4)->approve(); ``` #### Unapprove model -```shell +```php Post::where('id', 4)->unapprove(); ``` @@ -234,32 +235,32 @@ class Post extends Model #### Get only published models -```shell +```php Post::all(); Post::withoutUnpublished(); ``` #### Get only unpublished models -```shell +```php Post::onlyUnpublished(); ``` #### Get published + unpublished models -```shell +```php Post::withUnpublished(); ``` #### Publish model -```shell +```php Post::where('id', 4)->publish(); ``` #### Unpublish model -```shell +```php Post::where('id', 4)->unpublish(); ``` @@ -285,32 +286,32 @@ class Post extends Model #### Get only verified models -```shell +```php Post::all(); Post::withoutUnverified(); ``` #### Get only unverified models -```shell +```php Post::onlyUnverified(); ``` #### Get verified + unverified models -```shell +```php Post::withUnverified(); ``` #### Verify model -```shell +```php Post::where('id', 4)->verify(); ``` #### Unverify model -```shell +```php Post::where('id', 4)->unverify(); ``` @@ -361,38 +362,38 @@ By default all records that have a `is_kept` equals to 0 will be excluded from y #### Get only kept models -```shell +```php Post::all(); Post::withoutUnkept(); ``` #### Get only unkept models -```shell +```php Post::onlyUnkept(); ``` #### Get kept + unkept models -```shell +```php Post::withUnkept(); ``` #### Keep model -```shell +```php Post::where('id', 4)->keep(); ``` #### Unkeep model -```shell +```php Post::where('id', 4)->unkeep(); ``` #### Get unkept models which older than hours -```shell +```php Post::onlyUnkeptOlderThanHours(4); ``` @@ -420,40 +421,91 @@ class Post extends Model #### Get only not expired models -```shell +```php Post::all(); Post::withoutExpired(); ``` #### Get only expired models -```shell +```php Post::onlyExpired(); ``` #### Get expired + not expired models -```shell +```php Post::withExpired(); ``` #### Set expire flag to model -```shell +```php Post::where('id', 4)->expire(); ``` #### Remove expire flag from model -```shell +```php Post::where('id', 4)->unexpire(); ``` +### Setup a closable model + +```php +close(); +``` + +#### Remove close flag from model + +```php +Post::where('id', 4)->unclose(); +``` + ## Testing Run the tests with: -```shell +```sh vendor/bin/phpunit ``` diff --git a/composer.json b/composer.json index c3718b0..cdeadab 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,7 @@ "active", "activated", "deactivated", + "inactive", "kept", "unkept", "published", @@ -25,7 +26,12 @@ "unverified", "persisted", "approved", - "unapproved" + "unapproved", + "toggle", + "closed", + "opened", + "expiry", + "expired" ], "authors": [ { diff --git a/src/Scopes/Inverse/ClosedFlagScope.php b/src/Scopes/Inverse/ClosedFlagScope.php new file mode 100644 index 0000000..aea7d4c --- /dev/null +++ b/src/Scopes/Inverse/ClosedFlagScope.php @@ -0,0 +1,123 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Scopes\Inverse; + +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Scope; + +/** + * Class ClosedFlagScope. + * + * @package Cog\Flag\Scopes\Inverse + */ +class ClosedFlagScope implements Scope +{ + /** + * All of the extensions to be added to the builder. + * + * @var array + */ + protected $extensions = ['Open', 'Close', 'WithClosed', 'WithoutClosed', 'OnlyClosed']; + + /** + * Apply the scope to a given Eloquent query builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @param \Illuminate\Database\Eloquent\Model $model + * @return \Illuminate\Database\Eloquent\Builder + */ + public function apply(Builder $builder, Model $model) + { + return $builder->where('is_closed', 0); + } + + /** + * Extend the query builder with the needed functions. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + public function extend(Builder $builder) + { + foreach ($this->extensions as $extension) { + $this->{"add{$extension}"}($builder); + } + } + + /** + * Add the `open` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOpen(Builder $builder) + { + $builder->macro('open', function (Builder $builder) { + $builder->withClosed(); + + return $builder->update(['is_closed' => 0]); + }); + } + + /** + * Add the `close` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addClose(Builder $builder) + { + $builder->macro('close', function (Builder $builder) { + return $builder->update(['is_closed' => 1]); + }); + } + + /** + * Add the `withClosed` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithClosed(Builder $builder) + { + $builder->macro('withClosed', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the `withoutClosed` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutClosed(Builder $builder) + { + $builder->macro('withoutClosed', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_closed', 0); + }); + } + + /** + * Add the `onlyClosed` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyClosed(Builder $builder) + { + $builder->macro('onlyClosed', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_closed', 1); + }); + } +} diff --git a/src/Traits/Inverse/HasClosedFlag.php b/src/Traits/Inverse/HasClosedFlag.php new file mode 100644 index 0000000..2531ff1 --- /dev/null +++ b/src/Traits/Inverse/HasClosedFlag.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Inverse; + +use Cog\Flag\Scopes\Inverse\ClosedFlagScope; + +/** + * Class HasClosedFlag. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasClosedFlag +{ + /** + * Boot the HasClosedFlag trait for a model. + * + * @return void + */ + public static function bootHasClosedFlag() + { + static::addGlobalScope(new ClosedFlagScope); + } +} diff --git a/src/Traits/Inverse/HasExpiredFlag.php b/src/Traits/Inverse/HasExpiredFlag.php index d8cc167..61d2508 100644 --- a/src/Traits/Inverse/HasExpiredFlag.php +++ b/src/Traits/Inverse/HasExpiredFlag.php @@ -16,7 +16,7 @@ /** * Class HasExpiredFlag. * - * @package Cog\Flag\Traits\Classic + * @package Cog\Flag\Traits\Inverse */ trait HasExpiredFlag { diff --git a/tests/database/factories/EntityWithClosedFlagFactory.php b/tests/database/factories/EntityWithClosedFlagFactory.php new file mode 100644 index 0000000..567be32 --- /dev/null +++ b/tests/database/factories/EntityWithClosedFlagFactory.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +$factory->define(\Cog\Flag\Tests\Stubs\Models\Inverse\EntityWithClosedFlag::class, function (\Faker\Generator $faker) { + return [ + 'name' => $faker->word, + 'is_closed' => true, + ]; +}); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_accepted_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_accepted_flag_table.php index fc00401..d00999e 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_accepted_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_accepted_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithAcceptedFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithAcceptedFlagTable extends Migration */ public function up() { - Schema::create('entity_with_accepted_flag', function ($table) { + Schema::create('entity_with_accepted_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_accepted'); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_active_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_active_flag_table.php index 570ce89..bdfcba8 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_active_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_active_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithActiveFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithActiveFlagTable extends Migration */ public function up() { - Schema::create('entity_with_active_flag', function ($table) { + Schema::create('entity_with_active_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_active'); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_approved_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_approved_flag_table.php index 2ae1cc0..fe48f75 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_approved_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_approved_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithApprovedFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithApprovedFlagTable extends Migration */ public function up() { - Schema::create('entity_with_approved_flag', function ($table) { + Schema::create('entity_with_approved_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_approved'); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_closed_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_closed_flag_table.php new file mode 100644 index 0000000..3973ea1 --- /dev/null +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_closed_flag_table.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; + +/** + * Class CreateEntityWithClosedFlagTable. + */ +class CreateEntityWithClosedFlagTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('entity_with_closed_flag', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->boolean('is_closed'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('entity_with_closed_flag'); + } +} diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_expired_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_expired_flag_table.php index dc5e698..eb8c45c 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_expired_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_expired_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithExpiredFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithExpiredFlagTable extends Migration */ public function up() { - Schema::create('entity_with_expired_flag', function ($table) { + Schema::create('entity_with_expired_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_expired'); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_kept_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_kept_flag_table.php index b30073e..6c29f3b 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_kept_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_kept_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithKeptFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithKeptFlagTable extends Migration */ public function up() { - Schema::create('entity_with_kept_flag', function ($table) { + Schema::create('entity_with_kept_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_kept')->default(false); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_published_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_published_flag_table.php index 6aa52ce..797aaae 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_published_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_published_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithPublishedFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithPublishedFlagTable extends Migration */ public function up() { - Schema::create('entity_with_published_flag', function ($table) { + Schema::create('entity_with_published_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_published'); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_verified_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_verified_flag_table.php index c0acce5..9cfcd8c 100644 --- a/tests/database/migrations/2016_09_25_182750_create_entity_with_verified_flag_table.php +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_verified_flag_table.php @@ -10,6 +10,7 @@ */ use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; /** * Class CreateEntityWithVerifiedFlagTable. @@ -23,7 +24,7 @@ class CreateEntityWithVerifiedFlagTable extends Migration */ public function up() { - Schema::create('entity_with_verified_flag', function ($table) { + Schema::create('entity_with_verified_flag', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->boolean('is_verified'); diff --git a/tests/stubs/Models/Inverse/EntityWithClosedFlag.php b/tests/stubs/Models/Inverse/EntityWithClosedFlag.php new file mode 100644 index 0000000..2f8d176 --- /dev/null +++ b/tests/stubs/Models/Inverse/EntityWithClosedFlag.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Tests\Stubs\Models\Inverse; + +use Cog\Flag\Traits\Inverse\HasClosedFlag; +use Illuminate\Database\Eloquent\Model; + +/** + * Class EntityWithClosedFlag. + * + * @package Cog\Flag\Tests\Stubs\Models\Inverse + */ +class EntityWithClosedFlag extends Model +{ + use HasClosedFlag; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'entity_with_closed_flag'; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'name', + ]; + + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'is_closed' => 'bool', + ]; +} diff --git a/tests/unit/Scopes/Inverse/ClosedFlagScopeTest.php b/tests/unit/Scopes/Inverse/ClosedFlagScopeTest.php new file mode 100644 index 0000000..953acdd --- /dev/null +++ b/tests/unit/Scopes/Inverse/ClosedFlagScopeTest.php @@ -0,0 +1,111 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Tests\Unit\Scopes\Inverse; + +use Cog\Flag\Tests\Stubs\Models\Inverse\EntityWithClosedFlag; +use Cog\Flag\Tests\TestCase; + +/** + * Class ClosedFlagScopeTest. + * + * @package Cog\Flag\Tests\Unit\Scopes\Inverse + */ +class ClosedFlagScopeTest extends TestCase +{ + /** @test */ + public function it_can_get_only_not_closed() + { + factory(EntityWithClosedFlag::class, 2)->create([ + 'is_closed' => true, + ]); + factory(EntityWithClosedFlag::class, 3)->create([ + 'is_closed' => false, + ]); + + $entities = EntityWithClosedFlag::all(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_without_closed() + { + factory(EntityWithClosedFlag::class, 2)->create([ + 'is_closed' => true, + ]); + factory(EntityWithClosedFlag::class, 3)->create([ + 'is_closed' => false, + ]); + + $entities = EntityWithClosedFlag::withoutClosed()->get(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_with_closed() + { + factory(EntityWithClosedFlag::class, 2)->create([ + 'is_closed' => true, + ]); + factory(EntityWithClosedFlag::class, 3)->create([ + 'is_closed' => false, + ]); + + $entities = EntityWithClosedFlag::withClosed()->get(); + + $this->assertCount(5, $entities); + } + + /** @test */ + public function it_can_get_only_closed() + { + factory(EntityWithClosedFlag::class, 2)->create([ + 'is_closed' => true, + ]); + factory(EntityWithClosedFlag::class, 3)->create([ + 'is_closed' => false, + ]); + + $entities = EntityWithClosedFlag::onlyClosed()->get(); + + $this->assertCount(2, $entities); + } + + /** @test */ + public function it_can_open_model() + { + $model = factory(EntityWithClosedFlag::class)->create([ + 'is_closed' => true, + ]); + + EntityWithClosedFlag::where('id', $model->id)->open(); + + $model = EntityWithClosedFlag::where('id', $model->id)->first(); + + $this->assertFalse($model->is_closed); + } + + /** @test */ + public function it_can_close_model() + { + $model = factory(EntityWithClosedFlag::class)->create([ + 'is_closed' => false, + ]); + + EntityWithClosedFlag::where('id', $model->id)->close(); + + $model = EntityWithClosedFlag::withClosed()->where('id', $model->id)->first(); + + $this->assertTrue($model->is_closed); + } +}