Skip to content

Commit

Permalink
Laravel 10
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Feb 15, 2023
1 parent e3011c4 commit 47482cb
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ composer.lock
cghooks.lock

.php-cs-fixer.cache
.phpunit.cache/
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
},
"require-dev": {
"mockery/mockery": "^1.5",
"phpunit/phpunit": "^9.5|^10.0",
"orchestra/testbench": "^7.4",
"friendsofphp/php-cs-fixer": "^3.8"
"phpunit/phpunit": "^10.0",
"orchestra/testbench": "^8.0",
"friendsofphp/php-cs-fixer": "^3.8",
"brainmaestro/composer-git-hooks": "dev-master",
"laravel/pint": "^1.5"
},
"extra": {
"laravel": {
Expand Down Expand Up @@ -57,8 +59,8 @@
"cghooks update"
],
"cghooks": "vendor/bin/cghooks",
"check-style": "php-cs-fixer fix --using-cache=no --diff --dry-run --ansi",
"fix-style": "php-cs-fixer fix --using-cache=no --ansi",
"check-style": "vendor/bin/pint --test",
"fix-style": "vendor/bin/pint",
"test": "phpunit --colors=always"
},
"scripts-descriptions": {
Expand Down
2 changes: 1 addition & 1 deletion config/follow.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
/*
/*
* Use uuid as primary key.
*/
'uuids' => false,
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src/</directory>
Expand Down
3 changes: 3 additions & 0 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
class Event
{
public int|string $followable_id;

public int|string $followable_type;

public int|string $follower_id;

public int|string $user_id;

protected Followable $relation;
Expand Down
4 changes: 2 additions & 2 deletions src/FollowServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class FollowServiceProvider extends ServiceProvider
public function boot()
{
$this->publishes([
\dirname(__DIR__) . '/config/follow.php' => config_path('follow.php'),
\dirname(__DIR__).'/config/follow.php' => config_path('follow.php'),
], 'config');

$this->publishes([
\dirname(__DIR__) . '/migrations/' => database_path('migrations'),
\dirname(__DIR__).'/migrations/' => database_path('migrations'),
], 'migrations');
}
}
4 changes: 2 additions & 2 deletions src/Followable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

namespace Overtrue\LaravelFollow;

use function config;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Support\Str;
use Overtrue\LaravelFollow\Events\Followed;
use Overtrue\LaravelFollow\Events\Unfollowed;
use function config;

/**
* @property int|string $followable_id;
* @property int|string $followable_type;
* @property int|string $user_id;
*
* @method HasMany of(Model $model)
* @method HasMany followedBy(Model $model)
* @method HasMany withType(string $type)
Expand Down
14 changes: 4 additions & 10 deletions src/Traits/Followable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

namespace Overtrue\LaravelFollow\Traits;

use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use function config;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use Overtrue\LaravelFollow\Traits\Follower as Follower;
use function config;

/**
* @property Collection $followables
Expand All @@ -28,7 +22,7 @@ public function needsToApproveFollowRequests(): bool

public function rejectFollowRequestFrom(Model $follower): void
{
if (!in_array(Follower::class, \class_uses($follower))) {
if (! in_array(Follower::class, \class_uses($follower))) {
throw new \InvalidArgumentException('The model must use the Follower trait.');
}

Expand All @@ -37,7 +31,7 @@ public function rejectFollowRequestFrom(Model $follower): void

public function acceptFollowRequestFrom(Model $follower): void
{
if (!in_array(Follower::class, \class_uses($follower))) {
if (! in_array(Follower::class, \class_uses($follower))) {
throw new \InvalidArgumentException('The model must use the Follower trait.');
}

Expand All @@ -46,7 +40,7 @@ public function acceptFollowRequestFrom(Model $follower): void

public function isFollowedBy(Model $follower): bool
{
if (!in_array(Follower::class, \class_uses($follower))) {
if (! in_array(Follower::class, \class_uses($follower))) {
throw new \InvalidArgumentException('The model must use the Follower trait.');
}

Expand Down
27 changes: 12 additions & 15 deletions src/Traits/Follower.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@

namespace Overtrue\LaravelFollow\Traits;

use function abort_if;
use function class_uses;
use function collect;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Pagination\CursorPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Enumerable;
use Illuminate\Support\LazyCollection;
use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
use Overtrue\LaravelFollow\Traits\Followable;
use function abort_if;
use function class_uses;
use function collect;
use function in_array;
use InvalidArgumentException;
use function is_array;
use function iterator_to_array;
use JetBrains\PhpStorm\ArrayShape;

/**
* @property Collection $followings
*/
trait Follower
{
#[ArrayShape(['pending' => "mixed"])]
#[ArrayShape(['pending' => 'mixed'])]
public function follow(Model $followable): array
{
if ($followable->is($this)) {
throw new InvalidArgumentException('Cannot follow yourself.');
}

if (!in_array(Followable::class, class_uses($followable))) {
if (! in_array(Followable::class, class_uses($followable))) {
throw new InvalidArgumentException('The followable model must use the Followable trait.');
}

Expand All @@ -45,15 +42,15 @@ public function follow(Model $followable): array
'followable_id' => $followable->getKey(),
'followable_type' => $followable->getMorphClass(),
], [
'accepted_at' => $isPending ? null : now()
'accepted_at' => $isPending ? null : now(),
]);

return ['pending' => $isPending];
}

public function unfollow(Model $followable): void
{
if (!in_array(Followable::class, class_uses($followable))) {
if (! in_array(Followable::class, class_uses($followable))) {
throw new InvalidArgumentException('The followable model must use the Followable trait.');
}

Expand All @@ -67,7 +64,7 @@ public function toggleFollow(Model $followable): void

public function isFollowing(Model $followable): bool
{
if (!in_array(Followable::class, class_uses($followable))) {
if (! in_array(Followable::class, class_uses($followable))) {
throw new InvalidArgumentException('The followable model must use the Followable trait.');
}

Expand All @@ -84,7 +81,7 @@ public function isFollowing(Model $followable): bool

public function hasRequestedToFollow(Model $followable): bool
{
if (!in_array(\Overtrue\LaravelFollow\Traits\Followable::class, \class_uses($followable))) {
if (! in_array(\Overtrue\LaravelFollow\Traits\Followable::class, \class_uses($followable))) {
throw new InvalidArgumentException('The followable model must use the Followable trait.');
}

Expand Down Expand Up @@ -144,7 +141,7 @@ public function attachFollowStatus($followables, callable $resolver = null)
break;
}

abort_if(!($followables instanceof Enumerable), 422, 'Invalid $followables type.');
abort_if(! ($followables instanceof Enumerable), 422, 'Invalid $followables type.');

$followed = $this->followings()->get();

Expand Down
6 changes: 0 additions & 6 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Tests;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Overtrue\LaravelFollow\Events\Followed;
use Overtrue\LaravelFollow\Events\Unfollowed;
Expand Down Expand Up @@ -180,11 +179,6 @@ function () use ($user1, $user2, $user3, $user4) {
$this->assertSame(0, $sqls->count());
}

/**
* @param \Closure $callback
*
* @return \Illuminate\Support\Collection
*/
protected function getQueryLog(\Closure $callback): \Illuminate\Support\Collection
{
$sqls = \collect([]);
Expand Down
5 changes: 0 additions & 5 deletions tests/PrivacyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

namespace Tests;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Overtrue\LaravelFollow\Events\Followed;
use Overtrue\LaravelFollow\Events\Unfollowed;

class PrivacyTest extends TestCase
{
public function setUp(): void
Expand Down
9 changes: 4 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
/**
* Load package service provider.
*
* @param \Illuminate\Foundation\Application $app
*
* @param \Illuminate\Foundation\Application $app
* @return array
*/
protected function getPackageProviders($app)
Expand All @@ -21,7 +20,7 @@ protected function getPackageProviders($app)
/**
* Define environment setup.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
*/
protected function getEnvironmentSetUp($app)
{
Expand All @@ -37,7 +36,7 @@ protected function getEnvironmentSetUp($app)
protected function setUp(): void
{
parent::setUp();
$this->loadMigrationsFrom(__DIR__ . '/migrations');
$this->loadMigrationsFrom(dirname(__DIR__) . '/migrations');
$this->loadMigrationsFrom(__DIR__.'/migrations');
$this->loadMigrationsFrom(dirname(__DIR__).'/migrations');
}
}
3 changes: 0 additions & 3 deletions tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class User extends Model
'private' => 'boolean',
];

/**
* @return bool
*/
public function needsToApproveFollowRequests(): bool
{
return $this->private ?? false;
Expand Down

0 comments on commit 47482cb

Please sign in to comment.