Skip to content

Commit

Permalink
Fix issue ryangjchandler#8: Add a description field for internal refe…
Browse files Browse the repository at this point in the history
…rence.
  • Loading branch information
Ojsholly committed Sep 17, 2024
1 parent f4bfeee commit d14c66f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions database/factories/TokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ public function domains($domains)
]);
}

public function description(string $description)
{
return $this->state([
'description' => $description,
]);
}

public function definition()
{
return [
Expand Down
1 change: 1 addition & 0 deletions database/migrations/create_bearer_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return new class extends Migration
Schema::create('bearer_tokens', function (Blueprint $table) {
$table->id();
$table->string('token')->unique();
$table->text('description')->nullable();
$table->text('domains')->nullable();
$table->datetime('expires_at')->nullable();
$table->timestamps();
Expand Down
3 changes: 2 additions & 1 deletion src/Bearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public function generateTokenUsing(Closure $callback)
return $this;
}

public function generate(array $domains = [], DateTimeInterface $expiresAt = null): Token
public function generate(array $domains = [], DateTimeInterface $expiresAt = null, ?string $description = null): Token
{
$callback = $this->generateTokenCallback;

return Token::create([
'token' => $callback(),
'domains' => $domains,
'description' => $description,
'expires_at' => $expiresAt,
]);
}
Expand Down
10 changes: 9 additions & 1 deletion src/Models/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Token extends Model
protected $table = 'bearer_tokens';

protected $fillable = [
'token', 'domains', 'expires_at',
'token', 'domains', 'description', 'expires_at',
];

protected $casts = [
Expand Down Expand Up @@ -55,4 +55,12 @@ public function expires(DateTimeInterface $expiresAt): Token

return $this;
}

public function setDescription(string $description): Token
{
$this->description = $description;

return $this;
}

}
12 changes: 12 additions & 0 deletions tests/BearerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,16 @@ public function test_it_can_create_token_with_expires_at()

$this->assertTrue($time->equalTo($token->expires_at));
}

public function test_it_can_create_token_with_description()
{
$description = "Example description for the token.";

$token = Bearer::generate([], null, $description);

$this->assertDatabaseHas('bearer_tokens', [
'token' => $token->token,
'description' => $description,
]);
}
}
17 changes: 17 additions & 0 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,21 @@ public function test_it_can_interacts_with_expiration_time_in_months()
$this->assertTrue($token->expired);
Carbon::setTestNow();
}

public function test_it_can_have_description()
{
$token = Token::factory()->create();

$description = 'Example description for the token.';

$token->setDescription($description);

$this->assertSame($description, $token->description);

$newDescription = 'New example description for the token.';

$token->update(['description' => $newDescription]);

$this->assertSame($newDescription, $token->description);
}
}

0 comments on commit d14c66f

Please sign in to comment.