diff --git a/.styleci.yml b/.styleci.yml index 64b948b..22dd2f9 100644 --- a/.styleci.yml +++ b/.styleci.yml @@ -4,3 +4,4 @@ disabled: - concat_without_spaces - phpdoc_no_package - logical_not_operators_with_successor_space + - length_ordered_imports diff --git a/CHANGELOG.md b/CHANGELOG.md index 602df85..f2be214 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ All notable changes to `laravel-eloquent-flag` will be documented in this file. +## 2.0.0 - 2016-01-04 + +#### 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 + +- `Inverse Logic` flags group. Hides entities if flag not set. +- `is_expired` inverse boolean flag added. + ## 1.5.0 - 2016-12-31 - `is_approved` boolean flag added. diff --git a/README.md b/README.md index 2e3d5a9..1325314 100644 --- a/README.md +++ b/README.md @@ -18,20 +18,26 @@ Eloquent flagged attributes behavior. Add commonly used flags to models very qui ## Available flags list -| Trait name | Database columns | Flag type | -| ---------- | ---------------- | --------- | -| `HasAcceptedFlag` | `is_accepted` | Boolean | -| `HasActiveFlag` | `is_active` | Boolean | -| `HasApprovedFlag` | `is_approved` | Boolean | -| `HasKeptFlag` | `is_kept` | Boolean | -| `HasPublishedFlag` | `is_published` | Boolean | -| `HasVerifiedFlag` | `is_verified` | Boolean | +| 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 | ## How it works Eloquent Flag is an easy way to add flagged attributes to eloquent models. All flags has their own trait which adds global scopes to desired entity. -The main logic of the flags: If flag is `false` - entity should be hidden from the query results. Omitted entities could be retrieved by using special global scope methods. +All flags separated on 2 logical groups: + +- `Classic` flags displays only entities with flag setted as `true`. +- `Inverse` flags displays only entities with flag setted as `false`. + +Omitted entities could be retrieved by using special global scope methods, unique for each flag. ## Installation @@ -60,7 +66,7 @@ And then include the service provider within `app/config/app.php`. namespace App\Models; -use Cog\Flag\Traits\HasActiveFlag; +use Cog\Flag\Traits\Classic\HasActiveFlag; use Illuminate\Database\Eloquent\Model; class Post extends Model @@ -111,7 +117,7 @@ Post::where('id', 4)->deactivate(); namespace App\Models; -use Cog\Flag\Traits\HasAcceptedFlag; +use Cog\Flag\Traits\Classic\HasAcceptedFlag; use Illuminate\Database\Eloquent\Model; class Post extends Model @@ -162,7 +168,7 @@ Post::where('id', 4)->unaccept(); namespace App\Models; -use Cog\Flag\Traits\HasApprovedFlag; +use Cog\Flag\Traits\Classic\HasApprovedFlag; use Illuminate\Database\Eloquent\Model; class Post extends Model @@ -213,7 +219,7 @@ Post::where('id', 4)->unapprove(); namespace App\Models; -use Cog\Flag\Traits\HasPublishedFlag; +use Cog\Flag\Traits\Classic\HasPublishedFlag; use Illuminate\Database\Eloquent\Model; class Post extends Model @@ -264,7 +270,7 @@ Post::where('id', 4)->unpublish(); namespace App\Models; -use Cog\Flag\Traits\HasVerifiedFlag; +use Cog\Flag\Traits\Classic\HasVerifiedFlag; use Illuminate\Database\Eloquent\Model; class Post extends Model @@ -336,7 +342,7 @@ Keep functionality required when you are trying to attach related models before namespace App\Models; -use Cog\Flag\Traits\HasKeptFlag; +use Cog\Flag\Traits\Classic\HasKeptFlag; use Illuminate\Database\Eloquent\Model; class Post extends Model @@ -392,6 +398,57 @@ Post::onlyUnkeptOlderThanHours(4); Output will have all unkept models created earlier than 4 hours ago. +### Setup an expirable model + +```php +expire(); +``` + +#### Remove expire flag from model + +```shell +Post::where('id', 4)->unexpire(); +``` + ## Testing Run the tests with: diff --git a/src/Providers/FlagServiceProvider.php b/src/Providers/FlagServiceProvider.php index 389404d..76dccd3 100644 --- a/src/Providers/FlagServiceProvider.php +++ b/src/Providers/FlagServiceProvider.php @@ -16,7 +16,7 @@ /** * Class FlagServiceProvider. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Providers */ class FlagServiceProvider extends ServiceProvider { diff --git a/src/Scopes/AcceptedFlagScope.php b/src/Scopes/Classic/AcceptedFlagScope.php similarity index 97% rename from src/Scopes/AcceptedFlagScope.php rename to src/Scopes/Classic/AcceptedFlagScope.php index d1ec557..10235da 100644 --- a/src/Scopes/AcceptedFlagScope.php +++ b/src/Scopes/Classic/AcceptedFlagScope.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Scopes; +namespace Cog\Flag\Scopes\Classic; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Builder; /** * Class AcceptedFlagScope. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Scopes\Classic */ class AcceptedFlagScope implements Scope { diff --git a/src/Scopes/ActiveFlagScope.php b/src/Scopes/Classic/ActiveFlagScope.php similarity index 97% rename from src/Scopes/ActiveFlagScope.php rename to src/Scopes/Classic/ActiveFlagScope.php index 54ef208..2c4f120 100644 --- a/src/Scopes/ActiveFlagScope.php +++ b/src/Scopes/Classic/ActiveFlagScope.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Scopes; +namespace Cog\Flag\Scopes\Classic; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Builder; /** * Class ActiveFlagScope. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Scopes\Classic */ class ActiveFlagScope implements Scope { diff --git a/src/Scopes/ApprovedFlagScope.php b/src/Scopes/Classic/ApprovedFlagScope.php similarity index 97% rename from src/Scopes/ApprovedFlagScope.php rename to src/Scopes/Classic/ApprovedFlagScope.php index 774513b..6e6d54e 100644 --- a/src/Scopes/ApprovedFlagScope.php +++ b/src/Scopes/Classic/ApprovedFlagScope.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Scopes; +namespace Cog\Flag\Scopes\Classic; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Builder; /** * Class ApprovedFlagScope. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Scopes\Classic */ class ApprovedFlagScope implements Scope { diff --git a/src/Scopes/KeptFlagScope.php b/src/Scopes/Classic/KeptFlagScope.php similarity index 97% rename from src/Scopes/KeptFlagScope.php rename to src/Scopes/Classic/KeptFlagScope.php index 5b99e1b..443a6e8 100644 --- a/src/Scopes/KeptFlagScope.php +++ b/src/Scopes/Classic/KeptFlagScope.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Scopes; +namespace Cog\Flag\Scopes\Classic; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Builder; /** * Class KeptFlagScope. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Scopes\Classic */ class KeptFlagScope implements Scope { diff --git a/src/Scopes/PublishedFlagScope.php b/src/Scopes/Classic/PublishedFlagScope.php similarity index 97% rename from src/Scopes/PublishedFlagScope.php rename to src/Scopes/Classic/PublishedFlagScope.php index 4224abc..ad46cf1 100644 --- a/src/Scopes/PublishedFlagScope.php +++ b/src/Scopes/Classic/PublishedFlagScope.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Scopes; +namespace Cog\Flag\Scopes\Classic; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Builder; /** * Class PublishedFlagScope. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Scopes\Classic */ class PublishedFlagScope implements Scope { diff --git a/src/Scopes/VerifiedFlagScope.php b/src/Scopes/Classic/VerifiedFlagScope.php similarity index 97% rename from src/Scopes/VerifiedFlagScope.php rename to src/Scopes/Classic/VerifiedFlagScope.php index d696957..7672113 100644 --- a/src/Scopes/VerifiedFlagScope.php +++ b/src/Scopes/Classic/VerifiedFlagScope.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Scopes; +namespace Cog\Flag\Scopes\Classic; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Scope; -use Illuminate\Database\Eloquent\Builder; /** * Class VerifiedFlagScope. * - * @package Cog\Flag\Scopes + * @package Cog\Flag\Scopes\Classic */ class VerifiedFlagScope implements Scope { diff --git a/src/Scopes/Inverse/ExpiredFlagScope.php b/src/Scopes/Inverse/ExpiredFlagScope.php new file mode 100644 index 0000000..7ba17d6 --- /dev/null +++ b/src/Scopes/Inverse/ExpiredFlagScope.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 ExpiredFlagScope. + * + * @package Cog\Flag\Scopes\Inverse + */ +class ExpiredFlagScope implements Scope +{ + /** + * All of the extensions to be added to the builder. + * + * @var array + */ + protected $extensions = ['Unexpire', 'Expire', 'WithExpired', 'WithoutExpired', 'OnlyExpired']; + + /** + * 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_expired', 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 `unexpire` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addUnexpire(Builder $builder) + { + $builder->macro('unexpire', function (Builder $builder) { + $builder->withExpired(); + + return $builder->update(['is_expired' => 0]); + }); + } + + /** + * Add the `expire` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addExpire(Builder $builder) + { + $builder->macro('expire', function (Builder $builder) { + return $builder->update(['is_expired' => 1]); + }); + } + + /** + * Add the `withExpired` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithExpired(Builder $builder) + { + $builder->macro('withExpired', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the `withoutExpired` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutExpired(Builder $builder) + { + $builder->macro('withoutExpired', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_expired', 0); + }); + } + + /** + * Add the `onlyExpired` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyExpired(Builder $builder) + { + $builder->macro('onlyExpired', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_expired', 1); + }); + } +} diff --git a/src/Traits/HasAcceptedFlag.php b/src/Traits/Classic/HasAcceptedFlag.php similarity index 81% rename from src/Traits/HasAcceptedFlag.php rename to src/Traits/Classic/HasAcceptedFlag.php index 31b59d9..cca9746 100644 --- a/src/Traits/HasAcceptedFlag.php +++ b/src/Traits/Classic/HasAcceptedFlag.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Traits; +namespace Cog\Flag\Traits\Classic; -use Cog\Flag\Scopes\AcceptedFlagScope; +use Cog\Flag\Scopes\Classic\AcceptedFlagScope; /** * Class HasAcceptedFlag. * - * @package Cog\Flag\Traits + * @package Cog\Flag\Traits\Classic */ trait HasAcceptedFlag { diff --git a/src/Traits/HasActiveFlag.php b/src/Traits/Classic/HasActiveFlag.php similarity index 81% rename from src/Traits/HasActiveFlag.php rename to src/Traits/Classic/HasActiveFlag.php index 09ab41f..2b1a11b 100644 --- a/src/Traits/HasActiveFlag.php +++ b/src/Traits/Classic/HasActiveFlag.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Traits; +namespace Cog\Flag\Traits\Classic; -use Cog\Flag\Scopes\ActiveFlagScope; +use Cog\Flag\Scopes\Classic\ActiveFlagScope; /** * Class HasActiveFlag. * - * @package Cog\Flag\Traits + * @package Cog\Flag\Traits\Classic */ trait HasActiveFlag { diff --git a/src/Traits/HasApprovedFlag.php b/src/Traits/Classic/HasApprovedFlag.php similarity index 81% rename from src/Traits/HasApprovedFlag.php rename to src/Traits/Classic/HasApprovedFlag.php index 0a041f2..d6a91b9 100644 --- a/src/Traits/HasApprovedFlag.php +++ b/src/Traits/Classic/HasApprovedFlag.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Traits; +namespace Cog\Flag\Traits\Classic; -use Cog\Flag\Scopes\ApprovedFlagScope; +use Cog\Flag\Scopes\Classic\ApprovedFlagScope; /** * Class HasApprovedFlag. * - * @package Cog\Flag\Traits + * @package Cog\Flag\Traits\Classic */ trait HasApprovedFlag { diff --git a/src/Traits/HasKeptFlag.php b/src/Traits/Classic/HasKeptFlag.php similarity index 92% rename from src/Traits/HasKeptFlag.php rename to src/Traits/Classic/HasKeptFlag.php index f2a64d1..380962c 100644 --- a/src/Traits/HasKeptFlag.php +++ b/src/Traits/Classic/HasKeptFlag.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Traits; +namespace Cog\Flag\Traits\Classic; use Carbon\Carbon; -use Cog\Flag\Scopes\KeptFlagScope; +use Cog\Flag\Scopes\Classic\KeptFlagScope; use Illuminate\Database\Eloquent\Builder; /** * Class HasKeptFlag. * - * @package Cog\Flag\Traits + * @package Cog\Flag\Traits\Classic */ trait HasKeptFlag { diff --git a/src/Traits/HasPublishedFlag.php b/src/Traits/Classic/HasPublishedFlag.php similarity index 81% rename from src/Traits/HasPublishedFlag.php rename to src/Traits/Classic/HasPublishedFlag.php index e58bcbe..8fe03f9 100644 --- a/src/Traits/HasPublishedFlag.php +++ b/src/Traits/Classic/HasPublishedFlag.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Traits; +namespace Cog\Flag\Traits\Classic; -use Cog\Flag\Scopes\PublishedFlagScope; +use Cog\Flag\Scopes\Classic\PublishedFlagScope; /** * Class HasPublishedFlag. * - * @package Cog\Flag\Traits + * @package Cog\Flag\Traits\Classic */ trait HasPublishedFlag { diff --git a/src/Traits/HasVerifiedFlag.php b/src/Traits/Classic/HasVerifiedFlag.php similarity index 80% rename from src/Traits/HasVerifiedFlag.php rename to src/Traits/Classic/HasVerifiedFlag.php index 33254e4..a0b6303 100644 --- a/src/Traits/HasVerifiedFlag.php +++ b/src/Traits/Classic/HasVerifiedFlag.php @@ -9,14 +9,14 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Traits; +namespace Cog\Flag\Traits\Classic; -use Cog\Flag\Scopes\VerifiedFlagScope; +use Cog\Flag\Scopes\Classic\VerifiedFlagScope; /** * Class HasVerifiedFlag. * - * @package Cog\Flag\Traits + * @package Cog\Flag\Traits\Classic */ trait HasVerifiedFlag { diff --git a/src/Traits/Inverse/HasExpiredFlag.php b/src/Traits/Inverse/HasExpiredFlag.php new file mode 100644 index 0000000..d8cc167 --- /dev/null +++ b/src/Traits/Inverse/HasExpiredFlag.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\ExpiredFlagScope; + +/** + * Class HasExpiredFlag. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasExpiredFlag +{ + /** + * Boot the HasExpiredFlag trait for a model. + * + * @return void + */ + public static function bootHasExpiredFlag() + { + static::addGlobalScope(new ExpiredFlagScope); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 07daeee..81d109a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,8 +11,8 @@ namespace Cog\Flag\Tests; -use Illuminate\Support\Facades\File; use Cog\Flag\Providers\FlagServiceProvider; +use Illuminate\Support\Facades\File; use Orchestra\Testbench\TestCase as Orchestra; /** diff --git a/tests/database/factories/EntityWithAcceptedFlagFactory.php b/tests/database/factories/EntityWithAcceptedFlagFactory.php index f89fcc7..a037b67 100644 --- a/tests/database/factories/EntityWithAcceptedFlagFactory.php +++ b/tests/database/factories/EntityWithAcceptedFlagFactory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithAcceptedFlag::class, function (\Faker\Generator $faker) { +$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithAcceptedFlag::class, function (\Faker\Generator $faker) { return [ 'name' => $faker->word, 'is_accepted' => true, diff --git a/tests/database/factories/EntityWithActiveFlagFactory.php b/tests/database/factories/EntityWithActiveFlagFactory.php index b3d630c..c1761b9 100644 --- a/tests/database/factories/EntityWithActiveFlagFactory.php +++ b/tests/database/factories/EntityWithActiveFlagFactory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithActiveFlag::class, function (\Faker\Generator $faker) { +$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithActiveFlag::class, function (\Faker\Generator $faker) { return [ 'name' => $faker->word, 'is_active' => true, diff --git a/tests/database/factories/EntityWithApprovedFlagFactory.php b/tests/database/factories/EntityWithApprovedFlagFactory.php index 2f57e5e..34bd115 100644 --- a/tests/database/factories/EntityWithApprovedFlagFactory.php +++ b/tests/database/factories/EntityWithApprovedFlagFactory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithApprovedFlag::class, function (\Faker\Generator $faker) { +$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithApprovedFlag::class, function (\Faker\Generator $faker) { return [ 'name' => $faker->word, 'is_approved' => true, diff --git a/tests/database/factories/EntityWithExpiredFlagFactory.php b/tests/database/factories/EntityWithExpiredFlagFactory.php new file mode 100644 index 0000000..2779704 --- /dev/null +++ b/tests/database/factories/EntityWithExpiredFlagFactory.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\EntityWithExpiredFlag::class, function (\Faker\Generator $faker) { + return [ + 'name' => $faker->word, + 'is_expired' => false, + ]; +}); diff --git a/tests/database/factories/EntityWithKeptFlagFactory.php b/tests/database/factories/EntityWithKeptFlagFactory.php index c6d4be2..a5d7c63 100644 --- a/tests/database/factories/EntityWithKeptFlagFactory.php +++ b/tests/database/factories/EntityWithKeptFlagFactory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithKeptFlag::class, function (\Faker\Generator $faker) { +$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithKeptFlag::class, function (\Faker\Generator $faker) { return [ 'name' => $faker->word, 'is_kept' => false, diff --git a/tests/database/factories/EntityWithPublishedFlagFactory.php b/tests/database/factories/EntityWithPublishedFlagFactory.php index 5776f25..6068d9a 100644 --- a/tests/database/factories/EntityWithPublishedFlagFactory.php +++ b/tests/database/factories/EntityWithPublishedFlagFactory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithPublishedFlag::class, function (\Faker\Generator $faker) { +$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedFlag::class, function (\Faker\Generator $faker) { return [ 'name' => $faker->word, 'is_published' => true, diff --git a/tests/database/factories/EntityWithVerifiedFlagFactory.php b/tests/database/factories/EntityWithVerifiedFlagFactory.php index 3066de7..26de18d 100644 --- a/tests/database/factories/EntityWithVerifiedFlagFactory.php +++ b/tests/database/factories/EntityWithVerifiedFlagFactory.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -$factory->define(\Cog\Flag\Tests\Stubs\Models\EntityWithVerifiedFlag::class, function (\Faker\Generator $faker) { +$factory->define(\Cog\Flag\Tests\Stubs\Models\Classic\EntityWithVerifiedFlag::class, function (\Faker\Generator $faker) { return [ 'name' => $faker->word, 'is_verified' => true, 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 new file mode 100644 index 0000000..dc5e698 --- /dev/null +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_expired_flag_table.php @@ -0,0 +1,43 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Illuminate\Database\Migrations\Migration; + +/** + * Class CreateEntityWithExpiredFlagTable. + */ +class CreateEntityWithExpiredFlagTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('entity_with_expired_flag', function ($table) { + $table->increments('id'); + $table->string('name'); + $table->boolean('is_expired'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('entity_with_expired_flag'); + } +} diff --git a/tests/stubs/Models/EntityWithAcceptedFlag.php b/tests/stubs/Models/Classic/EntityWithAcceptedFlag.php similarity index 85% rename from tests/stubs/Models/EntityWithAcceptedFlag.php rename to tests/stubs/Models/Classic/EntityWithAcceptedFlag.php index 0345a88..3a9c8b3 100644 --- a/tests/stubs/Models/EntityWithAcceptedFlag.php +++ b/tests/stubs/Models/Classic/EntityWithAcceptedFlag.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Stubs\Models; +namespace Cog\Flag\Tests\Stubs\Models\Classic; -use Cog\Flag\Traits\HasAcceptedFlag; +use Cog\Flag\Traits\Classic\HasAcceptedFlag; use Illuminate\Database\Eloquent\Model; /** * Class EntityWithAcceptedFlag. * - * @package Cog\Flag\Tests\Stubs\Models + * @package Cog\Flag\Tests\Stubs\Models\Classic */ class EntityWithAcceptedFlag extends Model { diff --git a/tests/stubs/Models/EntityWithActiveFlag.php b/tests/stubs/Models/Classic/EntityWithActiveFlag.php similarity index 85% rename from tests/stubs/Models/EntityWithActiveFlag.php rename to tests/stubs/Models/Classic/EntityWithActiveFlag.php index 69c7777..d0c3264 100644 --- a/tests/stubs/Models/EntityWithActiveFlag.php +++ b/tests/stubs/Models/Classic/EntityWithActiveFlag.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Stubs\Models; +namespace Cog\Flag\Tests\Stubs\Models\Classic; -use Cog\Flag\Traits\HasActiveFlag; +use Cog\Flag\Traits\Classic\HasActiveFlag; use Illuminate\Database\Eloquent\Model; /** * Class EntityWithActiveFlag. * - * @package Cog\Flag\Tests\Stubs\Models + * @package Cog\Flag\Tests\Stubs\Models\Classic */ class EntityWithActiveFlag extends Model { diff --git a/tests/stubs/Models/EntityWithApprovedFlag.php b/tests/stubs/Models/Classic/EntityWithApprovedFlag.php similarity index 85% rename from tests/stubs/Models/EntityWithApprovedFlag.php rename to tests/stubs/Models/Classic/EntityWithApprovedFlag.php index 97fc37b..9f75991 100644 --- a/tests/stubs/Models/EntityWithApprovedFlag.php +++ b/tests/stubs/Models/Classic/EntityWithApprovedFlag.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Stubs\Models; +namespace Cog\Flag\Tests\Stubs\Models\Classic; -use Cog\Flag\Traits\HasApprovedFlag; +use Cog\Flag\Traits\Classic\HasApprovedFlag; use Illuminate\Database\Eloquent\Model; /** * Class EntityWithApprovedFlag. * - * @package Cog\Flag\Tests\Stubs\Models + * @package Cog\Flag\Tests\Stubs\Models\Classic */ class EntityWithApprovedFlag extends Model { diff --git a/tests/stubs/Models/EntityWithKeptFlag.php b/tests/stubs/Models/Classic/EntityWithKeptFlag.php similarity index 85% rename from tests/stubs/Models/EntityWithKeptFlag.php rename to tests/stubs/Models/Classic/EntityWithKeptFlag.php index d41b4ea..aa2745e 100644 --- a/tests/stubs/Models/EntityWithKeptFlag.php +++ b/tests/stubs/Models/Classic/EntityWithKeptFlag.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Stubs\Models; +namespace Cog\Flag\Tests\Stubs\Models\Classic; -use Cog\Flag\Traits\HasKeptFlag; +use Cog\Flag\Traits\Classic\HasKeptFlag; use Illuminate\Database\Eloquent\Model; /** * Class EntityWithKeptFlag. * - * @package Cog\Flag\Tests\Stubs\Models + * @package Cog\Flag\Tests\Stubs\Models\Classic */ class EntityWithKeptFlag extends Model { diff --git a/tests/stubs/Models/EntityWithPublishedFlag.php b/tests/stubs/Models/Classic/EntityWithPublishedFlag.php similarity index 85% rename from tests/stubs/Models/EntityWithPublishedFlag.php rename to tests/stubs/Models/Classic/EntityWithPublishedFlag.php index 3020fa1..cc633d6 100644 --- a/tests/stubs/Models/EntityWithPublishedFlag.php +++ b/tests/stubs/Models/Classic/EntityWithPublishedFlag.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Stubs\Models; +namespace Cog\Flag\Tests\Stubs\Models\Classic; -use Cog\Flag\Traits\HasPublishedFlag; +use Cog\Flag\Traits\Classic\HasPublishedFlag; use Illuminate\Database\Eloquent\Model; /** * Class EntityWithPublishedFlag. * - * @package Cog\Flag\Tests\Stubs\Models + * @package Cog\Flag\Tests\Stubs\Models\Classic */ class EntityWithPublishedFlag extends Model { diff --git a/tests/stubs/Models/EntityWithVerifiedFlag.php b/tests/stubs/Models/Classic/EntityWithVerifiedFlag.php similarity index 85% rename from tests/stubs/Models/EntityWithVerifiedFlag.php rename to tests/stubs/Models/Classic/EntityWithVerifiedFlag.php index ea10afa..8e4d33a 100644 --- a/tests/stubs/Models/EntityWithVerifiedFlag.php +++ b/tests/stubs/Models/Classic/EntityWithVerifiedFlag.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Stubs\Models; +namespace Cog\Flag\Tests\Stubs\Models\Classic; -use Cog\Flag\Traits\HasVerifiedFlag; +use Cog\Flag\Traits\Classic\HasVerifiedFlag; use Illuminate\Database\Eloquent\Model; /** * Class EntityWithVerifiedFlag. * - * @package Cog\Flag\Tests\Stubs\Models + * @package Cog\Flag\Tests\Stubs\Models\Classic */ class EntityWithVerifiedFlag extends Model { diff --git a/tests/stubs/Models/Inverse/EntityWithExpiredFlag.php b/tests/stubs/Models/Inverse/EntityWithExpiredFlag.php new file mode 100644 index 0000000..4d52c9e --- /dev/null +++ b/tests/stubs/Models/Inverse/EntityWithExpiredFlag.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\HasExpiredFlag; +use Illuminate\Database\Eloquent\Model; + +/** + * Class EntityWithExpiredFlag. + * + * @package Cog\Flag\Tests\Stubs\Models\Inverse + */ +class EntityWithExpiredFlag extends Model +{ + use HasExpiredFlag; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'entity_with_expired_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_expired' => 'bool', + ]; +} diff --git a/tests/unit/Scopes/AcceptedFlagScopeTest.php b/tests/unit/Scopes/Classic/AcceptedFlagScopeTest.php similarity index 94% rename from tests/unit/Scopes/AcceptedFlagScopeTest.php rename to tests/unit/Scopes/Classic/AcceptedFlagScopeTest.php index 21b42c2..576c401 100644 --- a/tests/unit/Scopes/AcceptedFlagScopeTest.php +++ b/tests/unit/Scopes/Classic/AcceptedFlagScopeTest.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Scopes; +namespace Cog\Flag\Tests\Unit\Scopes\Classic; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithAcceptedFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithAcceptedFlag; /** * Class AcceptedFlagScopeTest. * - * @package Cog\Flag\Tests\Unit\Scopes + * @package Cog\Flag\Tests\Unit\Scopes\Classic */ class AcceptedFlagScopeTest extends TestCase { diff --git a/tests/unit/Scopes/ActiveFlagScopeTest.php b/tests/unit/Scopes/Classic/ActiveFlagScopeTest.php similarity index 94% rename from tests/unit/Scopes/ActiveFlagScopeTest.php rename to tests/unit/Scopes/Classic/ActiveFlagScopeTest.php index f0807e5..3fa0729 100644 --- a/tests/unit/Scopes/ActiveFlagScopeTest.php +++ b/tests/unit/Scopes/Classic/ActiveFlagScopeTest.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Scopes; +namespace Cog\Flag\Tests\Unit\Scopes\Classic; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithActiveFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithActiveFlag; /** * Class ActiveFlagScopeTest. * - * @package Cog\Flag\Tests\Unit\Scopes + * @package Cog\Flag\Tests\Unit\Scopes\Classic */ class ActiveFlagScopeTest extends TestCase { diff --git a/tests/unit/Scopes/ApprovedFlagScopeTest.php b/tests/unit/Scopes/Classic/ApprovedFlagScopeTest.php similarity index 94% rename from tests/unit/Scopes/ApprovedFlagScopeTest.php rename to tests/unit/Scopes/Classic/ApprovedFlagScopeTest.php index 5060a13..77906de 100644 --- a/tests/unit/Scopes/ApprovedFlagScopeTest.php +++ b/tests/unit/Scopes/Classic/ApprovedFlagScopeTest.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Scopes; +namespace Cog\Flag\Tests\Unit\Scopes\Classic; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithApprovedFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithApprovedFlag; /** * Class ApprovedFlagScopeTest. * - * @package Cog\Flag\Tests\Unit\Scopes + * @package Cog\Flag\Tests\Unit\Scopes\Classic */ class ApprovedFlagScopeTest extends TestCase { diff --git a/tests/unit/Scopes/KeptFlagScopeTest.php b/tests/unit/Scopes/Classic/KeptFlagScopeTest.php similarity index 94% rename from tests/unit/Scopes/KeptFlagScopeTest.php rename to tests/unit/Scopes/Classic/KeptFlagScopeTest.php index b8419ea..589f099 100644 --- a/tests/unit/Scopes/KeptFlagScopeTest.php +++ b/tests/unit/Scopes/Classic/KeptFlagScopeTest.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Scopes; +namespace Cog\Flag\Tests\Unit\Scopes\Classic; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithKeptFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithKeptFlag; /** * Class KeptFlagScopeTest. * - * @package Cog\Flag\Tests\Unit\Scopes + * @package Cog\Flag\Tests\Unit\Scopes\Classic */ class KeptFlagScopeTest extends TestCase { diff --git a/tests/unit/Scopes/PublishedFlagScopeTest.php b/tests/unit/Scopes/Classic/PublishedFlagScopeTest.php similarity index 94% rename from tests/unit/Scopes/PublishedFlagScopeTest.php rename to tests/unit/Scopes/Classic/PublishedFlagScopeTest.php index 0ce5bbd..6efbbfd 100644 --- a/tests/unit/Scopes/PublishedFlagScopeTest.php +++ b/tests/unit/Scopes/Classic/PublishedFlagScopeTest.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Scopes; +namespace Cog\Flag\Tests\Unit\Scopes\Classic; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithPublishedFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithPublishedFlag; /** * Class PublishedFlagScopeTest. * - * @package Cog\Flag\Tests\Unit\Scopes + * @package Cog\Flag\Tests\Unit\Scopes\Classic */ class PublishedFlagScopeTest extends TestCase { diff --git a/tests/unit/Scopes/VerifiedFlagScopeTest.php b/tests/unit/Scopes/Classic/VerifiedFlagScopeTest.php similarity index 94% rename from tests/unit/Scopes/VerifiedFlagScopeTest.php rename to tests/unit/Scopes/Classic/VerifiedFlagScopeTest.php index 148ccfe..dd4b9dd 100644 --- a/tests/unit/Scopes/VerifiedFlagScopeTest.php +++ b/tests/unit/Scopes/Classic/VerifiedFlagScopeTest.php @@ -9,15 +9,15 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Scopes; +namespace Cog\Flag\Tests\Unit\Scopes\Classic; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithVerifiedFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithVerifiedFlag; /** * Class VerifiedFlagScopeTest. * - * @package Cog\Flag\Tests\Unit\Scopes + * @package Cog\Flag\Tests\Unit\Scopes\Classic */ class VerifiedFlagScopeTest extends TestCase { diff --git a/tests/unit/Scopes/Inverse/ExpiredFlagScopeTest.php b/tests/unit/Scopes/Inverse/ExpiredFlagScopeTest.php new file mode 100644 index 0000000..6580978 --- /dev/null +++ b/tests/unit/Scopes/Inverse/ExpiredFlagScopeTest.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\EntityWithExpiredFlag; +use Cog\Flag\Tests\TestCase; + +/** + * Class ExpiredFlagScopeTest. + * + * @package Cog\Flag\Tests\Unit\Scopes\Inverse + */ +class ExpiredFlagScopeTest extends TestCase +{ + /** @test */ + public function it_can_get_only_not_expired() + { + factory(EntityWithExpiredFlag::class, 2)->create([ + 'is_expired' => true, + ]); + factory(EntityWithExpiredFlag::class, 3)->create([ + 'is_expired' => false, + ]); + + $entities = EntityWithExpiredFlag::all(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_without_expired() + { + factory(EntityWithExpiredFlag::class, 2)->create([ + 'is_expired' => true, + ]); + factory(EntityWithExpiredFlag::class, 3)->create([ + 'is_expired' => false, + ]); + + $entities = EntityWithExpiredFlag::withoutExpired()->get(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_with_expired() + { + factory(EntityWithExpiredFlag::class, 2)->create([ + 'is_expired' => true, + ]); + factory(EntityWithExpiredFlag::class, 3)->create([ + 'is_expired' => false, + ]); + + $entities = EntityWithExpiredFlag::withExpired()->get(); + + $this->assertCount(5, $entities); + } + + /** @test */ + public function it_can_get_only_expired() + { + factory(EntityWithExpiredFlag::class, 2)->create([ + 'is_expired' => true, + ]); + factory(EntityWithExpiredFlag::class, 3)->create([ + 'is_expired' => false, + ]); + + $entities = EntityWithExpiredFlag::onlyExpired()->get(); + + $this->assertCount(2, $entities); + } + + /** @test */ + public function it_can_unexpire_model() + { + $model = factory(EntityWithExpiredFlag::class)->create([ + 'is_expired' => true, + ]); + + EntityWithExpiredFlag::where('id', $model->id)->unexpire(); + + $model = EntityWithExpiredFlag::where('id', $model->id)->first(); + + $this->assertFalse($model->is_expired); + } + + /** @test */ + public function it_can_expire_model() + { + $model = factory(EntityWithExpiredFlag::class)->create([ + 'is_expired' => false, + ]); + + EntityWithExpiredFlag::where('id', $model->id)->expire(); + + $model = EntityWithExpiredFlag::withExpired()->where('id', $model->id)->first(); + + $this->assertTrue($model->is_expired); + } +} diff --git a/tests/unit/Traits/HasKeptFlagTest.php b/tests/unit/Traits/Classic/HasKeptFlagTest.php similarity index 91% rename from tests/unit/Traits/HasKeptFlagTest.php rename to tests/unit/Traits/Classic/HasKeptFlagTest.php index f914511..b1eb469 100644 --- a/tests/unit/Traits/HasKeptFlagTest.php +++ b/tests/unit/Traits/Classic/HasKeptFlagTest.php @@ -9,16 +9,16 @@ * file that was distributed with this source code. */ -namespace Cog\Flag\Tests\Unit\Traits; +namespace Cog\Flag\Tests\Unit\Traits\Classic; use Carbon\Carbon; +use Cog\Flag\Tests\Stubs\Models\Classic\EntityWithKeptFlag; use Cog\Flag\Tests\TestCase; -use Cog\Flag\Tests\Stubs\Models\EntityWithKeptFlag; /** * Class HasKeptFlagTest. * - * @package Cog\Flag\Tests\Unit\Traits + * @package Cog\Flag\Tests\Unit\Traits\Classic */ class HasKeptFlagTest extends TestCase {