diff --git a/.gitignore b/.gitignore index 4246596..8f5beef 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ vendor/ composer.lock composer.phar phpunit.xml +.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 894b31e..11898b9 100644 --- a/README.md +++ b/README.md @@ -52,8 +52,12 @@ Eloquent boolean & timestamp flagged attributes behavior. Enhance eloquent model | `HasApprovedFlag` | Classic | `is_approved` | Boolean | `HasApprovedAt` | | `HasClosedAt` | Inverse | `closed_at` | Timestamp | `HasClosedFlag` | | `HasClosedFlag` | Inverse | `is_closed` | Boolean | `HasClosedAt` | +| `HasEndedAt` | Inverse | `ended_at` | Timestamp | `HasEndedFlag` | +| `HasEndededFlag` | Inverse | `is_ended` | Boolean | `HasEndedAt` | | `HasExpiredAt` | Inverse | `expired_at` | Timestamp | `HasExpiredFlag` | | `HasExpiredFlag` | Inverse | `is_expired` | Boolean | `HasExpiredAt` | +| `HasInvitedAt` | Classic | `invited_at` | Timestamp | `HasInvitedFlag` | +| `HasInvitedFlag` | Classic | `is_invited` | Boolean | `HasInvitedAt` | | `HasKeptFlag` | Classic | `is_kept` | Boolean | - | | `HasPublishedAt` | Classic | `published_at` | Timestamp | `HasPublishedFlag` | | `HasPublishedFlag` | Classic | `is_published` | Boolean | `HasPublishedAt` | diff --git a/composer.json b/composer.json index 0969aff..e009d05 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "closed", "opened", "expiry", - "expired" + "expired", + "ended" ], "authors": [ { diff --git a/src/Scopes/Classic/InvitedAtScope.php b/src/Scopes/Classic/InvitedAtScope.php new file mode 100644 index 0000000..ffdb714 --- /dev/null +++ b/src/Scopes/Classic/InvitedAtScope.php @@ -0,0 +1,128 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Scopes\Classic; + +use Carbon\Carbon; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Scope; + +/** + * Class InvitedAtScope. + * + * @package Cog\Flag\Scopes\Classic + */ +class InvitedAtScope implements Scope +{ + /** + * All of the extensions to be added to the builder. + * + * @var array + */ + protected $extensions = ['Invite', 'Uninvite', 'WithUninvited', 'WithoutUninvited', 'OnlyUninvited']; + + /** + * 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) + { + if (method_exists($model, 'shouldApplyInvitedAtScope') && !$model->shouldApplyInvitedAtScope()) { + return $builder; + } + + return $builder->whereNotNull('invited_at'); + } + + /** + * 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 `invite` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addInvite(Builder $builder) + { + $builder->macro('invite', function (Builder $builder) { + $builder->withUninvited(); + + return $builder->update(['invited_at' => Carbon::now()]); + }); + } + + /** + * Add the `uninvite` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addUninvite(Builder $builder) + { + $builder->macro('uninvite', function (Builder $builder) { + return $builder->update(['invited_at' => null]); + }); + } + + /** + * Add the `withUninvited` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithUninvited(Builder $builder) + { + $builder->macro('withUninvited', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the `withoutUninvited` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutUninvited(Builder $builder) + { + $builder->macro('withoutUninvited', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->whereNotNull('invited_at'); + }); + } + + /** + * Add the `onlyUninvited` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyUninvited(Builder $builder) + { + $builder->macro('onlyUninvited', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->whereNull('invited_at'); + }); + } +} diff --git a/src/Scopes/Classic/InvitedFlagScope.php b/src/Scopes/Classic/InvitedFlagScope.php new file mode 100644 index 0000000..1d13ec6 --- /dev/null +++ b/src/Scopes/Classic/InvitedFlagScope.php @@ -0,0 +1,127 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Scopes\Classic; + +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Scope; + +/** + * Class InvitedFlagScope. + * + * @package Cog\Flag\Scopes\Classic + */ +class InvitedFlagScope implements Scope +{ + /** + * All of the extensions to be added to the builder. + * + * @var array + */ + protected $extensions = ['Invite', 'Uninvite', 'WithUninvited', 'WithoutUninvited', 'OnlyUninvited']; + + /** + * 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) + { + if (method_exists($model, 'shouldApplyInvitedFlagScope') && !$model->shouldApplyInvitedFlagScope()) { + return $builder; + } + + return $builder->where('is_invited', 1); + } + + /** + * 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 `invite` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addInvite(Builder $builder) + { + $builder->macro('invite', function (Builder $builder) { + $builder->withUninvited(); + + return $builder->update(['is_invited' => 1]); + }); + } + + /** + * Add the `uninvite` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addUninvite(Builder $builder) + { + $builder->macro('uninvite', function (Builder $builder) { + return $builder->update(['is_invited' => 0]); + }); + } + + /** + * Add the `withUninvited` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithUninvited(Builder $builder) + { + $builder->macro('withUninvited', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the `withoutUninvited` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutUninvited(Builder $builder) + { + $builder->macro('withoutUninvited', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_invited', 1); + }); + } + + /** + * Add the `onlyUninvited` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyUninvited(Builder $builder) + { + $builder->macro('onlyUninvited', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_invited', 0); + }); + } +} diff --git a/src/Scopes/Inverse/EndedAtScope.php b/src/Scopes/Inverse/EndedAtScope.php new file mode 100644 index 0000000..02a07db --- /dev/null +++ b/src/Scopes/Inverse/EndedAtScope.php @@ -0,0 +1,128 @@ + + * + * 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 Carbon\Carbon; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Scope; + +/** + * Class EndedAtScope. + * + * @package Cog\Flag\Scopes\Inverse + */ +class EndedAtScope implements Scope +{ + /** + * All of the extensions to be added to the builder. + * + * @var array + */ + protected $extensions = ['Unend', 'End', 'WithEnded', 'WithoutEnded', 'OnlyEnded']; + + /** + * 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) + { + if (method_exists($model, 'shouldApplyEndedAtScope') && !$model->shouldApplyEndedAtScope()) { + return $builder; + } + + return $builder->whereNull('ended_at'); + } + + /** + * 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 `unend` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addUnend(Builder $builder) + { + $builder->macro('unend', function (Builder $builder) { + $builder->withEnded(); + + return $builder->update(['ended_at' => null]); + }); + } + + /** + * Add the `end` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addEnd(Builder $builder) + { + $builder->macro('end', function (Builder $builder) { + return $builder->update(['ended_at' => Carbon::now()]); + }); + } + + /** + * Add the `withEnded` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithEnded(Builder $builder) + { + $builder->macro('withEnded', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the `withoutEnded` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutEnded(Builder $builder) + { + $builder->macro('withoutEnded', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->whereNull('ended_at'); + }); + } + + /** + * Add the `onlyEnded` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyEnded(Builder $builder) + { + $builder->macro('onlyEnded', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->whereNotNull('ended_at'); + }); + } +} diff --git a/src/Scopes/Inverse/EndedFlagScope.php b/src/Scopes/Inverse/EndedFlagScope.php new file mode 100644 index 0000000..bcf836c --- /dev/null +++ b/src/Scopes/Inverse/EndedFlagScope.php @@ -0,0 +1,127 @@ + + * + * 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 EndedFlagScope. + * + * @package Cog\Flag\Scopes\Inverse + */ +class EndedFlagScope implements Scope +{ + /** + * All of the extensions to be added to the builder. + * + * @var array + */ + protected $extensions = ['Unend', 'End', 'WithEnded', 'WithoutEnded', 'OnlyEnded']; + + /** + * 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) + { + if (method_exists($model, 'shouldApplyEndedFlagScope') && !$model->shouldApplyEndedFlagScope()) { + return $builder; + } + + return $builder->where('is_ended', 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 `unend` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addUnend(Builder $builder) + { + $builder->macro('unend', function (Builder $builder) { + $builder->withEnded(); + + return $builder->update(['is_ended' => 0]); + }); + } + + /** + * Add the `end` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addEnd(Builder $builder) + { + $builder->macro('end', function (Builder $builder) { + return $builder->update(['is_ended' => 1]); + }); + } + + /** + * Add the `withEnded` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithEnded(Builder $builder) + { + $builder->macro('withEnded', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + /** + * Add the `withoutEnded` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutEnded(Builder $builder) + { + $builder->macro('withoutEnded', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_ended', 0); + }); + } + + /** + * Add the `onlyEnded` extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyEnded(Builder $builder) + { + $builder->macro('onlyEnded', function (Builder $builder) { + return $builder->withoutGlobalScope($this)->where('is_ended', 1); + }); + } +} diff --git a/src/Traits/Classic/HasInvitedAt.php b/src/Traits/Classic/HasInvitedAt.php new file mode 100644 index 0000000..8790227 --- /dev/null +++ b/src/Traits/Classic/HasInvitedAt.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Classic; + +/** + * Class HasInvitedAt. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasInvitedAt +{ + use HasInvitedAtHelpers; + use HasInvitedAtScope; +} diff --git a/src/Traits/Classic/HasInvitedAtHelpers.php b/src/Traits/Classic/HasInvitedAtHelpers.php new file mode 100644 index 0000000..0cf9a07 --- /dev/null +++ b/src/Traits/Classic/HasInvitedAtHelpers.php @@ -0,0 +1,90 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Classic; + +use Carbon\Carbon; + +/** + * Class HasInvitedAtHelpers. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasInvitedAtHelpers +{ + /** + * Set invited flag. + * + * @return static + */ + public function setInvitedFlag() + { + $this->invited_at = Carbon::now(); + + return $this; + } + + /** + * Unset invited flag. + * + * @return static + */ + public function unsetInvitedFlag() + { + $this->invited_at = null; + + return $this; + } + + /** + * If entity is invited. + * + * @return bool + */ + public function isInvited() + { + return !is_null($this->invited_at); + } + + /** + * If entity is uninvited. + * + * @return bool + */ + public function isUninvited() + { + return !$this->isInvited(); + } + + /** + * Mark entity as invited. + * + * @return void + */ + public function invite() + { + $this->setInvitedFlag()->save(); + + // :TODO: Fire an event here + } + + /** + * Mark entity as uninvited. + * + * @return void + */ + public function uninvite() + { + $this->unsetInvitedFlag()->save(); + + // :TODO: Fire an event here + } +} diff --git a/src/Traits/Classic/HasInvitedAtScope.php b/src/Traits/Classic/HasInvitedAtScope.php new file mode 100644 index 0000000..dff408d --- /dev/null +++ b/src/Traits/Classic/HasInvitedAtScope.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\Classic; + +use Cog\Flag\Scopes\Classic\InvitedAtScope; + +/** + * Class HasInvitedAtScope. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasInvitedAtScope +{ + /** + * Boot the HasInvitedAtScope for a model. + * + * @return void + */ + public static function bootHasInvitedAtScope() + { + static::addGlobalScope(new InvitedAtScope); + } +} diff --git a/src/Traits/Classic/HasInvitedFlag.php b/src/Traits/Classic/HasInvitedFlag.php new file mode 100644 index 0000000..c03fa98 --- /dev/null +++ b/src/Traits/Classic/HasInvitedFlag.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Classic; + +/** + * Class HasInvitedFlag. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasInvitedFlag +{ + use HasInvitedFlagHelpers; + use HasInvitedFlagScope; +} diff --git a/src/Traits/Classic/HasInvitedFlagHelpers.php b/src/Traits/Classic/HasInvitedFlagHelpers.php new file mode 100644 index 0000000..2141ee8 --- /dev/null +++ b/src/Traits/Classic/HasInvitedFlagHelpers.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Classic; + +/** + * Class HasInvitedFlagHelpers. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasInvitedFlagHelpers +{ + /** + * Set invited flag. + * + * @return static + */ + public function setInvitedFlag() + { + $this->is_invited = true; + + return $this; + } + + /** + * Unset invited flag. + * + * @return static + */ + public function unsetInvitedFlag() + { + $this->is_invited = false; + + return $this; + } + + /** + * If entity is invited. + * + * @return bool + */ + public function isInvited() + { + return (bool) $this->is_invited; + } + + /** + * If entity is uninvited. + * + * @return bool + */ + public function isUninvited() + { + return !$this->isInvited(); + } + + /** + * Mark entity as invited. + * + * @return void + */ + public function invite() + { + $this->setInvitedFlag()->save(); + + // :TODO: Fire an event here + } + + /** + * Mark entity as uninvited. + * + * @return void + */ + public function uninvite() + { + $this->unsetInvitedFlag()->save(); + + // :TODO: Fire an event here + } +} diff --git a/src/Traits/Classic/HasInvitedFlagScope.php b/src/Traits/Classic/HasInvitedFlagScope.php new file mode 100644 index 0000000..654177b --- /dev/null +++ b/src/Traits/Classic/HasInvitedFlagScope.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\Classic; + +use Cog\Flag\Scopes\Classic\InvitedFlagScope; + +/** + * Class HasInvitedFlagScope. + * + * @package Cog\Flag\Traits\Classic + */ +trait HasInvitedFlagScope +{ + /** + * Boot the HasInvitedFlagScope for a model. + * + * @return void + */ + public static function bootHasInvitedFlagScope() + { + static::addGlobalScope(new InvitedFlagScope); + } +} diff --git a/src/Traits/Inverse/HasEndedAt.php b/src/Traits/Inverse/HasEndedAt.php new file mode 100644 index 0000000..2b2308e --- /dev/null +++ b/src/Traits/Inverse/HasEndedAt.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Inverse; + +/** + * Class HasEndedAt. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasEndedAt +{ + use HasEndedAtHelpers; + use HasEndedAtScope; +} diff --git a/src/Traits/Inverse/HasEndedAtHelpers.php b/src/Traits/Inverse/HasEndedAtHelpers.php new file mode 100644 index 0000000..031d89f --- /dev/null +++ b/src/Traits/Inverse/HasEndedAtHelpers.php @@ -0,0 +1,90 @@ + + * + * 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 Carbon\Carbon; + +/** + * Class HasEndedAtHelpers. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasEndedAtHelpers +{ + /** + * Set end flag. + * + * @return static + */ + public function setEndedFlag() + { + $this->ended_at = Carbon::now(); + + return $this; + } + + /** + * Unset end flag. + * + * @return static + */ + public function unsetEndedFlag() + { + $this->ended_at = null; + + return $this; + } + + /** + * If entity is end. + * + * @return bool + */ + public function isEnded() + { + return !is_null($this->ended_at); + } + + /** + * If entity is opened. + * + * @return bool + */ + public function isUnended() + { + return !$this->isEnded(); + } + + /** + * Mark entity as end. + * + * @return void + */ + public function end() + { + $this->setEndedFlag()->save(); + + // :TODO: Fire an event here + } + + /** + * Mark entity as opened. + * + * @return void + */ + public function unend() + { + $this->unsetEndedFlag()->save(); + + // :TODO: Fire an event here + } +} diff --git a/src/Traits/Inverse/HasEndedAtScope.php b/src/Traits/Inverse/HasEndedAtScope.php new file mode 100644 index 0000000..62e9f9e --- /dev/null +++ b/src/Traits/Inverse/HasEndedAtScope.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\EndedAtScope; + +/** + * Class HasEndedAtScope. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasEndedAtScope +{ + /** + * Boot the HasEndedAtScope trait for a model. + * + * @return void + */ + public static function bootHasEndedAtScope() + { + static::addGlobalScope(new EndedAtScope); + } +} diff --git a/src/Traits/Inverse/HasEndedFlag.php b/src/Traits/Inverse/HasEndedFlag.php new file mode 100644 index 0000000..19734f9 --- /dev/null +++ b/src/Traits/Inverse/HasEndedFlag.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Inverse; + +/** + * Class HasEndedFlag. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasEndedFlag +{ + use HasEndedFlagHelpers; + use HasEndedFlagScope; +} diff --git a/src/Traits/Inverse/HasEndedFlagHelpers.php b/src/Traits/Inverse/HasEndedFlagHelpers.php new file mode 100644 index 0000000..a4703a5 --- /dev/null +++ b/src/Traits/Inverse/HasEndedFlagHelpers.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Flag\Traits\Inverse; + +/** + * Class HasEndedFlagHelpers. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasEndedFlagHelpers +{ + /** + * Set ended flag. + * + * @return static + */ + public function setEndedFlag() + { + $this->is_ended = true; + + return $this; + } + + /** + * Unset ended flag. + * + * @return static + */ + public function unsetEndedFlag() + { + $this->is_ended = false; + + return $this; + } + + /** + * If entity is ended. + * + * @return bool + */ + public function isEnded() + { + return (bool) $this->is_ended; + } + + /** + * If entity is opened. + * + * @return bool + */ + public function isUnended() + { + return !$this->isEnded(); + } + + /** + * Mark entity as ended. + * + * @return void + */ + public function end() + { + $this->setEndedFlag()->save(); + + // :TODO: Fire an event here + } + + /** + * Mark entity as opened. + * + * @return void + */ + public function unend() + { + $this->unsetEndedFlag()->save(); + + // :TODO: Fire an event here + } +} diff --git a/src/Traits/Inverse/HasEndedFlagScope.php b/src/Traits/Inverse/HasEndedFlagScope.php new file mode 100644 index 0000000..7620b9d --- /dev/null +++ b/src/Traits/Inverse/HasEndedFlagScope.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\EndedFlagScope; + +/** + * Class HasEndedFlagScope. + * + * @package Cog\Flag\Traits\Inverse + */ +trait HasEndedFlagScope +{ + /** + * Boot the HasEndedFlagScope trait for a model. + * + * @return void + */ + public static function bootHasEndedFlagScope() + { + static::addGlobalScope(new EndedFlagScope); + } +} diff --git a/tests/Stubs/Models/Classic/EntityWithInvitedAt.php b/tests/Stubs/Models/Classic/EntityWithInvitedAt.php new file mode 100644 index 0000000..3e7cd9d --- /dev/null +++ b/tests/Stubs/Models/Classic/EntityWithInvitedAt.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\Tests\Flag\Stubs\Models\Classic; + +use Cog\Flag\Traits\Classic\HasInvitedAt; +use Illuminate\Database\Eloquent\Model; + +/** + * Class EntityWithInvitedAt. + * + * @package Cog\Tests\Flag\Stubs\Models\Classic + */ +class EntityWithInvitedAt extends Model +{ + use HasInvitedAt; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'entity_with_invited_at'; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'name', + ]; + + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'invited_at' => 'datetime', + ]; +} diff --git a/tests/Stubs/Models/Classic/EntityWithInvitedAtUnapplied.php b/tests/Stubs/Models/Classic/EntityWithInvitedAtUnapplied.php new file mode 100644 index 0000000..2b87226 --- /dev/null +++ b/tests/Stubs/Models/Classic/EntityWithInvitedAtUnapplied.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Stubs\Models\Classic; + +/** + * Class EntityWithInvitedAtUnapplied. + * + * @package Cog\Tests\Flag\Stubs\Models\Classic + */ +class EntityWithInvitedAtUnapplied extends EntityWithInvitedAt +{ + /** + * Determine if InvitedAtScope should be applied by default. + * + * @return bool + */ + public function shouldApplyInvitedAtScope() + { + return false; + } +} diff --git a/tests/Stubs/Models/Classic/EntityWithInvitedFlag.php b/tests/Stubs/Models/Classic/EntityWithInvitedFlag.php new file mode 100644 index 0000000..863f0d0 --- /dev/null +++ b/tests/Stubs/Models/Classic/EntityWithInvitedFlag.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\Tests\Flag\Stubs\Models\Classic; + +use Cog\Flag\Traits\Classic\HasInvitedFlag; +use Illuminate\Database\Eloquent\Model; + +/** + * Class EntityWithInvitedFlag. + * + * @package Cog\Tests\Flag\Stubs\Models\Classic + */ +class EntityWithInvitedFlag extends Model +{ + use HasInvitedFlag; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'entity_with_invited_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_invited' => 'bool', + ]; +} diff --git a/tests/Stubs/Models/Classic/EntityWithInvitedFlagUnapplied.php b/tests/Stubs/Models/Classic/EntityWithInvitedFlagUnapplied.php new file mode 100644 index 0000000..0cbe327 --- /dev/null +++ b/tests/Stubs/Models/Classic/EntityWithInvitedFlagUnapplied.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Stubs\Models\Classic; + +/** + * Class EntityWithInvitedFlagUnapplied. + * + * @package Cog\Tests\Flag\Stubs\Models\Classic + */ +class EntityWithInvitedFlagUnapplied extends EntityWithInvitedFlag +{ + /** + * Determine if InvitedFlagScope should be applied by default. + * + * @return bool + */ + public function shouldApplyInvitedFlagScope() + { + return false; + } +} diff --git a/tests/Stubs/Models/Inverse/EntityWithEndedAt.php b/tests/Stubs/Models/Inverse/EntityWithEndedAt.php new file mode 100644 index 0000000..acc6056 --- /dev/null +++ b/tests/Stubs/Models/Inverse/EntityWithEndedAt.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\Tests\Flag\Stubs\Models\Inverse; + +use Cog\Flag\Traits\Inverse\HasEndedAt; +use Illuminate\Database\Eloquent\Model; + +/** + * Class EntityWithEndedAt. + * + * @package Cog\Tests\Flag\Stubs\Models\Inverse + */ +class EntityWithEndedAt extends Model +{ + use HasEndedAt; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'entity_with_ended_at'; + + /** + * The attributes that are mass assignable. + * + * @var array + */ + protected $fillable = [ + 'name', + ]; + + /** + * The attributes that should be cast to native types. + * + * @var array + */ + protected $casts = [ + 'ended_at' => 'datetime', + ]; +} diff --git a/tests/Stubs/Models/Inverse/EntityWithEndedAtUnapplied.php b/tests/Stubs/Models/Inverse/EntityWithEndedAtUnapplied.php new file mode 100644 index 0000000..5401f6f --- /dev/null +++ b/tests/Stubs/Models/Inverse/EntityWithEndedAtUnapplied.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Stubs\Models\Inverse; + +/** + * Class EntityWithEndedAtUnapplied. + * + * @package Cog\Tests\Flag\Stubs\Models\Inverse + */ +class EntityWithEndedAtUnapplied extends EntityWithEndedAt +{ + /** + * Determine if EndedAtScope should be applied by default. + * + * @return bool + */ + public function shouldApplyEndedAtScope() + { + return false; + } +} diff --git a/tests/Stubs/Models/Inverse/EntityWithEndedFlag.php b/tests/Stubs/Models/Inverse/EntityWithEndedFlag.php new file mode 100644 index 0000000..65dfce0 --- /dev/null +++ b/tests/Stubs/Models/Inverse/EntityWithEndedFlag.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\Tests\Flag\Stubs\Models\Inverse; + +use Cog\Flag\Traits\Inverse\HasEndedFlag; +use Illuminate\Database\Eloquent\Model; + +/** + * Class EntityWithEndedFlag. + * + * @package Cog\Tests\Flag\Stubs\Models\Inverse + */ +class EntityWithEndedFlag extends Model +{ + use HasEndedFlag; + + /** + * The table associated with the model. + * + * @var string + */ + protected $table = 'entity_with_ended_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_ended' => 'bool', + ]; +} diff --git a/tests/Stubs/Models/Inverse/EntityWithEndedFlagUnapplied.php b/tests/Stubs/Models/Inverse/EntityWithEndedFlagUnapplied.php new file mode 100644 index 0000000..a0bc611 --- /dev/null +++ b/tests/Stubs/Models/Inverse/EntityWithEndedFlagUnapplied.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Stubs\Models\Inverse; + +/** + * Class EntityWithEndedFlagUnapplied. + * + * @package Cog\Tests\Flag\Stubs\Models\Inverse + */ +class EntityWithEndedFlagUnapplied extends EntityWithEndedFlag +{ + /** + * Determine if EndedFlagScope should be applied by default. + * + * @return bool + */ + public function shouldApplyEndedFlagScope() + { + return false; + } +} diff --git a/tests/Unit/Scopes/Classic/InvitedAtScopeTest.php b/tests/Unit/Scopes/Classic/InvitedAtScopeTest.php new file mode 100644 index 0000000..97a5b8d --- /dev/null +++ b/tests/Unit/Scopes/Classic/InvitedAtScopeTest.php @@ -0,0 +1,128 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Scopes\Classic; + +use Carbon\Carbon; +use Cog\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedAt; +use Cog\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedAtUnapplied; +use Cog\Tests\Flag\TestCase; + +/** + * Class InvitedAtScopeTest. + * + * @package Cog\Tests\Flag\Unit\Scopes\Classic + */ +class InvitedAtScopeTest extends TestCase +{ + /** @test */ + public function it_can_get_only_invited() + { + factory(EntityWithInvitedAt::class, 3)->create([ + 'invited_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithInvitedAt::class, 2)->create([ + 'invited_at' => null, + ]); + + $entities = EntityWithInvitedAt::all(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_without_uninvited() + { + factory(EntityWithInvitedAt::class, 3)->create([ + 'invited_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithInvitedAt::class, 2)->create([ + 'invited_at' => null, + ]); + + $entities = EntityWithInvitedAt::withoutUninvited()->get(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_with_uninvited() + { + factory(EntityWithInvitedAt::class, 3)->create([ + 'invited_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithInvitedAt::class, 2)->create([ + 'invited_at' => null, + ]); + + $entities = EntityWithInvitedAt::withUninvited()->get(); + + $this->assertCount(5, $entities); + } + + /** @test */ + public function it_can_get_only_uninvited() + { + factory(EntityWithInvitedAt::class, 3)->create([ + 'invited_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithInvitedAt::class, 2)->create([ + 'invited_at' => null, + ]); + + $entities = EntityWithInvitedAt::onlyUninvited()->get(); + + $this->assertCount(2, $entities); + } + + /** @test */ + public function it_can_invite_model() + { + $model = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => null, + ]); + + EntityWithInvitedAt::where('id', $model->id)->invite(); + + $model = EntityWithInvitedAt::where('id', $model->id)->first(); + + $this->assertNotNull($model->invited_at); + } + + /** @test */ + public function it_can_uninvite_model() + { + $model = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => Carbon::now()->subDay(), + ]); + + EntityWithInvitedAt::where('id', $model->id)->uninvite(); + + $model = EntityWithInvitedAt::withUninvited()->where('id', $model->id)->first(); + + $this->assertNull($model->invited_at); + } + + /** @test */ + public function it_can_skip_apply() + { + factory(EntityWithInvitedAt::class, 3)->create([ + 'invited_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithInvitedAt::class, 2)->create([ + 'invited_at' => null, + ]); + + $entities = EntityWithInvitedAtUnapplied::all(); + + $this->assertCount(5, $entities); + } +} diff --git a/tests/Unit/Scopes/Classic/InvitedFlagScopeTest.php b/tests/Unit/Scopes/Classic/InvitedFlagScopeTest.php new file mode 100644 index 0000000..608d7cb --- /dev/null +++ b/tests/Unit/Scopes/Classic/InvitedFlagScopeTest.php @@ -0,0 +1,127 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Scopes\Classic; + +use Cog\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedFlag; +use Cog\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedFlagUnapplied; +use Cog\Tests\Flag\TestCase; + +/** + * Class InvitedFlagScopeTest. + * + * @package Cog\Tests\Flag\Unit\Scopes\Classic + */ +class InvitedFlagScopeTest extends TestCase +{ + /** @test */ + public function it_can_get_only_invited() + { + factory(EntityWithInvitedFlag::class, 3)->create([ + 'is_invited' => true, + ]); + factory(EntityWithInvitedFlag::class, 2)->create([ + 'is_invited' => false, + ]); + + $entities = EntityWithInvitedFlag::all(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_without_uninvited() + { + factory(EntityWithInvitedFlag::class, 3)->create([ + 'is_invited' => true, + ]); + factory(EntityWithInvitedFlag::class, 2)->create([ + 'is_invited' => false, + ]); + + $entities = EntityWithInvitedFlag::withoutUninvited()->get(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_with_uninvited() + { + factory(EntityWithInvitedFlag::class, 3)->create([ + 'is_invited' => true, + ]); + factory(EntityWithInvitedFlag::class, 2)->create([ + 'is_invited' => false, + ]); + + $entities = EntityWithInvitedFlag::withUninvited()->get(); + + $this->assertCount(5, $entities); + } + + /** @test */ + public function it_can_get_only_uninvited() + { + factory(EntityWithInvitedFlag::class, 3)->create([ + 'is_invited' => true, + ]); + factory(EntityWithInvitedFlag::class, 2)->create([ + 'is_invited' => false, + ]); + + $entities = EntityWithInvitedFlag::onlyUninvited()->get(); + + $this->assertCount(2, $entities); + } + + /** @test */ + public function it_can_invite_model() + { + $model = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => false, + ]); + + EntityWithInvitedFlag::where('id', $model->id)->invite(); + + $model = EntityWithInvitedFlag::where('id', $model->id)->first(); + + $this->assertTrue($model->is_invited); + } + + /** @test */ + public function it_can_uninvite_model() + { + $model = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => true, + ]); + + EntityWithInvitedFlag::where('id', $model->id)->uninvite(); + + $model = EntityWithInvitedFlag::withUninvited()->where('id', $model->id)->first(); + + $this->assertFalse($model->is_invited); + } + + /** @test */ + public function it_can_skip_apply() + { + factory(EntityWithInvitedFlag::class, 3)->create([ + 'is_invited' => true, + ]); + factory(EntityWithInvitedFlag::class, 2)->create([ + 'is_invited' => false, + ]); + + $entities = EntityWithInvitedFlagUnapplied::all(); + + $this->assertCount(5, $entities); + } +} diff --git a/tests/Unit/Scopes/Inverse/EndedAtScopeTest.php b/tests/Unit/Scopes/Inverse/EndedAtScopeTest.php new file mode 100644 index 0000000..ebc96f0 --- /dev/null +++ b/tests/Unit/Scopes/Inverse/EndedAtScopeTest.php @@ -0,0 +1,128 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Scopes\Inverse; + +use Carbon\Carbon; +use Cog\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedAt; +use Cog\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedAtUnapplied; +use Cog\Tests\Flag\TestCase; + +/** + * Class EndedAtScopeTest. + * + * @package Cog\Tests\Flag\Unit\Scopes\Inverse + */ +class EndedAtScopeTest extends TestCase +{ + /** @test */ + public function it_can_get_only_not_ended() + { + factory(EntityWithEndedAt::class, 2)->create([ + 'ended_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithEndedAt::class, 3)->create([ + 'ended_at' => null, + ]); + + $entities = EntityWithEndedAt::all(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_without_ended() + { + factory(EntityWithEndedAt::class, 2)->create([ + 'ended_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithEndedAt::class, 3)->create([ + 'ended_at' => null, + ]); + + $entities = EntityWithEndedAt::withoutEnded()->get(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_with_ended() + { + factory(EntityWithEndedAt::class, 2)->create([ + 'ended_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithEndedAt::class, 3)->create([ + 'ended_at' => null, + ]); + + $entities = EntityWithEndedAt::withEnded()->get(); + + $this->assertCount(5, $entities); + } + + /** @test */ + public function it_can_get_only_ended() + { + factory(EntityWithEndedAt::class, 2)->create([ + 'ended_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithEndedAt::class, 3)->create([ + 'ended_at' => null, + ]); + + $entities = EntityWithEndedAt::onlyEnded()->get(); + + $this->assertCount(2, $entities); + } + + /** @test */ + public function it_can_unend_model() + { + $model = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => Carbon::now()->subDay(), + ]); + + EntityWithEndedAt::where('id', $model->id)->unend(); + + $model = EntityWithEndedAt::where('id', $model->id)->first(); + + $this->assertNull($model->ended_at); + } + + /** @test */ + public function it_can_end_model() + { + $model = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => null, + ]); + + EntityWithEndedAt::where('id', $model->id)->end(); + + $model = EntityWithEndedAt::withEnded()->where('id', $model->id)->first(); + + $this->assertNotNull($model->ended_at); + } + + /** @test */ + public function it_can_skip_apply() + { + factory(EntityWithEndedAt::class, 3)->create([ + 'ended_at' => Carbon::now()->subDay(), + ]); + factory(EntityWithEndedAt::class, 2)->create([ + 'ended_at' => null, + ]); + + $entities = EntityWithEndedAtUnapplied::all(); + + $this->assertCount(5, $entities); + } +} diff --git a/tests/Unit/Scopes/Inverse/EndedFlagScopeTest.php b/tests/Unit/Scopes/Inverse/EndedFlagScopeTest.php new file mode 100644 index 0000000..60a3c54 --- /dev/null +++ b/tests/Unit/Scopes/Inverse/EndedFlagScopeTest.php @@ -0,0 +1,127 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Scopes\Inverse; + +use Cog\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedFlag; +use Cog\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedFlagUnapplied; +use Cog\Tests\Flag\TestCase; + +/** + * Class EndedFlagScopeTest. + * + * @package Cog\Tests\Flag\Unit\Scopes\Inverse + */ +class EndedFlagScopeTest extends TestCase +{ + /** @test */ + public function it_can_get_only_not_ended() + { + factory(EntityWithEndedFlag::class, 2)->create([ + 'is_ended' => true, + ]); + factory(EntityWithEndedFlag::class, 3)->create([ + 'is_ended' => false, + ]); + + $entities = EntityWithEndedFlag::all(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_without_ended() + { + factory(EntityWithEndedFlag::class, 2)->create([ + 'is_ended' => true, + ]); + factory(EntityWithEndedFlag::class, 3)->create([ + 'is_ended' => false, + ]); + + $entities = EntityWithEndedFlag::withoutEnded()->get(); + + $this->assertCount(3, $entities); + } + + /** @test */ + public function it_can_get_with_ended() + { + factory(EntityWithEndedFlag::class, 2)->create([ + 'is_ended' => true, + ]); + factory(EntityWithEndedFlag::class, 3)->create([ + 'is_ended' => false, + ]); + + $entities = EntityWithEndedFlag::withEnded()->get(); + + $this->assertCount(5, $entities); + } + + /** @test */ + public function it_can_get_only_ended() + { + factory(EntityWithEndedFlag::class, 2)->create([ + 'is_ended' => true, + ]); + factory(EntityWithEndedFlag::class, 3)->create([ + 'is_ended' => false, + ]); + + $entities = EntityWithEndedFlag::onlyEnded()->get(); + + $this->assertCount(2, $entities); + } + + /** @test */ + public function it_can_unend_model() + { + $model = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => true, + ]); + + EntityWithEndedFlag::where('id', $model->id)->unend(); + + $model = EntityWithEndedFlag::where('id', $model->id)->first(); + + $this->assertFalse($model->is_ended); + } + + /** @test */ + public function it_can_end_model() + { + $model = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => false, + ]); + + EntityWithEndedFlag::where('id', $model->id)->end(); + + $model = EntityWithEndedFlag::withEnded()->where('id', $model->id)->first(); + + $this->assertTrue($model->is_ended); + } + + /** @test */ + public function it_can_skip_apply() + { + factory(EntityWithEndedFlag::class, 3)->create([ + 'is_ended' => true, + ]); + factory(EntityWithEndedFlag::class, 2)->create([ + 'is_ended' => false, + ]); + + $entities = EntityWithEndedFlagUnapplied::all(); + + $this->assertCount(5, $entities); + } +} diff --git a/tests/Unit/Traits/Classic/HasInvitedAtHelpersTest.php b/tests/Unit/Traits/Classic/HasInvitedAtHelpersTest.php new file mode 100644 index 0000000..f4e937d --- /dev/null +++ b/tests/Unit/Traits/Classic/HasInvitedAtHelpersTest.php @@ -0,0 +1,102 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Traits\Classic; + +use Carbon\Carbon; +use Cog\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedAt; +use Cog\Tests\Flag\TestCase; + +/** + * Class HasInvitedAtHelpersTest. + * + * @package Cog\Tests\Flag\Unit\Traits\Classic + */ +class HasInvitedAtHelpersTest extends TestCase +{ + /** @test */ + public function it_can_set_invited_flag() + { + $entity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => null, + ]); + + $entity->setInvitedFlag(); + + $this->assertNotNull($entity->invited_at); + } + + /** @test */ + public function it_can_unset_invited_flag() + { + $entity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => Carbon::now(), + ]); + + $entity->unsetInvitedFlag(); + + $this->assertNull($entity->invited_at); + } + + /** @test */ + public function it_can_check_if_entity_is_invited() + { + $invitedEntity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => Carbon::now(), + ]); + + $uninvitedEntity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => null, + ]); + + $this->assertTrue($invitedEntity->isInvited()); + $this->assertFalse($uninvitedEntity->isInvited()); + } + + /** @test */ + public function it_can_check_if_entity_is_uninvited() + { + $invitedEntity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => Carbon::now(), + ]); + + $uninvitedEntity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => null, + ]); + + $this->assertFalse($invitedEntity->isUninvited()); + $this->assertTrue($uninvitedEntity->isUninvited()); + } + + /** @test */ + public function it_can_invite_entity() + { + $entity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => null, + ]); + + $entity->invite(); + + $this->assertNotNull($entity->invited_at); + } + + /** @test */ + public function it_can_uninvite_entity() + { + $entity = factory(EntityWithInvitedAt::class)->create([ + 'invited_at' => Carbon::now(), + ]); + + $entity->uninvite(); + + $this->assertNull($entity->invited_at); + } +} diff --git a/tests/Unit/Traits/Classic/HasInvitedFlagHelpersTest.php b/tests/Unit/Traits/Classic/HasInvitedFlagHelpersTest.php new file mode 100644 index 0000000..011c2f1 --- /dev/null +++ b/tests/Unit/Traits/Classic/HasInvitedFlagHelpersTest.php @@ -0,0 +1,101 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Traits\Classic; + +use Cog\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedFlag; +use Cog\Tests\Flag\TestCase; + +/** + * Class HasInvitedFlagHelpersTest. + * + * @package Cog\Tests\Flag\Unit\Traits\Classic + */ +class HasInvitedFlagHelpersTest extends TestCase +{ + /** @test */ + public function it_can_set_invited_flag() + { + $entity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => false, + ]); + + $entity->setInvitedFlag(); + + $this->assertTrue($entity->is_invited); + } + + /** @test */ + public function it_can_unset_invited_flag() + { + $entity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => true, + ]); + + $entity->unsetInvitedFlag(); + + $this->assertFalse($entity->is_invited); + } + + /** @test */ + public function it_can_check_if_entity_is_invited() + { + $invitedEntity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => true, + ]); + + $uninvitedEntity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => false, + ]); + + $this->assertTrue($invitedEntity->isInvited()); + $this->assertFalse($uninvitedEntity->isInvited()); + } + + /** @test */ + public function it_can_check_if_entity_is_uninvited() + { + $invitedEntity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => true, + ]); + + $uninvitedEntity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => false, + ]); + + $this->assertFalse($invitedEntity->isUninvited()); + $this->assertTrue($uninvitedEntity->isUninvited()); + } + + /** @test */ + public function it_can_invite_entity() + { + $entity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => false, + ]); + + $entity->invite(); + + $this->assertTrue($entity->is_invited); + } + + /** @test */ + public function it_can_uninvite_entity() + { + $entity = factory(EntityWithInvitedFlag::class)->create([ + 'is_invited' => true, + ]); + + $entity->uninvite(); + + $this->assertFalse($entity->is_invited); + } +} diff --git a/tests/Unit/Traits/Inverse/HasEndedAtHelpersTest.php b/tests/Unit/Traits/Inverse/HasEndedAtHelpersTest.php new file mode 100644 index 0000000..6da2575 --- /dev/null +++ b/tests/Unit/Traits/Inverse/HasEndedAtHelpersTest.php @@ -0,0 +1,102 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Traits\Classic; + +use Carbon\Carbon; +use Cog\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedAt; +use Cog\Tests\Flag\TestCase; + +/** + * Class HasEndedAtHelpersTest. + * + * @package Cog\Tests\Flag\Unit\Traits\Inverse + */ +class HasEndedAtHelpersTest extends TestCase +{ + /** @test */ + public function it_can_set_ended_flag() + { + $entity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => null, + ]); + + $entity->setEndedFlag(); + + $this->assertNotNull($entity->ended_at); + } + + /** @test */ + public function it_can_unset_ended_flag() + { + $entity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => Carbon::now(), + ]); + + $entity->unsetEndedFlag(); + + $this->assertNull($entity->ended_at); + } + + /** @test */ + public function it_can_check_if_entity_is_ended() + { + $endedEntity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => Carbon::now(), + ]); + + $unendedEntity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => null, + ]); + + $this->assertTrue($endedEntity->isEnded()); + $this->assertFalse($unendedEntity->isEnded()); + } + + /** @test */ + public function it_can_check_if_entity_is_unended() + { + $endedEntity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => Carbon::now(), + ]); + + $unendedEntity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => null, + ]); + + $this->assertFalse($endedEntity->isUnended()); + $this->assertTrue($unendedEntity->isUnended()); + } + + /** @test */ + public function it_can_end_entity() + { + $entity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => null, + ]); + + $entity->end(); + + $this->assertNotNull($entity->ended_at); + } + + /** @test */ + public function it_can_unend_entity() + { + $entity = factory(EntityWithEndedAt::class)->create([ + 'ended_at' => Carbon::now(), + ]); + + $entity->unend(); + + $this->assertNull($entity->ended_at); + } +} diff --git a/tests/Unit/Traits/Inverse/HasEndedFlagHelpersTest.php b/tests/Unit/Traits/Inverse/HasEndedFlagHelpersTest.php new file mode 100644 index 0000000..7594d95 --- /dev/null +++ b/tests/Unit/Traits/Inverse/HasEndedFlagHelpersTest.php @@ -0,0 +1,101 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Cog\Tests\Flag\Unit\Traits\Classic; + +use Cog\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedFlag; +use Cog\Tests\Flag\TestCase; + +/** + * Class HasEndedFlagHelpersTest. + * + * @package Cog\Tests\Flag\Unit\Traits\Inverse + */ +class HasEndedFlagHelpersTest extends TestCase +{ + /** @test */ + public function it_can_set_ended_flag() + { + $entity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => false, + ]); + + $entity->setEndedFlag(); + + $this->assertTrue($entity->is_ended); + } + + /** @test */ + public function it_can_unset_ended_flag() + { + $entity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => true, + ]); + + $entity->unsetEndedFlag(); + + $this->assertFalse($entity->is_ended); + } + + /** @test */ + public function it_can_check_if_entity_is_ended() + { + $endedEntity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => true, + ]); + + $unendedEntity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => false, + ]); + + $this->assertTrue($endedEntity->isEnded()); + $this->assertFalse($unendedEntity->isEnded()); + } + + /** @test */ + public function it_can_check_if_entity_is_unended() + { + $endedEntity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => true, + ]); + + $unendedEntity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => false, + ]); + + $this->assertFalse($endedEntity->isUnended()); + $this->assertTrue($unendedEntity->isUnended()); + } + + /** @test */ + public function it_can_end_entity() + { + $entity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => false, + ]); + + $entity->end(); + + $this->assertTrue($entity->is_ended); + } + + /** @test */ + public function it_can_unend_entity() + { + $entity = factory(EntityWithEndedFlag::class)->create([ + 'is_ended' => true, + ]); + + $entity->unend(); + + $this->assertFalse($entity->is_ended); + } +} diff --git a/tests/database/factories/EntityWithEndedAtFactory.php b/tests/database/factories/EntityWithEndedAtFactory.php new file mode 100644 index 0000000..5d6de39 --- /dev/null +++ b/tests/database/factories/EntityWithEndedAtFactory.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\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedAt::class, function (\Faker\Generator $faker) { + return [ + 'name' => $faker->word, + 'ended_at' => null, + ]; +}); diff --git a/tests/database/factories/EntityWithEndedFlagFactory.php b/tests/database/factories/EntityWithEndedFlagFactory.php new file mode 100644 index 0000000..13aa7a0 --- /dev/null +++ b/tests/database/factories/EntityWithEndedFlagFactory.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\Tests\Flag\Stubs\Models\Inverse\EntityWithEndedFlag::class, function (\Faker\Generator $faker) { + return [ + 'name' => $faker->word, + 'is_ended' => false, + ]; +}); diff --git a/tests/database/factories/EntityWithInvitedAtFactory.php b/tests/database/factories/EntityWithInvitedAtFactory.php new file mode 100644 index 0000000..91f7103 --- /dev/null +++ b/tests/database/factories/EntityWithInvitedAtFactory.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\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedAt::class, function (\Faker\Generator $faker) { + return [ + 'name' => $faker->word, + 'invited_at' => null, + ]; +}); diff --git a/tests/database/factories/EntityWithInvitedFlagFactory.php b/tests/database/factories/EntityWithInvitedFlagFactory.php new file mode 100644 index 0000000..32b270e --- /dev/null +++ b/tests/database/factories/EntityWithInvitedFlagFactory.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\Tests\Flag\Stubs\Models\Classic\EntityWithInvitedFlag::class, function (\Faker\Generator $faker) { + return [ + 'name' => $faker->word, + 'is_invited' => false, + ]; +}); diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_ended_at_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_ended_at_table.php new file mode 100644 index 0000000..4deeee5 --- /dev/null +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_ended_at_table.php @@ -0,0 +1,45 @@ + + * + * 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; +use Illuminate\Support\Facades\Schema; + +/** + * Class CreateEntityWithEndedAtTable. + */ +class CreateEntityWithEndedAtTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('entity_with_ended_at', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->timestamp('ended_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('entity_with_ended_at'); + } +} diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_ended_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_ended_flag_table.php new file mode 100644 index 0000000..7deca2c --- /dev/null +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_ended_flag_table.php @@ -0,0 +1,45 @@ + + * + * 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; +use Illuminate\Support\Facades\Schema; + +/** + * Class CreateEntityWithEndedFlagTable. + */ +class CreateEntityWithEndedFlagTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('entity_with_ended_flag', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->boolean('is_ended'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('entity_with_ended_flag'); + } +} diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_invited_at_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_invited_at_table.php new file mode 100644 index 0000000..37f3304 --- /dev/null +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_invited_at_table.php @@ -0,0 +1,45 @@ + + * + * 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; +use Illuminate\Support\Facades\Schema; + +/** + * Class CreateEntityWithInvitedAtTable. + */ +class CreateEntityWithInvitedAtTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('entity_with_invited_at', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->timestamp('invited_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('entity_with_invited_at'); + } +} diff --git a/tests/database/migrations/2016_09_25_182750_create_entity_with_invited_flag_table.php b/tests/database/migrations/2016_09_25_182750_create_entity_with_invited_flag_table.php new file mode 100644 index 0000000..344e68f --- /dev/null +++ b/tests/database/migrations/2016_09_25_182750_create_entity_with_invited_flag_table.php @@ -0,0 +1,45 @@ + + * + * 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; +use Illuminate\Support\Facades\Schema; + +/** + * Class CreateEntityWithInvitedFlagTable. + */ +class CreateEntityWithInvitedFlagTable extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::create('entity_with_invited_flag', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->boolean('is_invited'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('entity_with_invited_flag'); + } +}