Skip to content

Commit 8db61e6

Browse files
authored
Support subscribable for subscriber (#20)
1 parent 3fda4a9 commit 8db61e6

File tree

7 files changed

+106
-63
lines changed

7 files changed

+106
-63
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ $user->hasSubscribed($channel);
6262
$user->hasNotSubscribed($channel);
6363

6464
// Get subscribed info
65-
$user->subscriptions()->count();
65+
$user->subscriberSubscriptions()->count();
6666

6767
// with type
68-
$user->subscriptions()->withType(Channel::class)->count();
68+
$user->subscriberSubscriptions()->withType(Channel::class)->count();
6969

7070
// get subscribed channels
7171
Channel::query()->whereSubscribedBy($user)->get();

ecs.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
$parameters->set(
1313
Option::SETS,
1414
[
15-
SetList::PHP_70,
16-
SetList::PHP_71,
1715
SetList::CLEAN_CODE,
1816
SetList::COMMON,
1917
SetList::PSR_12,

src/Concerns/Subscribable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use function is_a;
1212

1313
/**
14-
* @property-read \Illuminate\Database\Eloquent\Collection|\LaravelInteraction\Subscribe\Subscription[] $subscriptions
14+
* @property-read \Illuminate\Database\Eloquent\Collection|\LaravelInteraction\Subscribe\Subscription[] $subscribableSubscriptions
1515
* @property-read \Illuminate\Database\Eloquent\Collection|\LaravelInteraction\Subscribe\Concerns\Subscriber[] $subscribers
1616
* @property-read int|null $subscribers_count
1717
*
@@ -35,7 +35,7 @@ public function isSubscribedBy(Model $user): bool
3535
return $this->subscribers->contains($user);
3636
}
3737

38-
return ($this->relationLoaded('subscriptions') ? $this->subscriptions : $this->subscriptions())
38+
return ($this->relationLoaded('subscribableSubscriptions') ? $this->subscribableSubscriptions : $this->subscribableSubscriptions())
3939
->where(config('subscribe.column_names.user_foreign_key'), $user->getKey())->count() > 0;
4040
}
4141

@@ -47,7 +47,7 @@ public function isNotSubscribedBy(Model $user): bool
4747
/**
4848
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
4949
*/
50-
public function subscriptions(): MorphMany
50+
public function subscribableSubscriptions(): MorphMany
5151
{
5252
return $this->morphMany(config('subscribe.models.subscription'), 'subscribable');
5353
}

src/Concerns/Subscriber.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Illuminate\Database\Eloquent\Relations\MorphToMany;
1010

1111
/**
12-
* @property-read \Illuminate\Database\Eloquent\Collection|\LaravelInteraction\Subscribe\Subscription[] $subscriptions
13-
* @property-read int|null $subscriptions_count
12+
* @property-read \Illuminate\Database\Eloquent\Collection|\LaravelInteraction\Subscribe\Subscription[] $subscriberSubscriptions
13+
* @property-read int|null $subscriber_subscriptions_count
1414
*/
1515
trait Subscriber
1616
{
@@ -57,7 +57,7 @@ public function toggleSubscribe(Model $object): void
5757
*/
5858
public function hasSubscribed(Model $object): bool
5959
{
60-
return ($this->relationLoaded('subscriptions') ? $this->subscriptions : $this->subscriptions())
60+
return ($this->relationLoaded('subscriberSubscriptions') ? $this->subscriberSubscriptions : $this->subscriberSubscriptions())
6161
->where('subscribable_id', $object->getKey())
6262
->where('subscribable_type', $object->getMorphClass())
6363
->count() > 0;
@@ -71,7 +71,7 @@ public function hasNotSubscribed(Model $object): bool
7171
/**
7272
* @return \Illuminate\Database\Eloquent\Relations\HasMany
7373
*/
74-
public function subscriptions(): HasMany
74+
public function subscriberSubscriptions(): HasMany
7575
{
7676
return $this->hasMany(config('subscribe.models.subscription'), config('subscribe.column_names.user_foreign_key'), $this->getKeyName());
7777
}

tests/Concerns/SubscribableTest.php

Lines changed: 92 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,43 @@
1111

1212
class SubscribableTest extends TestCase
1313
{
14-
public function testSubscriptions(): void
14+
public function modelClasses(): array
15+
{
16+
return[
17+
[Channel::class],
18+
[User::class],
19+
];
20+
}
21+
22+
/**
23+
* @dataProvider modelClasses
24+
*
25+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
26+
*/
27+
public function testSubscriptions(string $modelClass): void
1528
{
1629
$user = User::query()->create();
17-
$channel = Channel::query()->create();
18-
$user->subscribe($channel);
19-
self::assertSame(1, $channel->subscriptions()->count());
20-
self::assertSame(1, $channel->subscriptions->count());
30+
$model = $modelClass::query()->create();
31+
$user->subscribe($model);
32+
self::assertSame(1, $model->subscribableSubscriptions()->count());
33+
self::assertSame(1, $model->subscribableSubscriptions->count());
2134
}
2235

23-
public function testSubscribersCount(): void
36+
/**
37+
* @dataProvider modelClasses
38+
*
39+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
40+
*/
41+
public function testSubscribersCount(string $modelClass): void
2442
{
2543
$user = User::query()->create();
26-
$channel = Channel::query()->create();
27-
$user->subscribe($channel);
28-
self::assertSame(1, $channel->subscribersCount());
29-
$user->unsubscribe($channel);
30-
self::assertSame(1, $channel->subscribersCount());
31-
$channel->loadCount('subscribers');
32-
self::assertSame(0, $channel->subscribersCount());
44+
$model = $modelClass::query()->create();
45+
$user->subscribe($model);
46+
self::assertSame(1, $model->subscribersCount());
47+
$user->unsubscribe($model);
48+
self::assertSame(1, $model->subscribersCount());
49+
$model->loadCount('subscribers');
50+
self::assertSame(0, $model->subscribersCount());
3351
}
3452

3553
public function data(): array
@@ -67,61 +85,86 @@ public function testSubscribersCountForHumans($actual, $onePrecision, $twoPrecis
6785
self::assertSame($halfDown, $channel->subscribersCountForHumans(2, PHP_ROUND_HALF_DOWN));
6886
}
6987

70-
public function testIsSubscribedBy(): void
88+
/**
89+
* @dataProvider modelClasses
90+
*
91+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
92+
*/
93+
public function testIsSubscribedBy(string $modelClass): void
7194
{
7295
$user = User::query()->create();
73-
$channel = Channel::query()->create();
74-
self::assertFalse($channel->isSubscribedBy($channel));
75-
$user->subscribe($channel);
76-
self::assertTrue($channel->isSubscribedBy($user));
77-
$channel->load('subscribers');
78-
$user->unsubscribe($channel);
79-
self::assertTrue($channel->isSubscribedBy($user));
80-
$channel->load('subscribers');
81-
self::assertFalse($channel->isSubscribedBy($user));
96+
$model = $modelClass::query()->create();
97+
self::assertFalse($model->isSubscribedBy($model));
98+
$user->subscribe($model);
99+
self::assertTrue($model->isSubscribedBy($user));
100+
$model->load('subscribers');
101+
$user->unsubscribe($model);
102+
self::assertTrue($model->isSubscribedBy($user));
103+
$model->load('subscribers');
104+
self::assertFalse($model->isSubscribedBy($user));
82105
}
83106

84-
public function testIsNotSubscribedBy(): void
107+
/**
108+
* @dataProvider modelClasses
109+
*
110+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
111+
*/
112+
public function testIsNotSubscribedBy(string $modelClass): void
85113
{
86114
$user = User::query()->create();
87-
$channel = Channel::query()->create();
88-
self::assertTrue($channel->isNotSubscribedBy($channel));
89-
$user->subscribe($channel);
90-
self::assertFalse($channel->isNotSubscribedBy($user));
91-
$channel->load('subscribers');
92-
$user->unsubscribe($channel);
93-
self::assertFalse($channel->isNotSubscribedBy($user));
94-
$channel->load('subscribers');
95-
self::assertTrue($channel->isNotSubscribedBy($user));
115+
$model = $modelClass::query()->create();
116+
self::assertTrue($model->isNotSubscribedBy($model));
117+
$user->subscribe($model);
118+
self::assertFalse($model->isNotSubscribedBy($user));
119+
$model->load('subscribers');
120+
$user->unsubscribe($model);
121+
self::assertFalse($model->isNotSubscribedBy($user));
122+
$model->load('subscribers');
123+
self::assertTrue($model->isNotSubscribedBy($user));
96124
}
97125

98-
public function testSubscribers(): void
126+
/**
127+
* @dataProvider modelClasses
128+
*
129+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
130+
*/
131+
public function testSubscribers(string $modelClass): void
99132
{
100133
$user = User::query()->create();
101-
$channel = Channel::query()->create();
102-
$user->subscribe($channel);
103-
self::assertSame(1, $channel->subscribers()->count());
104-
$user->unsubscribe($channel);
105-
self::assertSame(0, $channel->subscribers()->count());
134+
$model = $modelClass::query()->create();
135+
$user->subscribe($model);
136+
self::assertSame(1, $model->subscribers()->count());
137+
$user->unsubscribe($model);
138+
self::assertSame(0, $model->subscribers()->count());
106139
}
107140

108-
public function testScopeWhereSubscribedBy(): void
141+
/**
142+
* @dataProvider modelClasses
143+
*
144+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
145+
*/
146+
public function testScopeWhereSubscribedBy(string $modelClass): void
109147
{
110148
$user = User::query()->create();
111149
$other = User::query()->create();
112-
$channel = Channel::query()->create();
113-
$user->subscribe($channel);
114-
self::assertSame(1, Channel::query()->whereSubscribedBy($user)->count());
115-
self::assertSame(0, Channel::query()->whereSubscribedBy($other)->count());
150+
$model = $modelClass::query()->create();
151+
$user->subscribe($model);
152+
self::assertSame(1, $modelClass::query()->whereSubscribedBy($user)->count());
153+
self::assertSame(0, $modelClass::query()->whereSubscribedBy($other)->count());
116154
}
117155

118-
public function testScopeWhereNotSubscribedBy(): void
156+
/**
157+
* @dataProvider modelClasses
158+
*
159+
* @param \LaravelInteraction\Subscribe\Tests\Models\User|\LaravelInteraction\Subscribe\Tests\Models\Channel|string $modelClass
160+
*/
161+
public function testScopeWhereNotSubscribedBy($modelClass): void
119162
{
120163
$user = User::query()->create();
121164
$other = User::query()->create();
122-
$channel = Channel::query()->create();
123-
$user->subscribe($channel);
124-
self::assertSame(0, Channel::query()->whereNotSubscribedBy($user)->count());
125-
self::assertSame(1, Channel::query()->whereNotSubscribedBy($other)->count());
165+
$model = $modelClass::query()->create();
166+
$user->subscribe($model);
167+
self::assertSame($modelClass::query()->whereKeyNot($model->getKey())->count(), $modelClass::query()->whereNotSubscribedBy($user)->count());
168+
self::assertSame($modelClass::query()->count(), $modelClass::query()->whereNotSubscribedBy($other)->count());
126169
}
127170
}

tests/Concerns/SubscriberTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public function testSubscriptions(): void
7979
$user = User::query()->create();
8080
$channel = Channel::query()->create();
8181
$user->toggleSubscribe($channel);
82-
self::assertSame(1, $user->subscriptions()->count());
83-
self::assertSame(1, $user->subscriptions->count());
82+
self::assertSame(1, $user->subscriberSubscriptions()->count());
83+
self::assertSame(1, $user->subscriberSubscriptions->count());
8484
}
8585

8686
public function testHasSubscribed(): void
@@ -90,7 +90,7 @@ public function testHasSubscribed(): void
9090
$user->toggleSubscribe($channel);
9191
self::assertTrue($user->hasSubscribed($channel));
9292
$user->toggleSubscribe($channel);
93-
$user->load('subscriptions');
93+
$user->load('subscriberSubscriptions');
9494
self::assertFalse($user->hasSubscribed($channel));
9595
}
9696

tests/Models/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace LaravelInteraction\Subscribe\Tests\Models;
66

77
use Illuminate\Database\Eloquent\Model;
8+
use LaravelInteraction\Subscribe\Concerns\Subscribable;
89
use LaravelInteraction\Subscribe\Concerns\Subscriber;
910

1011
/**
@@ -13,4 +14,5 @@
1314
class User extends Model
1415
{
1516
use Subscriber;
17+
use Subscribable;
1618
}

0 commit comments

Comments
 (0)