8000 [Notifier] Check Discord embed limitations by alamirault · Pull Request #47859 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] Check Discord embed limitations #47859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Discord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Check embed limitations

5.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordAuthorEmbedObject extends AbstractDiscordEmbedObject
{
private const NAME_LIMIT = 256;

/**
* @return $this
*/
public function name(string $name): static
{
if (\strlen($name) > self::NAME_LIMIT) {
throw new LengthException(sprintf('Maximum length for the name is %d characters.', self::NAME_LIMIT));
}

$this->options['name'] = $name;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordEmbed extends AbstractDiscordEmbed
{
private const TITLE_LIMIT = 256;
private const DESCRIPTION_LIMIT = 4096;
private const FIELDS_LIMIT = 25;

/**
* @return $this
*/
public function title(string $title): static
{
if (\strlen($title) > self::TITLE_LIMIT) {
throw new LengthException(sprintf('Maximum length for the title is %d characters.', self::TITLE_LIMIT));
}

$this->options['title'] = $title;

return $this;
Expand All @@ -31,6 +41,10 @@ public function title(string $title): static
*/
public function description(string $description): static
{
if (\strlen($description) > self::DESCRIPTION_LIMIT) {
throw new LengthException(sprintf('Maximum length for the description is %d characters.', self::DESCRIPTION_LIMIT));
}

$this->options['description'] = $description;

return $this;
Expand Down Expand Up @@ -111,6 +125,10 @@ public function author(DiscordAuthorEmbedObject $author): static
*/
public function addField(DiscordFieldEmbedObject $field): static
{
if (self::FIELDS_LIMIT === \count($this->options['fields'] ?? [])) {
throw new LengthException(sprintf('Maximum number of fields should not exceed %d.', self::FIELDS_LIMIT));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new LengthException(sprintf('Maximum number of fields should not exceed %d.', self::FIELDS_LIMIT));
throw new LengthException(sprintf('Maximum number of fields must not exceed %d.', self::FIELDS_LIMIT));

}

if (!isset($this->options['fields'])) {
$this->options['fields'] = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor. 8000 hu>
*/
final class DiscordFieldEmbedObject extends AbstractDiscordEmbedObject
{
private const NAME_LIMIT = 256;
private const VALUE_LIMIT = 1024;

/**
* @return $this
*/
public function name(string $name): static
{
if (\strlen($name) > self::NAME_LIMIT) {
throw new LengthException(sprintf('Maximum length for the name is %d characters.', self::NAME_LIMIT));
}

$this->options['name'] = $name;

return $this;
Expand All @@ -31,6 +40,10 @@ public function name(string $name): static
*/
public function value(string $value): static
{
if (\strlen($value) > self::VALUE_LIMIT) {
throw new LengthException(sprintf('Maximum length for the value is %d characters.', self::VALUE_LIMIT));
}

$this->options['value'] = $value;

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,24 @@

namespace Symfony\Component\Notifier\Bridge\Discord\Embeds;

use Symfony\Component\Notifier\Exception\LengthException;

/**
* @author Karoly Gossler <connor@connor.hu>
*/
final class DiscordFooterEmbedObject extends AbstractDiscordEmbedObject
{
private const TEXT_LIMIT = 2048;

/**
* @return $this
*/
public function text(string $text): static
{
if (\strlen($text) > self::TEXT_LIMIT) {
throw new LengthException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT));
}

$this->options['text'] = $text;

return $this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordAuthorEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordAuthorEmbedObjectTest extends TestCase
{
public function testCanBeInstantiated()
{
$author = (new DiscordAuthorEmbedObject())
->name('Doe')
->url('http://ur.l')
->iconUrl('http://icon-ur.l')
->proxyIconUrl('http://proxy-icon-ur.l');

$this->assertSame([
'name' => 'Doe',
'url' => 'http://ur.l',
'icon_url' => 'http://icon-ur.l',
'proxy_icon_url' => 'http://proxy-icon-ur.l',
], $author->toArray());
}

public function testThrowsWhenNameExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the name is 256 characters.');

(new DiscordAuthorEmbedObject())->name(str_repeat('h', 257));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordEmbed;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFieldEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordEmbedTest extends TestCase
{
public function testCanBeInstantiated()
{
$embed = (new DiscordEmbed())
->title('foo')
->description('bar')
->addField((new DiscordFieldEmbedObject())
->name('baz')
->value('qux')
);

$this->assertSame([
'title' => 'foo',
'description' => 'bar',
'fields' => [
[
'name' => 'baz',
'value' => 'qux',
],
],
], $embed->toArray());
}

public function testThrowsWhenTitleExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the title is 256 characters.');

(new DiscordEmbed())->title(str_repeat('h', 257));
}

public function testThrowsWhenDescriptionExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the description is 4096 characters.');

(new DiscordEmbed())->description(str_repeat('h', 4097));
}

public function testThrowsWhenFieldsLimitReached()
{
$embed = new DiscordEmbed();
for ($i = 0; $i < 25; ++$i) {
$embed->addField((new DiscordFieldEmbedObject())
->name('baz')
->value('qux')
);
}

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Maximum number of fields should not exceed 25.');

$embed->addField((new DiscordFieldEmbedObject())
->name('fail')
->value('fail')
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFieldEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordFieldEmbedObjectTest extends TestCase
{
public function testCanBeInstantiated()
{
$field = (new DiscordFieldEmbedObject())
->name('foo')
->value('bar')
->inline(true);

$this->assertSame([
'name' => 'foo',
'value' => 'bar',
'inline' => true,
], $field->toArray());
}

public function testThrowsWhenNameExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the name is 256 characters.');

(new DiscordFieldEmbedObject())->name(str_repeat('h', 257));
}

public function testThrowsWhenValueExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the value is 1024 characters.');

(new DiscordFieldEmbedObject())->value(str_repeat('h', 1025));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Discord\Tests\Embeds;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Bridge\Discord\Embeds\DiscordFooterEmbedObject;
use Symfony\Component\Notifier\Exception\LengthException;

final class DiscordFooterEmbedObjectTest extends TestCase
{
public function testCanBeInstantiated()
{
$author = (new DiscordFooterEmbedObject())
->text('foo')
->iconUrl('http://icon-ur.l')
->proxyIconUrl('http://proxy-icon-ur.l');

$this->assertSame([
'text' => 'foo',
'icon_url' => 'http://icon-ur.l',
'proxy_icon_url' => 'http://proxy-icon-ur.l',
], $author->toArray());
}

public function testThrowsWhenTextExceedsCharacterLimit()
{
$this->expectException(LengthException::class);
$this->expectExceptionMessage('Maximum length for the text is 2048 characters.');

(new DiscordFooterEmbedObject())->text(str_repeat('h', 2049));
}
}
0