From 7f868ccbd2d9c3313f2ba53c24dbf83e7b8e2184 Mon Sep 17 00:00:00 2001 From: Harish Kumar Date: Wed, 20 Mar 2019 17:53:32 +0530 Subject: [PATCH] Laravel 5.8 compatibility --- .travis.yml | 15 +++++-- CHANGELOG.md | 19 +++++++++ CONTRIBUTING.md | 55 +++++++++++++++++++++++++ README.md | 9 ++-- composer.json | 8 ++-- src/BannableServiceProvider.php | 12 ------ src/Exceptions/BannableTraitNotUsed.php | 2 +- tests/TestCase.php | 44 +++++++++++++------- tests/Unit/BanMiddlewareTest.php | 2 +- tests/Unit/BanObserverTest.php | 8 +--- tests/Unit/BannableTest.php | 12 ++---- 11 files changed, 132 insertions(+), 54 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 CONTRIBUTING.md diff --git a/.travis.yml b/.travis.yml index b3aed70..fe4dcee 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,18 @@ language: php php: - 7.1 - 7.2 + - 7.3 + +env: + matrix: + - COMPOSER_FLAGS="" before_script: - - composer self-update - - composer install --prefer-dist --no-interaction + - travis_retry composer self-update + - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source + +script: + - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover -script: ./vendor/bin/phpunit \ No newline at end of file +after_script: + - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..adff434 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +All notable changes to `laravel-bannable` will be documented in this file + +## 2.1.0 - 2019-03-20 + +- Laravel 5.8 compatibility +- `forbidBannedUser` route middleware alias is removed. To protect routes, register `Qirolab\Laravel\Bannable\Middleware\ForbidBannedUser` middleware in `$routeMiddleware` array of `app/Http/Kernel.php` file. + +## 2.0.0 - 2018-12-31 + +- 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/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..b4ae1c4 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +Please read and understand the contribution guide before creating an issue or pull request. + +## Etiquette + +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. + +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. + +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. + +## Viability + +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. + +## Procedure + +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. + +## Requirements + +If the project maintainer has any additional requirements, you will find them listed here. + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + +**Happy coding**! diff --git a/README.md b/README.md index 9249909..225149b 100644 --- a/README.md +++ b/README.md @@ -175,9 +175,10 @@ On model entity ban `\Qirolab\Laravel\Bannable\Events\ModelWasBanned` event is f On model entity unban `\Qirolab\Laravel\Bannable\Events\ModelWasUnbanned` event is fired. ## Middleware -This package has `forbidBannedUser` route middleware to restrict banned users to go to protected routes. +To prevent banned users to go to protected routes `Qirolab\Laravel\Bannable\Middleware\ForbidBannedUser` middleware is created. -```php -Route::get('/dashboard', 'DashboardController@index')->middleware('forbidBannedUser'); -``` +Register it in $routeMiddleware array of app/Http/Kernel.php file: +protected $routeMiddleware = [ + 'isBannedUser' => \Qirolab\Laravel\Bannable\Middleware\ForbidBannedUser::class, +] diff --git a/composer.json b/composer.json index 09aede4..b002784 100644 --- a/composer.json +++ b/composer.json @@ -10,10 +10,10 @@ } ], "require": { - "php": "^5.6|^7.0", - "illuminate/database": "~5.1.20|~5.2|~5.3|~5.4|~5.5|~5.6", - "illuminate/events": "~5.1.20|~5.2|~5.3|~5.4|~5.5|~5.6", - "illuminate/support": "~5.1.20|~5.2|~5.3|~5.4|~5.5|~5.6" + "php" : ">=7.0", + "illuminate/database": "~5.1.20|~5.2|~5.3|~5.4|~5.5|~5.6|~5.7|~5.8", + "illuminate/events": "~5.1.20|~5.2|~5.3|~5.4|~5.5|~5.6|~5.7|~5.8", + "illuminate/support": "~5.1.20|~5.2|~5.3|~5.4|~5.5|~5.6|~5.7|~5.8" }, "autoload": { "psr-4": { diff --git a/src/BannableServiceProvider.php b/src/BannableServiceProvider.php index df65d52..188aa09 100644 --- a/src/BannableServiceProvider.php +++ b/src/BannableServiceProvider.php @@ -5,7 +5,6 @@ use Illuminate\Support\ServiceProvider; use Qirolab\Laravel\Bannable\Models\Ban; use Qirolab\Laravel\Bannable\Observers\BanObserver; -use Qirolab\Laravel\Bannable\Middleware\ForbidBannedUser; class BannableServiceProvider extends ServiceProvider { @@ -28,7 +27,6 @@ public function boot() */ public function register() { - $this->registerMiddleware(); } /** @@ -58,14 +56,4 @@ protected function registerObservers() { $this->app->make(Ban::class)->observe(new BanObserver); } - - /** - * Register middleware. - * - * @return void - */ - protected function registerMiddleware() - { - $this->app['router']->aliasMiddleware('forbidBannedUser', ForbidBannedUser::class); - } } diff --git a/src/Exceptions/BannableTraitNotUsed.php b/src/Exceptions/BannableTraitNotUsed.php index d339258..9e31d07 100644 --- a/src/Exceptions/BannableTraitNotUsed.php +++ b/src/Exceptions/BannableTraitNotUsed.php @@ -15,7 +15,7 @@ public function __construct($message = null, $code = 0, Exception $previous = nu $class = "'".get_class($message)."'"; } - $message = "'Hkp22\Laravel\Bannable\Traits\Bannable' trait is not used in {$class} model."; + $message = "'Qirolab\Laravel\Bannable\Traits\Bannable' trait is not used in {$class} model."; parent::__construct($message, $code, $previous); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 45a3615..6b67389 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -21,9 +21,11 @@ protected function setUp() $this->publishPackageMigrations(); - $this->migratePackageTables(); + // $this->migratePackageTables(); - $this->migrateUnitTestTables(); + // $this->migrateUnitTestTables(); + + $this->setUpDatabase(); $this->registerPackageFactories(); } @@ -87,28 +89,42 @@ private function setDefaultUserModel($app) } /** - * Perform package database migrations. + * Set up the database. * * @return void */ - protected function migratePackageTables() + protected function setUpDatabase() { - $this->loadMigrationsFrom([ - '--realpath' => database_path('migrations'), - ]); + 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 unit test database migrations. + * Perform package database migrations. * * @return void */ - protected function migrateUnitTestTables() - { - $this->loadMigrationsFrom([ - '--realpath' => realpath(__DIR__.'/database/migrations'), - ]); - } + // 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'), + // ]); + // } /** * Register package related model factories. diff --git a/tests/Unit/BanMiddlewareTest.php b/tests/Unit/BanMiddlewareTest.php index bf536ef..1ae42c1 100644 --- a/tests/Unit/BanMiddlewareTest.php +++ b/tests/Unit/BanMiddlewareTest.php @@ -16,7 +16,7 @@ public function it_redirected_back_to_banned_user() $user->ban(); - $this->actingAs($user->refresh()); + $this->actingAs($user->fresh()); $request = Request::create('/test', 'GET'); diff --git a/tests/Unit/BanObserverTest.php b/tests/Unit/BanObserverTest.php index 3851e15..468402e 100644 --- a/tests/Unit/BanObserverTest.php +++ b/tests/Unit/BanObserverTest.php @@ -18,9 +18,7 @@ public function it_can_set_banned_flag_to_owner_model_on_create() $user->bans()->create([]); - $user->refresh(); - - $this->assertNotNull($user->banned_at); + $this->assertNotNull($user->fresh()->banned_at); } /** @test */ @@ -57,8 +55,6 @@ public function it_can_unset_banned_flag_to_owner_model_on_delete() $user->unban(); - $user->refresh(); - - $this->assertNull($user->banned_at); + $this->assertNull($user->fresh()->banned_at); } } diff --git a/tests/Unit/BannableTest.php b/tests/Unit/BannableTest.php index 76822f3..4bb7b2e 100644 --- a/tests/Unit/BannableTest.php +++ b/tests/Unit/BannableTest.php @@ -48,9 +48,7 @@ public function model_can_ban() $user->ban(); - $user->refresh(); - - $this->assertNotNull($user->banned_at); + $this->assertNotNull($user->fresh()->banned_at); } /** @test */ @@ -92,10 +90,8 @@ public function model_can_soft_delete_ban_on_unban() $user->unban(); - $user->refresh(); - // without Trash - $this->assertCount(0, $user->bans); + $this->assertCount(0, $user->fresh()->bans); // with Trash $this->assertCount(1, $user->bans()->withTrashed()->get()); @@ -152,11 +148,9 @@ public function model_can_ban_via_ban_relation_create() $ban = $user->bans()->create(); - $user->refresh(); - $this->assertInstanceOf(Ban::class, $ban); - $this->assertTrue($user->isBanned()); + $this->assertTrue($user->fresh()->isBanned()); } /** @test */