From 363c5e5e055adc421106f4d9a96b4448c5e9a506 Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 20:20:19 +0545 Subject: [PATCH 01/11] feat: Adds Approved Scope --- src/Scope/ApprovedScope.php | 184 ++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/Scope/ApprovedScope.php diff --git a/src/Scope/ApprovedScope.php b/src/Scope/ApprovedScope.php new file mode 100644 index 0000000..b4542b6 --- /dev/null +++ b/src/Scope/ApprovedScope.php @@ -0,0 +1,184 @@ +whereNotNull($model->getQualifiedApprovedAtColumn())->whereNull($model->getQualifiedRejectedAtColumn()); + } + + /** + * Extend the query builder with the needed functions. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + public function extend(Builder $builder): void + { + foreach ($this->extensions as $extension) { + $this->{"add{$extension}"}($builder); + } + } + + /** + * Get the "approved at" column for the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return string + */ + protected function getApprovedAtColumn(Builder $builder): string + { + if (count((array)$builder->getQuery()->joins) > 0) { + return $builder->getModel()->getQualifiedApprovedAtColumn(); + } + + return $builder->getModel()->getQualifiedApprovedAtColumn(); + } + + /** + * Get the "rejected at" column for the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return string + */ + protected function getRejectedAtColumn(Builder $builder): string + { + if (count((array)$builder->getQuery()->joins) > 0) { + return $builder->getModel()->getQualifiedRejectedAtColumn(); + } + + return $builder->getModel()->getQualifiedRejectedAtColumn(); + } + + /** + * Add the with-pending extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithPending(Builder $builder): void + { + $builder->macro('withPending', function (Builder $builder) { + + return $builder->withoutGlobalScope($this)->whereNull( + $this->getRejectedAtColumn($builder) + ); + }); + } + + /** + * Add the without-pending extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyPending(Builder $builder): void + { + $builder->macro('OnlyPending', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this)->whereNull( + $model->getQualifiedApprovedAtColumn() + )->WhereNull( + $model->getQualifiedRejectedAtColumn() + ); + + return $builder; + }); + } + + /** + * Add the without-approved extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutApproved(Builder $builder): void + { + $builder->macro('withoutApproved', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this)->whereNull( + $model->getQualifiedApprovedAtColumn() + ); + + return $builder; + }); + } + + /** + * Add the with-rejected extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithRejected(Builder $builder): void + { + $builder->macro('withRejected', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this)->whereNotNull( + $model->getQualifiedRejectedAtColumn() + )->whereNotNull( + $model->getQualifiedApprovedAtColumn() + ); + + return $builder; + }); + } + + /** + * Add the only-rejected extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyRejected(Builder $builder): void + { + $builder->macro('onlyRejected', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this)->whereNotNull( + $model->getQualifiedRejectedAtColumn() + ); + + return $builder; + }); + } + + /** + * Add the with-all extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithAll(Builder $builder): void + { + $builder->macro('withAll', function (Builder $builder) { + return $builder->withoutGlobalScope($this); + }); + } + + + +} From b01227d6b5f2c989e67ac585a9621345fd460ce1 Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 20:34:20 +0545 Subject: [PATCH 02/11] feat: Adds CanBeApproved Trait --- src/Traits/CanBeApproved.php | 117 +++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/Traits/CanBeApproved.php diff --git a/src/Traits/CanBeApproved.php b/src/Traits/CanBeApproved.php new file mode 100644 index 0000000..3fa6315 --- /dev/null +++ b/src/Traits/CanBeApproved.php @@ -0,0 +1,117 @@ +casts[$this->getApprovedAtColumn()])) { + $this->casts[$this->getApprovedAtColumn()] = 'datetime'; + } + if (!isset($this->casts[$this->getRejectedAtColumn()])) { + $this->casts[$this->getRejectedAtColumn()] = 'datetime'; + } + + } + + /** + * Get the name of the "approved at" column. + * + * @return string + */ + public function getApprovedAtColumn(): string + { + return defined(static::class . '::APPROVED_AT') ? static::APPROVED_AT : 'approved_at'; + } + + /** + * Get the name of the "rejected at" column. + * + * @return string + */ + public function getRejectedAtColumn(): string + { + return defined(static::class . '::REJECTED_AT') ? static::REJECTED_AT : 'rejected_at'; + } + + + /** + * Get the fully qualified "approved at" column. + * + * @return string + */ + public function getQualifiedApprovedAtColumn(): string + { + return $this->qualifyColumn($this->getApprovedAtColumn()); + } + + /** + * Get the fully qualified "rejected at" column. + * + * @return string + */ + public function getQualifiedRejectedAtColumn(): string + { + return $this->qualifyColumn($this->getRejectedAtColumn()); + } + + /** + * Approve the model. + * + * @return bool + */ + public function setApproved(): bool + { + $this->{$this->getApprovedAtColumn()} = $this->freshTimestamp(); + $this->{$this->getRejectedAtColumn()} = null; + return $this->save(); + } + + /** + * Reject the model. + * + * @return bool + */ + + public function setRejected(): bool + { + $this->{$this->getRejectedAtColumn()} = $this->freshTimestamp(); + $this->{$this->getApprovedAtColumn()} = null; + + return $this->save(); + } + + /** + * Set the model as pending. + * + * @return bool + */ + public function setPending(): bool + { + $this->{$this->getRejectedAtColumn()} = null; + $this->{$this->getApprovedAtColumn()} = null; + + return $this->save(); + } + + +} From 0d4bf5199812925ae7b1f905b6ada3ff31b49daf Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 21:58:04 +0545 Subject: [PATCH 03/11] test: Approval Test --- tests/ApproveTest.php | 48 +++++++++++++++++++++++++++++ tests/Models/Article.php | 5 +-- tests/migrations/articles_table.php | 2 ++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/ApproveTest.php diff --git a/tests/ApproveTest.php b/tests/ApproveTest.php new file mode 100644 index 0000000..70c972b --- /dev/null +++ b/tests/ApproveTest.php @@ -0,0 +1,48 @@ +create(); + $article->setApproved(); + $this->assertTrue($article->approved_at != null); + $this->assertTrue($article->rejected_at == null); + } + + public function test_change_to_rejected() + { + $article = Article::factory()->create(); + $article->setRejected(); + $this->assertTrue($article->rejected_at != null); + $this->assertTrue($article->approved_at == null); + } + + public function test_change_to_pending() + { + $article = Article::factory()->create(['approved_at'=>now()]); + $article->setPending(); + $this->assertTrue($article->approved_at == null); + $this->assertTrue($article->rejected_at == null); + } + + public function test_count_approved() + { + Article::factory()->count(5)->create(); + Article::factory()->count(4)->create(['approved_at' => now()]); + Article::factory()->count(3)->create(['rejected_at' => now()]); + $this->assertEquals(9, Article::withPending()->count()); + $this->assertEquals(5, Article::onlyPending()->count()); + $this->assertEquals(8, Article::withoutApproved()->count()); + $this->assertEquals(7, Article::withRejected()->count()); + $this->assertEquals(3, Article::onlyRejected()->count()); + $this->assertEquals(12, Article::withAll()->count()); + $this->assertEquals(12, Article::count()); + } + + +} diff --git a/tests/Models/Article.php b/tests/Models/Article.php index e9d9fef..0bc0a6d 100644 --- a/tests/Models/Article.php +++ b/tests/Models/Article.php @@ -3,6 +3,7 @@ namespace AchyutN\LaravelHelpers\Tests\Models; use AchyutN\LaravelHelpers\Tests\Factories\ArticleFactory; +use AchyutN\LaravelHelpers\Traits\CanBeApproved; use AchyutN\LaravelHelpers\Traits\CanBeInactive; use AchyutN\LaravelHelpers\Traits\HasTheSlug; use Illuminate\Database\Eloquent\Factories\Factory; @@ -10,7 +11,7 @@ class Article extends Model { - use CanBeInactive, HasTheSlug; + use CanBeInactive, HasTheSlug, CanBeApproved; protected $guarded = []; protected static function factory(int $count = 1): Factory { @@ -20,4 +21,4 @@ protected static function factory(int $count = 1): Factory return ArticleFactory::new(); } } -} \ No newline at end of file +} diff --git a/tests/migrations/articles_table.php b/tests/migrations/articles_table.php index 1f2e2f9..965515d 100644 --- a/tests/migrations/articles_table.php +++ b/tests/migrations/articles_table.php @@ -19,6 +19,8 @@ public function up(): void $table->string('slug')->nullable(); $table->text("content"); $table->dateTime('inactive_at')->nullable(); + $table->dateTime('approved_at')->nullable(); + $table->dateTime('rejected_at')->nullable(); $table->timestamps(); }); } From 5e9b91bbfad8c666c1e6f051b63782f5468705fa Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 21:58:38 +0545 Subject: [PATCH 04/11] fix: onlyPending typo fixed --- src/Scope/ApprovedScope.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scope/ApprovedScope.php b/src/Scope/ApprovedScope.php index b4542b6..5d9f719 100644 --- a/src/Scope/ApprovedScope.php +++ b/src/Scope/ApprovedScope.php @@ -94,7 +94,7 @@ protected function addWithPending(Builder $builder): void */ protected function addOnlyPending(Builder $builder): void { - $builder->macro('OnlyPending', function (Builder $builder) { + $builder->macro('onlyPending', function (Builder $builder) { $model = $builder->getModel(); $builder->withoutGlobalScope($this)->whereNull( From df9876cac9eca79294990e37c78b71a79cfe84d1 Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:24:24 +0545 Subject: [PATCH 05/11] test: Approval Test --- tests/ActiveTest.php | 48 ++++++++++++++++++++++++++++++ tests/ApproveTest.php | 48 ------------------------------ tests/Factories/ActiveFactory.php | 28 +++++++++++++++++ tests/Models/Active.php | 24 +++++++++++++++ tests/Models/Article.php | 2 +- tests/migrations/actives_table.php | 34 +++++++++++++++++++++ 6 files changed, 135 insertions(+), 49 deletions(-) create mode 100644 tests/ActiveTest.php delete mode 100644 tests/ApproveTest.php create mode 100644 tests/Factories/ActiveFactory.php create mode 100644 tests/Models/Active.php create mode 100644 tests/migrations/actives_table.php diff --git a/tests/ActiveTest.php b/tests/ActiveTest.php new file mode 100644 index 0000000..74a9a60 --- /dev/null +++ b/tests/ActiveTest.php @@ -0,0 +1,48 @@ +create(); + $active->setApproved(); + $this->assertFalse($active->approved_at == null); + $this->assertTrue($active->rejected_at == null); + } + + public function test_change_to_rejected() + { + $active = Active::factory()->create(['approved_at' => now()]); + $active->setRejected(); + $this->assertTrue($active->rejected_at != null); + $this->assertTrue($active->approved_at == null); + } + + public function test_change_to_pending() + { + $active = Active::factory()->create(['approved_at' => now()]); + $active->setPending(); + $this->assertTrue($active->approved_at == null); + $this->assertTrue($active->rejected_at == null); + } + + public function test_count_pending() + { + Active::factory()->create(['approved_at' => now()]); + $this->assertEquals(1, Active::withPending()->count()); + } + + public function test_count_approved() + { + Active::factory()->create(['approved_at' => now()]); + $this->assertEquals(1, Active::count()); + } + + + +} diff --git a/tests/ApproveTest.php b/tests/ApproveTest.php deleted file mode 100644 index 70c972b..0000000 --- a/tests/ApproveTest.php +++ /dev/null @@ -1,48 +0,0 @@ -create(); - $article->setApproved(); - $this->assertTrue($article->approved_at != null); - $this->assertTrue($article->rejected_at == null); - } - - public function test_change_to_rejected() - { - $article = Article::factory()->create(); - $article->setRejected(); - $this->assertTrue($article->rejected_at != null); - $this->assertTrue($article->approved_at == null); - } - - public function test_change_to_pending() - { - $article = Article::factory()->create(['approved_at'=>now()]); - $article->setPending(); - $this->assertTrue($article->approved_at == null); - $this->assertTrue($article->rejected_at == null); - } - - public function test_count_approved() - { - Article::factory()->count(5)->create(); - Article::factory()->count(4)->create(['approved_at' => now()]); - Article::factory()->count(3)->create(['rejected_at' => now()]); - $this->assertEquals(9, Article::withPending()->count()); - $this->assertEquals(5, Article::onlyPending()->count()); - $this->assertEquals(8, Article::withoutApproved()->count()); - $this->assertEquals(7, Article::withRejected()->count()); - $this->assertEquals(3, Article::onlyRejected()->count()); - $this->assertEquals(12, Article::withAll()->count()); - $this->assertEquals(12, Article::count()); - } - - -} diff --git a/tests/Factories/ActiveFactory.php b/tests/Factories/ActiveFactory.php new file mode 100644 index 0000000..e6de324 --- /dev/null +++ b/tests/Factories/ActiveFactory.php @@ -0,0 +1,28 @@ + + */ + protected $model = Active::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->sentence, + ]; + } +} diff --git a/tests/Models/Active.php b/tests/Models/Active.php new file mode 100644 index 0000000..8f5cf9f --- /dev/null +++ b/tests/Models/Active.php @@ -0,0 +1,24 @@ + 1) { + return ActiveFactory::times($count); + } else { + return ActiveFactory::new(); + } + } +} diff --git a/tests/Models/Article.php b/tests/Models/Article.php index 0bc0a6d..058d300 100644 --- a/tests/Models/Article.php +++ b/tests/Models/Article.php @@ -11,7 +11,7 @@ class Article extends Model { - use CanBeInactive, HasTheSlug, CanBeApproved; + use CanBeInactive, HasTheSlug; protected $guarded = []; protected static function factory(int $count = 1): Factory { diff --git a/tests/migrations/actives_table.php b/tests/migrations/actives_table.php new file mode 100644 index 0000000..e6ae986 --- /dev/null +++ b/tests/migrations/actives_table.php @@ -0,0 +1,34 @@ +id(); + $table->text('title'); + $table->dateTime('approved_at')->nullable(); + $table->dateTime('rejected_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists("actives"); + } +}; From 8606b12a3b47da28daa2d7aacffe62ed378d2d69 Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:24:45 +0545 Subject: [PATCH 06/11] fix: withRejected typo fixed --- src/Scope/ApprovedScope.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scope/ApprovedScope.php b/src/Scope/ApprovedScope.php index 5d9f719..bb419c0 100644 --- a/src/Scope/ApprovedScope.php +++ b/src/Scope/ApprovedScope.php @@ -139,7 +139,7 @@ protected function addWithRejected(Builder $builder): void $builder->withoutGlobalScope($this)->whereNotNull( $model->getQualifiedRejectedAtColumn() - )->whereNotNull( + )->whereNull( $model->getQualifiedApprovedAtColumn() ); From aaf13daf1b17006f39a205b9c70a23a8283f751c Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:27:36 +0545 Subject: [PATCH 07/11] test: Approval Test --- tests/ActiveTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/ActiveTest.php b/tests/ActiveTest.php index 74a9a60..23f7852 100644 --- a/tests/ActiveTest.php +++ b/tests/ActiveTest.php @@ -43,6 +43,34 @@ public function test_count_approved() $this->assertEquals(1, Active::count()); } + public function test_count_rejected() + { + Active::factory()->create(['rejected_at' => now()]); + $this->assertEquals(1, Active::withRejected()->count()); + } + + public function test_count_all() + { + Active::factory()->create(['rejected_at' => now()]); + $this->assertEquals(1, Active::withAll()->count()); + } + + public function test_count_only_pending() + { + Active::factory()->create(['approved_at' => now()]); + $this->assertEquals(0, Active::onlyPending()->count()); + } + + public function test_count_only_approved() + { + Active::factory()->create(['approved_at' => now()]); + $this->assertEquals(1, Active::count()); + } + public function test_count_only_rejected() + { + Active::factory()->create(['rejected_at' => now()]); + $this->assertEquals(1, Active::onlyRejected()->count()); + } } From 1fcf19b53b732f6f63ed8d323cfe2dd6e447747a Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:28:21 +0545 Subject: [PATCH 08/11] fix: test className typo fixed --- tests/{ActiveTest.php => ApproveTest.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename tests/{ActiveTest.php => ApproveTest.php} (98%) diff --git a/tests/ActiveTest.php b/tests/ApproveTest.php similarity index 98% rename from tests/ActiveTest.php rename to tests/ApproveTest.php index 23f7852..afe25a7 100644 --- a/tests/ActiveTest.php +++ b/tests/ApproveTest.php @@ -4,7 +4,7 @@ use AchyutN\LaravelHelpers\Tests\Models\Active; -class ActiveTest extends BaseTestCase +class ApproveTest extends BaseTestCase { public function test_change_to_approved() From 853156dd421296cbaf4dff8adff14f039546daab Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:31:54 +0545 Subject: [PATCH 09/11] test: Approval Test --- tests/ApproveTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/ApproveTest.php b/tests/ApproveTest.php index afe25a7..9992bd6 100644 --- a/tests/ApproveTest.php +++ b/tests/ApproveTest.php @@ -40,35 +40,41 @@ public function test_count_pending() public function test_count_approved() { Active::factory()->create(['approved_at' => now()]); + Active::factory()->count(10)->create(); $this->assertEquals(1, Active::count()); } public function test_count_rejected() { + Active::factory()->count(10)->create(); Active::factory()->create(['rejected_at' => now()]); $this->assertEquals(1, Active::withRejected()->count()); } public function test_count_all() { + Active::factory()->count(10)->create(); Active::factory()->create(['rejected_at' => now()]); - $this->assertEquals(1, Active::withAll()->count()); + $this->assertEquals(11, Active::withAll()->count()); } public function test_count_only_pending() { + Active::factory()->count(10)->create(); Active::factory()->create(['approved_at' => now()]); - $this->assertEquals(0, Active::onlyPending()->count()); + $this->assertEquals(10, Active::onlyPending()->count()); } public function test_count_only_approved() { + Active::factory()->count(10)->create(); Active::factory()->create(['approved_at' => now()]); $this->assertEquals(1, Active::count()); } public function test_count_only_rejected() { + Active::factory()->count(10)->create(); Active::factory()->create(['rejected_at' => now()]); $this->assertEquals(1, Active::onlyRejected()->count()); } From 40bcd2ab1a788450ee3b80f5a127626b32718f65 Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:34:27 +0545 Subject: [PATCH 10/11] fix: model, migration and factory class name typo fixed --- tests/ApproveTest.php | 48 +++++++++---------- .../{ActiveFactory.php => ApproveFactory.php} | 6 +-- tests/Models/{Active.php => Approve.php} | 8 ++-- .../{actives_table.php => approves_table.php} | 4 +- 4 files changed, 33 insertions(+), 33 deletions(-) rename tests/Factories/{ActiveFactory.php => ApproveFactory.php} (79%) rename tests/Models/{Active.php => Approve.php} (68%) rename tests/migrations/{actives_table.php => approves_table.php} (85%) diff --git a/tests/ApproveTest.php b/tests/ApproveTest.php index 9992bd6..143a668 100644 --- a/tests/ApproveTest.php +++ b/tests/ApproveTest.php @@ -2,14 +2,14 @@ namespace AchyutN\LaravelHelpers\Tests; -use AchyutN\LaravelHelpers\Tests\Models\Active; +use AchyutN\LaravelHelpers\Tests\Models\Approve; class ApproveTest extends BaseTestCase { public function test_change_to_approved() { - $active = Active::factory()->create(); + $active = Approve::factory()->create(); $active->setApproved(); $this->assertFalse($active->approved_at == null); $this->assertTrue($active->rejected_at == null); @@ -17,7 +17,7 @@ public function test_change_to_approved() public function test_change_to_rejected() { - $active = Active::factory()->create(['approved_at' => now()]); + $active = Approve::factory()->create(['approved_at' => now()]); $active->setRejected(); $this->assertTrue($active->rejected_at != null); $this->assertTrue($active->approved_at == null); @@ -25,7 +25,7 @@ public function test_change_to_rejected() public function test_change_to_pending() { - $active = Active::factory()->create(['approved_at' => now()]); + $active = Approve::factory()->create(['approved_at' => now()]); $active->setPending(); $this->assertTrue($active->approved_at == null); $this->assertTrue($active->rejected_at == null); @@ -33,50 +33,50 @@ public function test_change_to_pending() public function test_count_pending() { - Active::factory()->create(['approved_at' => now()]); - $this->assertEquals(1, Active::withPending()->count()); + Approve::factory()->create(['approved_at' => now()]); + $this->assertEquals(1, Approve::withPending()->count()); } public function test_count_approved() { - Active::factory()->create(['approved_at' => now()]); - Active::factory()->count(10)->create(); - $this->assertEquals(1, Active::count()); + Approve::factory()->create(['approved_at' => now()]); + Approve::factory()->count(10)->create(); + $this->assertEquals(1, Approve::count()); } public function test_count_rejected() { - Active::factory()->count(10)->create(); - Active::factory()->create(['rejected_at' => now()]); - $this->assertEquals(1, Active::withRejected()->count()); + Approve::factory()->count(10)->create(); + Approve::factory()->create(['rejected_at' => now()]); + $this->assertEquals(1, Approve::withRejected()->count()); } public function test_count_all() { - Active::factory()->count(10)->create(); - Active::factory()->create(['rejected_at' => now()]); - $this->assertEquals(11, Active::withAll()->count()); + Approve::factory()->count(10)->create(); + Approve::factory()->create(['rejected_at' => now()]); + $this->assertEquals(11, Approve::withAll()->count()); } public function test_count_only_pending() { - Active::factory()->count(10)->create(); - Active::factory()->create(['approved_at' => now()]); - $this->assertEquals(10, Active::onlyPending()->count()); + Approve::factory()->count(10)->create(); + Approve::factory()->create(['approved_at' => now()]); + $this->assertEquals(10, Approve::onlyPending()->count()); } public function test_count_only_approved() { - Active::factory()->count(10)->create(); - Active::factory()->create(['approved_at' => now()]); - $this->assertEquals(1, Active::count()); + Approve::factory()->count(10)->create(); + Approve::factory()->create(['approved_at' => now()]); + $this->assertEquals(1, Approve::count()); } public function test_count_only_rejected() { - Active::factory()->count(10)->create(); - Active::factory()->create(['rejected_at' => now()]); - $this->assertEquals(1, Active::onlyRejected()->count()); + Approve::factory()->count(10)->create(); + Approve::factory()->create(['rejected_at' => now()]); + $this->assertEquals(1, Approve::onlyRejected()->count()); } } diff --git a/tests/Factories/ActiveFactory.php b/tests/Factories/ApproveFactory.php similarity index 79% rename from tests/Factories/ActiveFactory.php rename to tests/Factories/ApproveFactory.php index e6de324..5e643ac 100644 --- a/tests/Factories/ActiveFactory.php +++ b/tests/Factories/ApproveFactory.php @@ -2,17 +2,17 @@ namespace AchyutN\LaravelHelpers\Tests\Factories; -use AchyutN\LaravelHelpers\Tests\Models\Active; +use AchyutN\LaravelHelpers\Tests\Models\Approve; use Illuminate\Database\Eloquent\Factories\Factory; -class ActiveFactory extends Factory +class ApproveFactory extends Factory { /** * The name of the factory's corresponding model. * * @var class-string<\Illuminate\Database\Eloquent\Model> */ - protected $model = Active::class; + protected $model = Approve::class; /** * Define the model's default state. diff --git a/tests/Models/Active.php b/tests/Models/Approve.php similarity index 68% rename from tests/Models/Active.php rename to tests/Models/Approve.php index 8f5cf9f..31040b9 100644 --- a/tests/Models/Active.php +++ b/tests/Models/Approve.php @@ -2,12 +2,12 @@ namespace AchyutN\LaravelHelpers\Tests\Models; -use AchyutN\LaravelHelpers\Tests\Factories\ActiveFactory; +use AchyutN\LaravelHelpers\Tests\Factories\ApproveFactory; use AchyutN\LaravelHelpers\Traits\CanBeApproved; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Database\Eloquent\Model; -class Active extends Model +class Approve extends Model { use CanBeApproved; @@ -16,9 +16,9 @@ class Active extends Model protected static function factory(int $count = 1): Factory { if ($count && $count > 1) { - return ActiveFactory::times($count); + return ApproveFactory::times($count); } else { - return ActiveFactory::new(); + return ApproveFactory::new(); } } } diff --git a/tests/migrations/actives_table.php b/tests/migrations/approves_table.php similarity index 85% rename from tests/migrations/actives_table.php rename to tests/migrations/approves_table.php index e6ae986..ec2e642 100644 --- a/tests/migrations/actives_table.php +++ b/tests/migrations/approves_table.php @@ -13,7 +13,7 @@ */ public function up(): void { - Schema::create("actives", function (Blueprint $table) { + Schema::create("approves", function (Blueprint $table) { $table->id(); $table->text('title'); $table->dateTime('approved_at')->nullable(); @@ -29,6 +29,6 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists("actives"); + Schema::dropIfExists("approves"); } }; From 4af94c8a06a3a52bc30c71563f852062aeca5e4d Mon Sep 17 00:00:00 2001 From: Dipesh79 Date: Wed, 7 Feb 2024 22:48:40 +0545 Subject: [PATCH 11/11] test: Approve And Active Test --- tests/ApproveActiveTest.php | 70 +++++++++++++++++++++++ tests/Factories/ApproveActiveFactory.php | 28 +++++++++ tests/Models/ApproveActive.php | 27 +++++++++ tests/migrations/approve_active_table.php | 35 ++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 tests/ApproveActiveTest.php create mode 100644 tests/Factories/ApproveActiveFactory.php create mode 100644 tests/Models/ApproveActive.php create mode 100644 tests/migrations/approve_active_table.php diff --git a/tests/ApproveActiveTest.php b/tests/ApproveActiveTest.php new file mode 100644 index 0000000..8158688 --- /dev/null +++ b/tests/ApproveActiveTest.php @@ -0,0 +1,70 @@ +create(); + $approve_active->setApproved(); + $this->assertFalse($approve_active->approved_at == null); + $this->assertTrue($approve_active->rejected_at == null); + } + + public function test_active_count() + { + ApproveActive::factory(10)->create(['rejected_at' => now()]); + $this->assertEquals(0, ApproveActive::count()); + } + + public function test_active_and_approved_count() + { + ApproveActive::factory(10)->create(['approved_at' => now()]); + $this->assertEquals(10, ApproveActive::count()); + } + + public function test_active_and_pending_count() + { + ApproveActive::factory(10)->create(); + $this->assertEquals(0, ApproveActive::count()); + } + + public function test_active_and_rejected_count() + { + ApproveActive::factory(10)->create(['rejected_at' => now()]); + $this->assertEquals(0, ApproveActive::count()); + } + + public function test_inactive_and_approved_count() + { + ApproveActive::factory(10)->create(['approved_at' => now(), 'inactive_at' => now()]); + $this->assertEquals(0, ApproveActive::count()); + } + + public function test_active_and_change_to_approved() + { + $approve_active = ApproveActive::factory()->create(); + $approve_active->setApproved(); + $this->assertEquals(1, ApproveActive::count()); + } + + public function test_inactive_and_change_to_approved() + { + $approve_active = ApproveActive::factory()->create(['inactive_at'=>now()]); + $approve_active->setApproved(); + $this->assertEquals(0, ApproveActive::count()); + } + + public function test_approved_and_change_to_active() + { + $approve_active = ApproveActive::factory()->create(['approved_at'=>now(),'inactive_at'=>now()]); + $approve_active->setActive(); + $this->assertEquals(1, ApproveActive::count()); + } + +} diff --git a/tests/Factories/ApproveActiveFactory.php b/tests/Factories/ApproveActiveFactory.php new file mode 100644 index 0000000..1dd3d9f --- /dev/null +++ b/tests/Factories/ApproveActiveFactory.php @@ -0,0 +1,28 @@ + + */ + protected $model = ApproveActive::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->sentence, + ]; + } +} diff --git a/tests/Models/ApproveActive.php b/tests/Models/ApproveActive.php new file mode 100644 index 0000000..386ef73 --- /dev/null +++ b/tests/Models/ApproveActive.php @@ -0,0 +1,27 @@ + 1) { + return ApproveActiveFactory::times($count); + } else { + return ApproveActiveFactory::new(); + } + } +} diff --git a/tests/migrations/approve_active_table.php b/tests/migrations/approve_active_table.php new file mode 100644 index 0000000..de8357c --- /dev/null +++ b/tests/migrations/approve_active_table.php @@ -0,0 +1,35 @@ +id(); + $table->text('title'); + $table->dateTime('inactive_at')->nullable(); + $table->dateTime('approved_at')->nullable(); + $table->dateTime('rejected_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::dropIfExists("approve_active"); + } +};