Skip to content

Commit

Permalink
fix(user): adjust user types
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Dec 31, 2023
1 parent 4bcc6a6 commit 0224d12
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
25 changes: 18 additions & 7 deletions src/Users/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
* @psalm-import-type BotJson from Bot
*
* @psalm-type UserJson = array{
* object: "user",
* id: string,
* name: string|null,
* avatar_url: string|null,
* type: "person"|"bot",
* name?: string,
* avatar_url?: string,
* type?: "person"|"bot",
* person?: PersonJson,
* bot?: BotJson,
* }
Expand Down Expand Up @@ -53,12 +54,22 @@ public static function fromArray(array $array): self
public function toArray(): array
{
$array = [
"id" => $this->id,
"name" => $this->name,
"avatar_url" => $this->avatarUrl,
"type" => $this->type->value,
"object" => "user",
"id" => $this->id,
];

if ($this->type !== null) {
$array["type"] = $this->type->value;
}

if ($this->name !== null) {
$array["name"] = $this->name;
}

if ($this->avatarUrl !== null) {
$array["avatar_url"] = $this->avatarUrl;
}

if ($this->isPerson()) {
$array["person"] = $this->person->toArray();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Common/MentionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function test_mention_database(): void
public function test_mention_user(): void
{
$user = User::fromArray([
"object" => "user",
"id" => "b0688871-85db-4637-8fc9-043a240fcaec",
"name" => "Mario Simao",
"avatar_url" => "http://example.com",
Expand Down Expand Up @@ -80,6 +81,7 @@ public function test_user_array_conversion(): void
$array = [
"type" => "user",
"user" => [
"object" => "user",
"id" => "b0688871-85db-4637-8fc9-043a240fcaec",
"name" => "Mario Simao",
"avatar_url" => "http://example.com",
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Pages/Properties/PeopleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public function test_array_conversion(): void
private function user1(): User
{
return User::fromArray([
"object" => "user",
"id" => "f98bfb6a-08b3-4e65-861b-6f68fb0c7a48",
"name" => "Mario",
"avatar_url" => null,
"type" => "person",
"person" => [ "email" => "[email protected]" ],
]);
Expand All @@ -83,9 +83,9 @@ private function user1(): User
private function user2(): User
{
return User::fromArray([
"object" => "user",
"id" => "f98bfb6a-08b3-4e65-861b-6f68fb0c7a48",
"name" => "Luigi",
"avatar_url" => null,
"type" => "person",
"person" => [ "email" => "[email protected]" ],
]);
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/Users/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class UserTest extends TestCase
public function test_person_from_array(): void
{
$array = [
"object" => "user",
"id" => "b0688871-85db-4637-8fc9-043a240fcaec",
"name" => "Mario Simao",
"avatar_url" => "http://example.com",
Expand All @@ -31,9 +32,9 @@ public function test_person_from_array(): void
public function test_bot_from_array(): void
{
$array = [
"object" => "user",
"id" => "b0688871-85db-4637-8fc9-043a240fcaec",
"name" => "Notion Bot",
"avatar_url" => null,
"type" => "bot",
"bot" => [],
];
Expand All @@ -49,9 +50,9 @@ public function test_bot_from_array(): void
public function test_invalid_type_from_array(): void
{
$array = [
"object" => "user",
"id" => "b0688871-85db-4637-8fc9-043a240fcaec",
"name" => "Invalid user",
"avatar_url" => null,
"type" => "wrong-type",
];

Expand Down

0 comments on commit 0224d12

Please sign in to comment.