Skip to content

Commit

Permalink
#13: support for fields and additional embed objects within GenericEmbed
Browse files Browse the repository at this point in the history
update test
  • Loading branch information
nwilging committed Apr 18, 2023
1 parent 6ea7b77 commit eb2e6af
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
78 changes: 76 additions & 2 deletions src/Support/Embeds/GenericEmbed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,87 @@ class GenericEmbed extends Embed
{
use MergesArrays;

public function __construct(string $title, string $description)
protected ?FooterEmbed $footer = null;

protected ?ImageEmbed $image = null;

protected ?ThumbnailEmbed $thumbnail = null;

protected ?VideoEmbed $video = null;

protected ?ProviderEmbed $provider = null;

protected ?AuthorEmbed $author = null;

/**
* @var FieldEmbed[]
*/
protected array $fields = [];

public function __construct(?string $title = null, ?string $description = null, ?string $timestamp = null)
{
parent::__construct($title, $description, $timestamp);
}

public function withAuthor(AuthorEmbed $author): self
{
$this->author = $author;
return $this;
}

public function withFooter(FooterEmbed $footer): self
{
$this->footer = $footer;
return $this;
}

public function withImage(ImageEmbed $image): self
{
parent::__construct($title, $description);
$this->image = $image;
return $this;
}

public function withThumbnail(ThumbnailEmbed $thumbnail): self
{
$this->thumbnail = $thumbnail;
return $this;
}

public function withVideo(VideoEmbed $video): self
{
$this->video = $video;
return $this;
}

public function withProvider(ProviderEmbed $provider): self
{
$this->provider = $provider;
return $this;
}

public function addField(FieldEmbed $field): self
{
$this->fields[] = $field;
return $this;
}

public function getType(): string
{
return static::TYPE_RICH;
}

public function toArray(): array
{
return $this->toMergedArray([
'footer' => $this->footer?->toArray(),
'image' => $this->image?->toArray(),
'thumbnail' => $this->thumbnail?->toArray(),
'video' => $this->video?->toArray(),
'provider' => $this->provider?->toArray(),
'author' => $this->author?->toArray(),
'fields' => array_map(function (FieldEmbed $field): array {
return $field->toArray();
}, $this->fields),
]);
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Support/Embeds/GenericEmbedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
namespace Nwilging\LaravelDiscordBotTests\Unit\Support\Embeds;

use Nwilging\LaravelDiscordBot\Support\Embed;
use Nwilging\LaravelDiscordBot\Support\Embeds\AuthorEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\FieldEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\FooterEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\GenericEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\ImageEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\ProviderEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\ThumbnailEmbed;
use Nwilging\LaravelDiscordBot\Support\Embeds\VideoEmbed;
use Nwilging\LaravelDiscordBotTests\TestCase;

class GenericEmbedTest extends TestCase
Expand Down Expand Up @@ -35,4 +42,51 @@ public function testEmbedWithOptions()
'color' => 42,
], $embed->toArray());
}

public function testEmbedWithAllOptions()
{
$title = 'test title';
$description = 'test description';

$footer = new FooterEmbed('test footer');
$author = new AuthorEmbed('test author');
$provider = new ProviderEmbed('test provider');
$image = new ImageEmbed('test image');
$thumbnail = new ThumbnailEmbed('test thumbnail');
$video = new VideoEmbed('test video');

$embed = new GenericEmbed($title, $description);

$embed->withColor(42);

$embed->withFooter($footer);
$embed->withAuthor($author);
$embed->withProvider($provider);
$embed->withImage($image);
$embed->withThumbnail($thumbnail);
$embed->withVideo($video);

$field1 = new FieldEmbed('test field 1', 'test field 1 value');
$field2 = new FieldEmbed('test field 2', 'test field 2 value');

$embed->addField($field1)
->addField($field2);

$this->assertEquals([
'type' => Embed::TYPE_RICH,
'title' => $title,
'description' => $description,
'color' => 42,
'footer' => $footer->toArray(),
'author' => $author->toArray(),
'provider' => $provider->toArray(),
'image' => $image->toArray(),
'thumbnail' => $thumbnail->toArray(),
'video' => $video->toArray(),
'fields' => [
$field1->toArray(),
$field2->toArray(),
],
], $embed->toArray());
}
}

0 comments on commit eb2e6af

Please sign in to comment.