Skip to content

Commit

Permalink
Laravel 8.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
hkp22 committed Sep 9, 2020
1 parent 6272788 commit 4cdeeec
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 172 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

106 changes: 52 additions & 54 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
],
"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
}
"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": "[email protected]"
}
],
"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
}
106 changes: 65 additions & 41 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,13 +25,7 @@ protected function setUp(): void

$this->publishPackageMigrations();

// $this->migratePackageTables();

// $this->migrateUnitTestTables();

$this->setUpDatabase();

$this->registerPackageFactories();
}

/**
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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
);
}
}
}
4 changes: 2 additions & 2 deletions tests/Unit/BanExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -26,4 +26,4 @@ public function it_can_throw_BannableTraitNotUsed_exception()
$response = $middleware->handle($request, function () {
});
}
}
}
6 changes: 3 additions & 3 deletions tests/Unit/BanMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand All @@ -44,4 +44,4 @@ public function it_cannot_redirect_to_non_banned_user()

$this->assertEquals($response, null);
}
}
}
34 changes: 17 additions & 17 deletions tests/Unit/BanModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
Expand Down Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -101,24 +101,24 @@ 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);

$bannableModels = Ban::whereBannable($user2)->get();
$this->assertCount(2, $bannableModels);
}
}
}
Loading

0 comments on commit 4cdeeec

Please sign in to comment.