Skip to content

Commit

Permalink
Add ViewersStore.viewers(SessionsSnapshot):Viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Oct 3, 2024
1 parent c77523e commit 9bd8cea
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 3 deletions.
13 changes: 13 additions & 0 deletions app/Domain/Online/ViewerUser.php
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,
)
{
}
}
15 changes: 15 additions & 0 deletions app/Domain/Online/Viewers.php
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,
)
{
}
}
31 changes: 31 additions & 0 deletions app/Domain/Online/ViewersStore.php
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(),
);
}
}
15 changes: 12 additions & 3 deletions tests/Unit/BaseFixture/Forum/ModelsDsl.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,21 @@ public function newUserDeleted(string $name): void
$this->newUser(name:$name, deleted:true);
}

public function newUserReturnId(string $name = null, string $permissionName = null): int
{
$user = $this->models->newUserReturn(name:$name);
public function newUserReturnId(
string $name = null,
string $permissionName = null,
string $groupName = null,
string $photoUrl = null,
bool $deleted = false,
): int
{
$user = $this->models->newUserReturn(name:$name, photoUrl:$photoUrl, deleted:$deleted);
if ($permissionName) {
$this->models->assignToGroupWithPermission($user, $permissionName);
}
if ($groupName) {
$this->models->assignToGroupWithNameReturnId($user, $groupName);
}
return $user->id;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/BaseFixture/Forum/ModelsDslTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,23 @@ public function newUserWithPermissionNoOther(): void
$this->assertUserCan(false, $userId, 'forum-delete');
}

#[Test]
public function newUserWithGroupName(): void
{
$userId = $this->models->newUserReturnId(groupName:'Writer');
$this->assertDatabaseHas('users', [
'id' => $userId,
'group_name' => 'Writer',
]);
}

#[Test]
public function newUserWithAvatar(): void
{
$userId = $this->models->newUserReturnId(photoUrl:'image.png');
$this->assertStringEndsWith('/image.png', (string)User::query()->findOrFail($userId)->photo->url());
}

private function assertUserCan(bool $expected, int $userId, string $permission): void
{
/** @var User $user */
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/BaseFixture/Forum/ModelsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ private function groupWithPermission(string $permissionName): Group
return $group;
}

public function assignToGroupWithNameReturnId(User $user, string $groupName): void
{
$groupId = $this->groupWithName($groupName)->id;
$user->groups()->sync([$groupId]);
$user->group_id = $groupId;
$user->save();
}

private function groupWithName(string $groupName): Group
{
return Group::query()->create(['name' => $groupName]);
}

public function newPostReturn(
string $content = null,
Carbon $createdAt = null,
Expand Down
86 changes: 86 additions & 0 deletions tests/Unit/OnlineUsers/ViewersStoreTest.php
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;
}
}

0 comments on commit 9bd8cea

Please sign in to comment.