Skip to content

Commit

Permalink
fixed #198
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jan 22, 2024
1 parent ddb5592 commit 5be8669
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
5 changes: 0 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@
<directory>./tests/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
1 change: 1 addition & 0 deletions src/Traits/Follower.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function attachFollowStatus($followables, callable $resolver = null)
$item = $followed->where('followable_id', $followable->getKey())
->where('followable_type', $followable->getMorphClass())
->first();
$followable->setAttribute('has_followed', !!$item);
$followable->setAttribute('followed_at', $item ? $item->created_at : null);
$followable->setAttribute('follow_accepted_at', $item ? $item->accepted_at : null);
}
Expand Down
21 changes: 21 additions & 0 deletions tests/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,27 +232,48 @@ function () use ($user1, $users) {

$users = $users->toArray()['data'];
$this->assertNull($users[0]['followed_at']);
$this->assertFalse($users[0]['has_followed']);

$this->assertNotNull($users[1]['followed_at']);
$this->assertTrue($users[1]['has_followed']);

$this->assertNotNull($users[2]['followed_at']);
$this->assertTrue($users[2]['has_followed']);

$this->assertNotNull($users[3]['followed_at']);
$this->assertTrue($users[3]['has_followed']);

// cursor paginator
$users = User::cursorPaginate();
$user1->attachFollowStatus($users);

$users = $users->toArray()['data'];
$this->assertNull($users[0]['followed_at']);
$this->assertFalse($users[0]['has_followed']);

$this->assertNotNull($users[1]['followed_at']);
$this->assertTrue($users[1]['has_followed']);

$this->assertNotNull($users[2]['followed_at']);
$this->assertTrue($users[2]['has_followed']);

$this->assertNotNull($users[3]['followed_at']);
$this->assertTrue($users[3]['has_followed']);

// cursor
$users = User::cursor();
$users = $user1->attachFollowStatus($users)->toArray();
$this->assertNull($users[0]['followed_at']);
$this->assertFalse($users[0]['has_followed']);

$this->assertNotNull($users[1]['followed_at']);
$this->assertTrue($users[1]['has_followed']);

$this->assertNotNull($users[2]['followed_at']);
$this->assertTrue($users[2]['has_followed']);

$this->assertNotNull($users[3]['followed_at']);
$this->assertTrue($users[3]['has_followed']);

// with custom resolver
$users = \collect(['creator' => $user2], ['creator' => $user3], ['creator' => $user4]);
Expand Down

0 comments on commit 5be8669

Please sign in to comment.