diff --git a/CHANGELOG.md b/CHANGELOG.md index be939f4..584ac89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,16 @@ All notable changes to `laravel-bannable` will be documented in this file +## 2.4.0 - 2020-09-09 + +- Laravel 8.0 support + ## 2.3.0 - 2020-03-09 + - Laravel 7.0 support ## 2.2.0 - 2019-09-18 + - Laravel 6.0 support ## 2.1.0 - 2019-03-20 @@ -18,8 +24,6 @@ All notable changes to `laravel-bannable` will be documented in this file - package namespace changed from Hkp22 to Qirolab. - package vendor name changed from hkp22 to qirolab. Now to install this package required new command to run `composer require qirolab/laravel-bannable`. - ## 1.0.0 - 2018-07-10 - initial release - diff --git a/composer.json b/composer.json index bbb6905..4b7f499 100644 --- a/composer.json +++ b/composer.json @@ -1,55 +1,53 @@ { - "name": "qirolab/laravel-bannable", - "description": "Laravel bannable package is for blocking and banning Eloquent models.", - "license": "MIT", - "keywords": [ - "laravel", - "eloquent", - "user", - "ban", - "block", - "restrict", - "forbid" - ], - "authors": [ - { - "name": "Harish Kumar", - "email": "harish.pra22@gmail.com" - } - ], - "require": { - "php": ">=7.0", - "illuminate/database": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/events": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0", - "illuminate/support": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0" - }, - "autoload": { - "psr-4": { - "Qirolab\\Laravel\\Bannable\\": "src" - } - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "^2.10", - "mockery/mockery": "^1.0", - "orchestra/database": "~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0", - "orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0", - "phpunit/phpunit": "^7.5|^8.0|^9.0" - }, - "autoload-dev": { - "psr-4": { - "Qirolab\\Tests\\Laravel\\Bannable\\": "tests/" - } - }, - "scripts": { - "test": "vendor/bin/phpunit" - }, - "extra": { - "laravel": { - "providers": [ - "Qirolab\\Laravel\\Bannable\\BannableServiceProvider" - ] - } - }, - "minimum-stability": "dev", - "prefer-stable": true -} \ No newline at end of file + "name": "qirolab/laravel-bannable", + "description": "Laravel bannable package is for blocking and banning Eloquent models.", + "license": "MIT", + "keywords": [ + "laravel", + "eloquent", + "user", + "ban", + "block", + "restrict", + "forbid" + ], + "authors": [ + { + "name": "Harish Kumar", + "email": "harish.pra22@gmail.com" + } + ], + "require": { + "php": ">=7.0", + "illuminate/database": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0", + "illuminate/events": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0", + "illuminate/support": "5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0" + }, + "autoload": { + "psr-4": { + "Qirolab\\Laravel\\Bannable\\": "src" + } + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.16", + "orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0|^6.0", + "phpunit/phpunit": "^7.5|^8.0|^9.0" + }, + "autoload-dev": { + "psr-4": { + "Qirolab\\Tests\\Laravel\\Bannable\\": "tests/" + } + }, + "scripts": { + "test": "vendor/bin/phpunit" + }, + "extra": { + "laravel": { + "providers": [ + "Qirolab\\Laravel\\Bannable\\BannableServiceProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 23ba8b4..8e00b75 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,8 +2,12 @@ namespace Qirolab\Tests\Laravel\Bannable; +use Faker\Factory; +use Faker\Generator; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\File; use Orchestra\Testbench\TestCase as Orchestra; +use Qirolab\Laravel\Bannable\Models\Ban; use Qirolab\Tests\Laravel\Bannable\Stubs\Models\User; abstract class TestCase extends Orchestra @@ -21,13 +25,7 @@ protected function setUp(): void $this->publishPackageMigrations(); - // $this->migratePackageTables(); - - // $this->migrateUnitTestTables(); - $this->setUpDatabase(); - - $this->registerPackageFactories(); } /** @@ -50,7 +48,7 @@ protected function publishPackageMigrations() */ protected function destroyPackageMigrations() { - File::cleanDirectory(__DIR__.'/../vendor/orchestra/testbench-core/laravel/database/migrations'); + File::cleanDirectory(__DIR__ . '/../vendor/orchestra/testbench-core/laravel/database/migrations'); } /** @@ -95,46 +93,72 @@ private function setDefaultUserModel($app) */ protected function setUpDatabase() { - include_once __DIR__.'/../migrations/2018_06_25_000000_create_bans_table.php'; - include_once __DIR__.'/database/migrations/2018_06_25_000000__create_user_table.php'; + include_once __DIR__ . '/../migrations/2018_06_25_000000_create_bans_table.php'; + include_once __DIR__ . '/database/migrations/2018_06_25_000000__create_user_table.php'; (new \CreateBansTable())->up(); (new \CreateUserTable())->up(); } - /** - * Perform package database migrations. - * - * @return void - */ - // protected function migratePackageTables() - // { - // $this->loadMigrationsFrom([ - // '--realpath' => database_path('migrations'), - // ]); - // } - - // /** - // * Perform unit test database migrations. - // * - // * @return void - // */ - // protected function migrateUnitTestTables() - // { - // $this->loadMigrationsFrom([ - // '--realpath' => realpath(__DIR__.'/database/migrations'), - // ]); - // } + protected function faker($locale = null) + { + $locale = $locale ?? Factory::DEFAULT_LOCALE; - /** - * Register package related model factories. - * - * @return void - */ - private function registerPackageFactories() + if (isset($this->app) && $this->app->bound(Generator::class)) { + return $this->app->make(Generator::class, ['locale' => $locale]); + } + + return Factory::create($locale); + } + + public function factory($class, $attributes = [], $amount = null) { - $pathToFactories = realpath(__DIR__.'/database/factories'); + if (isset($amount) && is_int($amount)) { + $resource = []; - $this->withFactories($pathToFactories); + for ($i = 0; $i < $amount; $i++) { + $resource[] = (new $class)->forceCreate($attributes); + } + + return new Collection($resource); + } + + return (new $class)->forceCreate($attributes); + } + + public function createUser($class = null, $attributes = [], $amount = null) + { + $class = $class ?? User::class; + return $this->factory( + $class, + array_merge( + ['name' => $this->faker()->name], + $attributes + ), + $amount + ); + } + + public function createBan($attributes = [], $amount = null) + { + $bannable = $this->createUser(User::class); + // dd(array_merge( + // [ + // 'bannable_id' => $bannable->getKey(), + // 'bannable_type' => $bannable->getMorphClass(), + // ], + // $attributes + // )); + return $this->factory( + Ban::class, + array_merge( + [ + 'bannable_id' => $bannable->getKey(), + 'bannable_type' => $bannable->getMorphClass(), + ], + $attributes + ), + $amount + ); } -} +} \ No newline at end of file diff --git a/tests/Unit/BanExceptionTest.php b/tests/Unit/BanExceptionTest.php index c6ee8ed..8e37954 100644 --- a/tests/Unit/BanExceptionTest.php +++ b/tests/Unit/BanExceptionTest.php @@ -15,7 +15,7 @@ public function it_can_throw_BannableTraitNotUsed_exception() { $this->expectException(BannableTraitNotUsed::class); - $user = factory(UserWithoutBannableTrait::class)->create(); + $user = $this->createUser(UserWithoutBannableTrait::class); $this->actingAs($user); @@ -26,4 +26,4 @@ public function it_can_throw_BannableTraitNotUsed_exception() $response = $middleware->handle($request, function () { }); } -} +} \ No newline at end of file diff --git a/tests/Unit/BanMiddlewareTest.php b/tests/Unit/BanMiddlewareTest.php index 4e0ac81..c6050a5 100644 --- a/tests/Unit/BanMiddlewareTest.php +++ b/tests/Unit/BanMiddlewareTest.php @@ -12,7 +12,7 @@ class BanMiddlewareTest extends TestCase /** @test */ public function it_redirected_back_to_banned_user() { - $user = factory(User::class)->create(); + $user = $this->createUser(User::class); $user->ban(); @@ -31,7 +31,7 @@ public function it_redirected_back_to_banned_user() /** @test */ public function it_cannot_redirect_to_non_banned_user() { - $user = factory(User::class)->create(); + $user = $this->createUser(User::class); $this->actingAs($user); @@ -44,4 +44,4 @@ public function it_cannot_redirect_to_non_banned_user() $this->assertEquals($response, null); } -} +} \ No newline at end of file diff --git a/tests/Unit/BanModelTest.php b/tests/Unit/BanModelTest.php index b00d33f..35b63a3 100644 --- a/tests/Unit/BanModelTest.php +++ b/tests/Unit/BanModelTest.php @@ -37,9 +37,9 @@ public function expired_at_can_be_filled_and_no_MassAssignmentException_error() /** @test */ public function it_can_has_ban_creator() { - $bannedBy = factory(User::class)->create(); + $bannedBy = $this->createUser(User::class); - $ban = factory(Ban::class)->create([ + $ban = $this->createBan([ 'created_by_id' => $bannedBy->getKey(), 'created_by_type' => $bannedBy->getMorphClass(), ]); @@ -72,11 +72,11 @@ public function it_can_make_model_with_expire_relative_date() /** @test */ public function it_can_has_bannable_model() { - $user = factory(User::class)->create(); + $user = $this->createUser(User::class); - $ban = factory(Ban::class)->create([ - 'bannable_id' => $user->getKey(), - 'bannable_type' => $user->getMorphClass(), + $ban = $this->createBan([ + 'created_by_id' => $user->getKey(), + 'created_by_type' => $user->getMorphClass(), ]); $this->assertInstanceOf(User::class, $ban->bannable); @@ -85,13 +85,13 @@ public function it_can_has_bannable_model() /** @test */ public function it_can_delete_all_expired_bans() { - factory(Ban::class, 3)->create([ + $this->createBan([ 'expired_at' => Carbon::now()->subMonth(), - ]); + ], 3); - factory(Ban::class, 4)->create([ + $this->createBan([ 'expired_at' => Carbon::now()->addMonth(), - ]); + ], 4); Ban::deleteExpired(); @@ -101,19 +101,19 @@ public function it_can_delete_all_expired_bans() /** @test */ public function it_can_scope_bannable_models() { - $user1 = factory(User::class)->create(); + $user1 = $this->createUser(User::class); - factory(Ban::class, 4)->create([ + $this->createBan([ 'bannable_id' => $user1->getKey(), 'bannable_type' => $user1->getMorphClass(), - ]); + ], 4); - $user2 = factory(User::class)->create(); + $user2 = $this->createUser(User::class); - factory(Ban::class, 2)->create([ + $this->createBan([ 'bannable_id' => $user2->getKey(), 'bannable_type' => $user2->getMorphClass(), - ]); + ], 2); $bannableModels = Ban::whereBannable($user1)->get(); $this->assertCount(4, $bannableModels); @@ -121,4 +121,4 @@ public function it_can_scope_bannable_models() $bannableModels = Ban::whereBannable($user2)->get(); $this->assertCount(2, $bannableModels); } -} +} \ No newline at end of file diff --git a/tests/Unit/BanObserverTest.php b/tests/Unit/BanObserverTest.php index 868f4b1..a4e090e 100644 --- a/tests/Unit/BanObserverTest.php +++ b/tests/Unit/BanObserverTest.php @@ -3,7 +3,6 @@ namespace Qirolab\Tests\Laravel\Bannable\Unit; use Carbon\Carbon; -use Qirolab\Laravel\Bannable\Models\Ban; use Qirolab\Tests\Laravel\Bannable\Stubs\Models\User; use Qirolab\Tests\Laravel\Bannable\TestCase; @@ -12,7 +11,7 @@ class BanObserverTest extends TestCase /** @test */ public function it_can_set_banned_flag_to_owner_model_on_create() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -24,9 +23,9 @@ public function it_can_set_banned_flag_to_owner_model_on_create() /** @test */ public function it_can_set_created_by_id_and_type_before_creating() { - $bannedBy = factory(User::class)->create(); + $bannedBy = $this->createUser(User::class); - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -42,11 +41,11 @@ public function it_can_set_created_by_id_and_type_before_creating() /** @test */ public function it_can_unset_banned_flag_to_owner_model_on_delete() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => Carbon::now(), ]); - factory(Ban::class)->create([ + $this->createBan([ 'bannable_id' => $user->getKey(), 'bannable_type' => $user->getMorphClass(), ]); @@ -57,4 +56,4 @@ public function it_can_unset_banned_flag_to_owner_model_on_delete() $this->assertNull($user->fresh()->banned_at); } -} +} \ No newline at end of file diff --git a/tests/Unit/BannableTest.php b/tests/Unit/BannableTest.php index 014ada2..4c6f63f 100644 --- a/tests/Unit/BannableTest.php +++ b/tests/Unit/BannableTest.php @@ -12,9 +12,9 @@ class BannableTest extends TestCase /** @test */ public function model_can_has_related_ban() { - $user = factory(User::class)->create(); + $user = $this->createUser(User::class); - $ban = factory(Ban::class)->create([ + $ban = $this->createBan([ 'bannable_id' => $user->getKey(), 'bannable_type' => $user->getMorphClass(), ]); @@ -29,12 +29,12 @@ public function model_can_has_related_ban() /** @test */ public function model_can_has_many_related_bans() { - $user = factory(User::class)->create(); + $user = $this->createUser(User::class); - factory(Ban::class, 2)->create([ + $this->createBan([ 'bannable_id' => $user->getKey(), 'bannable_type' => $user->getMorphClass(), - ]); + ], 2); $this->assertCount(2, $user->bans); } @@ -42,7 +42,7 @@ public function model_can_has_many_related_bans() /** @test */ public function model_can_ban() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -54,11 +54,11 @@ public function model_can_ban() /** @test */ public function model_can_unban() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => Carbon::now(), ]); - factory(Ban::class)->create([ + $this->createBan([ 'bannable_id' => $user->getKey(), 'bannable_type' => $user->getMorphClass(), ]); @@ -77,11 +77,11 @@ public function model_can_unban() /** @test */ public function model_can_soft_delete_ban_on_unban() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => Carbon::now(), ]); - factory(Ban::class)->create([ + $this->createBan([ 'bannable_id' => $user->getKey(), 'bannable_type' => $user->getMorphClass(), ]); @@ -100,7 +100,7 @@ public function model_can_soft_delete_ban_on_unban() /** @test */ public function model_can_return_ban_model() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -112,7 +112,7 @@ public function model_can_return_ban_model() /** @test */ public function model_can_has_empty_banned_by() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -124,9 +124,9 @@ public function model_can_has_empty_banned_by() /** @test */ public function model_can_has_current_user_as_banned_by() { - $bannedBy = factory(User::class)->create(); + $bannedBy = $this->createUser(User::class); - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -142,7 +142,7 @@ public function model_can_has_current_user_as_banned_by() /** @test */ public function model_can_ban_via_ban_relation_create() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -156,7 +156,7 @@ public function model_can_ban_via_ban_relation_create() /** @test */ public function model_can_ban_with_comment() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -170,7 +170,7 @@ public function model_can_ban_with_comment() /** @test */ public function model_can_ban_with_expiration_date() { - $user = factory(User::class)->create([ + $user = $this->createUser(User::class, [ 'banned_at' => null, ]); @@ -180,4 +180,4 @@ public function model_can_ban_with_expiration_date() $this->assertEquals('2086-03-28 00:00:00', $ban->expired_at); } -} +} \ No newline at end of file diff --git a/tests/Unit/BannedScopeTest.php b/tests/Unit/BannedScopeTest.php index 4a43354..58c2ac3 100644 --- a/tests/Unit/BannedScopeTest.php +++ b/tests/Unit/BannedScopeTest.php @@ -12,13 +12,13 @@ class BannedScopeTest extends TestCase /** @test */ public function it_can_get_all_unbanned_models_by_default() { - factory(User::class, 2)->create([ + $this->createUser(User::class, [ 'banned_at' => Carbon::now()->subDay(), - ]); + ], 2); - factory(User::class, 3)->create([ + $this->createUser(User::class, [ 'banned_at' => null, - ]); + ], 3); $entities = User::all(); @@ -28,13 +28,13 @@ public function it_can_get_all_unbanned_models_by_default() /** @test */ public function it_can_get_models_without_banned() { - factory(User::class, 2)->create([ + $this->createUser(User::class, [ 'banned_at' => Carbon::now()->subDay(), - ]); + ], 2); - factory(User::class, 3)->create([ + $this->createUser(User::class, [ 'banned_at' => null, - ]); + ], 3); $entities = User::withoutBanned()->get(); @@ -44,13 +44,13 @@ public function it_can_get_models_without_banned() /** @test */ public function it_can_get_models_with_banned() { - factory(User::class, 2)->create([ + $this->createUser(User::class, [ 'banned_at' => Carbon::now()->subDay(), - ]); + ], 2); - factory(User::class, 3)->create([ + $this->createUser(User::class, [ 'banned_at' => null, - ]); + ], 3); $entities = User::withBanned()->get(); @@ -60,13 +60,13 @@ public function it_can_get_models_with_banned() /** @test */ public function it_can_get_only_banned_models() { - factory(User::class, 2)->create([ + $this->createUser(User::class, [ 'banned_at' => Carbon::now()->subDay(), - ]); + ], 2); - factory(User::class, 3)->create([ + $this->createUser(User::class, [ 'banned_at' => null, - ]); + ], 3); $entities = User::onlyBanned()->get(); @@ -76,16 +76,16 @@ public function it_can_get_only_banned_models() /** @test */ public function it_can_disable_banned_at_default_scope() { - factory(User::class, 3)->create([ + $this->createUser(User::class, [ 'banned_at' => Carbon::now()->subDay(), - ]); + ], 2); - factory(User::class, 2)->create([ + $this->createUser(User::class, [ 'banned_at' => null, - ]); + ], 3); $entities = UserModelWithDisabledBannedScope::all(); $this->assertCount(5, $entities); } -} +} \ No newline at end of file diff --git a/tests/Unit/ModelBannedEventTest.php b/tests/Unit/ModelBannedEventTest.php index ab17c1a..8db66e3 100644 --- a/tests/Unit/ModelBannedEventTest.php +++ b/tests/Unit/ModelBannedEventTest.php @@ -13,7 +13,7 @@ public function it_can_fire_event_on_helper_call() { $this->expectsEvents(ModelWasBanned::class); - $entity = factory(User::class)->create(); + $entity = $this->createUser(User::class);; $entity->ban(); } @@ -23,8 +23,8 @@ public function it_can_fire_event_on_relation_create() { $this->expectsEvents(ModelWasBanned::class); - $entity = factory(User::class)->create(); + $entity = $this->createUser(User::class);; $entity->bans()->create(); } -} +} \ No newline at end of file diff --git a/tests/Unit/ModelUnbannedEventTest.php b/tests/Unit/ModelUnbannedEventTest.php index 9896d58..2b3f335 100644 --- a/tests/Unit/ModelUnbannedEventTest.php +++ b/tests/Unit/ModelUnbannedEventTest.php @@ -3,7 +3,6 @@ namespace Qirolab\Tests\Laravel\Bannable\Unit; use Qirolab\Laravel\Bannable\Events\ModelWasUnbanned; -use Qirolab\Laravel\Bannable\Models\Ban; use Qirolab\Tests\Laravel\Bannable\TestCase; class ModelUnbannedEventTest extends TestCase @@ -13,7 +12,7 @@ public function it_can_fire_event_on_helper_call() { $this->expectsEvents(ModelWasUnbanned::class); - $ban = factory(Ban::class)->create(); + $ban = $this->createBan(); $ban->bannable->unban(); } @@ -23,8 +22,8 @@ public function it_can_fire_event_on_relation_delete() { $this->expectsEvents(ModelWasUnbanned::class); - $ban = factory(Ban::class)->create(); + $ban = $this->createBan(); $ban->delete(); } -} +} \ No newline at end of file