-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ViewersStore.viewers(SessionsSnapshot):Viewers
- Loading branch information
Showing
7 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace Coyote\Domain\Online; | ||
|
||
readonly class ViewerUser | ||
{ | ||
public function __construct( | ||
public string $name, | ||
public ?string $groupName, | ||
public ?string $avatarUrl, | ||
) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace Coyote\Domain\Online; | ||
|
||
readonly class Viewers | ||
{ | ||
/** | ||
* @param ViewerUser[] $users | ||
*/ | ||
public function __construct( | ||
public array $users, | ||
public int $guestsCount, | ||
) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Coyote\Domain\Online; | ||
|
||
use Coyote\User; | ||
|
||
class ViewersStore | ||
{ | ||
public function viewers(SessionsSnapshot $sessions): Viewers | ||
{ | ||
return new Viewers( | ||
$this->mapToViewerUsers($sessions), | ||
$sessions->guestsCount, | ||
); | ||
} | ||
|
||
private function mapToViewerUsers(SessionsSnapshot $sessions): array | ||
{ | ||
return User::query()->findMany($sessions->users) | ||
->map($this->viewerUser(...)) | ||
->toArray(); | ||
} | ||
|
||
private function viewerUser(User $user): ViewerUser | ||
{ | ||
return new ViewerUser( | ||
$user->name, | ||
$user->group_name, | ||
$user->photo->getFilename(), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
namespace Tests\Unit\OnlineUsers; | ||
|
||
use Coyote\Domain\Online\SessionsSnapshot; | ||
use Coyote\Domain\Online\ViewersStore; | ||
use Coyote\Domain\Online\ViewerUser; | ||
use PHPUnit\Framework\Attributes\Before; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
use Tests\Unit\BaseFixture\Forum\Models; | ||
|
||
class ViewersStoreTest extends TestCase | ||
{ | ||
use Models; | ||
|
||
private ViewersStore $store; | ||
|
||
#[Before] | ||
public function initialize(): void | ||
{ | ||
$this->store = new ViewersStore(); | ||
} | ||
|
||
#[Test] | ||
public function remainsGuestsCount(): void | ||
{ | ||
$viewer = $this->store->viewers($this->guests(99)); | ||
$this->assertSame(99, $viewer->guestsCount); | ||
} | ||
|
||
#[Test] | ||
public function viewerHasUserName(): void | ||
{ | ||
$id = $this->models->newUserReturnId('Mark'); | ||
$this->assertSame('Mark', $this->viewer($id)->name); | ||
} | ||
|
||
#[Test] | ||
public function viewerHasUserGroupName(): void | ||
{ | ||
$id = $this->models->newUserReturnId(groupName:'Admin'); | ||
$this->assertSame('Admin', $this->viewer($id)->groupName); | ||
} | ||
|
||
#[Test] | ||
public function viewerHasUserAvatar(): void | ||
{ | ||
$id = $this->models->newUserReturnId(photoUrl:'image.png'); | ||
$this->assertSame('image.png', $this->viewer($id)->avatarUrl); | ||
} | ||
|
||
#[Test] | ||
public function forUserWithoutGroup_viewerHasGroupNameNull(): void | ||
{ | ||
$id = $this->models->newUserReturnId(); | ||
$this->assertNull($this->viewer($id)->groupName); | ||
} | ||
|
||
#[Test] | ||
public function deletedUsers_areNotIncluded(): void | ||
{ | ||
$id = $this->models->newUserReturnId(deleted:true); | ||
$this->assertEmpty($this->viewers($id)); | ||
} | ||
|
||
private function guests(int $guestsCount): SessionsSnapshot | ||
{ | ||
return new SessionsSnapshot([], $guestsCount); | ||
} | ||
|
||
private function users(array $users): SessionsSnapshot | ||
{ | ||
return new SessionsSnapshot($users, 0); | ||
} | ||
|
||
private function viewer(int $id): ViewerUser | ||
{ | ||
return $this->viewers($id)[0]; | ||
} | ||
|
||
private function viewers(int $id): array | ||
{ | ||
$viewer = $this->store->viewers($this->users([$id])); | ||
return $viewer->users; | ||
} | ||
} |