Skip to content

Commit

Permalink
Merge pull request #144 from ker0x/feature/travis-exception-deprecation
Browse files Browse the repository at this point in the history
Add PHP 7.4 to Travis, improve lisibility of exception, add deprecation
  • Loading branch information
ker0x authored Jan 27, 2020
2 parents 56d8218 + 2a37375 commit bb0f5a0
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 37 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
global:
Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ The Messenger library follows [SemVer](http://semver.org/).
**Changelog** (since [`3.3.0`](https://github.com/ker0x/messenger/compare/3.3.0...3.3.1))

- 3.3.1 (2020-01)
- Add Support of `CONFIRMED_EVENT_UPDATE` and `POST_PURCHASE_UPDATE` message tags.
- Deprecated old message tags which will be disabled on March 4th, 2020 (see: [Current Supported Tags](https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags#current_supported_tags))
- Add PHP 7.4 to Travis
- Add Support of `CONFIRMED_EVENT_UPDATE` and `POST_PURCHASE_UPDATE` message tags. (Thanks to @BFoucher)
- Deprecated old message tags which will be disabled on March 4th, 2020 (see [Current Supported Tags](https://developers.facebook.com/docs/messenger-platform/send-messages/message-tags#current_supported_tags))
- Buttons type `element_share` and `nested` are now deprecated

**Changelog** (since [`3.2.0`](https://github.com/ker0x/messenger/compare/3.2.0...3.3.0))

Expand Down
2 changes: 1 addition & 1 deletion src/Api/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected function isValidMessagingType(string $messagingType): void
{
$allowedMessagingType = $this->getAllowedMessagingType();
if (!\in_array($messagingType, $allowedMessagingType, true)) {
throw new InvalidTypeException(sprintf('messagingType must be either "%s".', implode(', ', $allowedMessagingType)));
throw new InvalidTypeException(sprintf('"messagingType" must be either "%s".', implode(', ', $allowedMessagingType)));
}
}

Expand Down
37 changes: 20 additions & 17 deletions src/Helper/ValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function isValidUrl(string $value): void
'/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/',
$value
)) {
throw new InvalidUrlException(sprintf('%s is not a valid url.', $value));
throw new InvalidUrlException(sprintf('"%s" is not a valid url.', $value));
}
}

Expand All @@ -64,7 +64,7 @@ protected function isValidUrl(string $value): void
protected function isValidLocale(string $value): void
{
if (!preg_match('/^[a-z]{2}_[A-Z]{2}$/', $value)) {
throw new InvalidLocaleException(sprintf('%s is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.', $value));
throw new InvalidLocaleException(sprintf('"%s" is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.', $value));
}
}

Expand All @@ -74,7 +74,7 @@ protected function isValidLocale(string $value): void
protected function isValidCountry(string $value): void
{
if (!preg_match('/^[A-Z]{2}$/', $value)) {
throw new InvalidCountryException(sprintf('%s is not valid. Country must be in ISO 3166 Alpha-2 format like FR.', $value));
throw new InvalidCountryException(sprintf('"%s" is not valid. Country must be in ISO 3166 Alpha-2 format like FR.', $value));
}
}

Expand All @@ -84,7 +84,7 @@ protected function isValidCountry(string $value): void
protected function isValidDateTime(string $value): void
{
if (!preg_match('/^(\d{4})-(0[1-9]|1[0-2])-([12]\d|0[1-9]|3[01])T(0\d|1\d|2[0-3]):([0-5]\d)$/', $value)) {
throw new InvalidDateTimeException(sprintf('%s is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format.', $value));
throw new InvalidDateTimeException(sprintf('"%s" is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format.', $value));
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ protected function isValidCurrency(string $value): void

$regex = '/^' . implode('|', $allowedCurrency) . '$/';
if (!preg_match($regex, $value)) {
throw new InvalidCurrencyException(sprintf('%s is not a valid currency. Currency must be in ISO-4217-3 format.', $value));
throw new InvalidCurrencyException(sprintf('"%s" is not a valid currency. Currency must be in ISO-4217-3 format.', $value));
}
}

Expand All @@ -124,7 +124,7 @@ protected function isValidExtension(string $filename, array $allowedExtension):
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (empty($ext) || !\in_array($ext, $allowedExtension, true)) {
throw new InvalidExtensionException(sprintf('%s does not have a valid extension. Allowed extensions are "%s".', $filename, implode(', ', $allowedExtension)));
throw new InvalidExtensionException(sprintf('"%s" does not have a valid extension. Allowed extensions are "%s".', $filename, implode(', ', $allowedExtension)));
}
}

Expand All @@ -138,11 +138,11 @@ protected function isValidButtons(array $buttons, array $allowedButtonsType): vo
/** @var \Kerox\Messenger\Model\Common\Button\AbstractButton $button */
foreach ($buttons as $button) {
if (!$button instanceof AbstractButton) {
throw new InvalidClassException(sprintf('Array can only contain instance of %s.', AbstractButton::class));
throw new InvalidClassException(sprintf('Array can only contain instance of "%s".', AbstractButton::class));
}

if (!\in_array($button->getType(), $allowedButtonsType, true)) {
throw new InvalidClassException(sprintf('Buttons can only be an instance of %s.', implode(', ', $allowedButtonsType)));
throw new InvalidClassException(sprintf('Buttons can only be an instance of "%s".', implode(', ', $allowedButtonsType)));
}
}
}
Expand All @@ -162,7 +162,7 @@ protected function isValidMessage($message): Message
return Message::create($message);
}

throw new MessengerException(sprintf('message must be a string or an instance of %s or %s.', Message::class, Attachment::class));
throw new MessengerException(sprintf('"message" must be a string or an instance of "%s" or "%s".', Message::class, Attachment::class));
}

/**
Expand All @@ -172,7 +172,7 @@ protected function isValidSenderAction(string $action): void
{
$allowedSenderAction = $this->getAllowedSenderAction();
if (!\in_array($action, $allowedSenderAction, true)) {
throw new InvalidKeyException(sprintf('action must be either "%s".', implode(', ', $allowedSenderAction)));
throw new InvalidKeyException(sprintf('"action" must be either "%s".', implode(', ', $allowedSenderAction)));
}
}

Expand All @@ -183,7 +183,7 @@ protected function isValidNotificationType(string $notificationType): void
{
$allowedNotificationType = $this->getAllowedNotificationType();
if (!\in_array($notificationType, $allowedNotificationType, true)) {
throw new InvalidTypeException(sprintf('notificationType must be either "%s".', implode(', ', $allowedNotificationType)));
throw new InvalidTypeException(sprintf('"notificationType" must be either "%s".', implode(', ', $allowedNotificationType)));
}
}

Expand All @@ -198,20 +198,23 @@ protected function isValidTag(string $tag, $message = null): void
$allowedTag = $this->getAllowedTag();
$deprecatedTag = $this->getDeprecatedTags();
if (!\in_array($tag, $allowedTag, true)) {
throw new InvalidKeyException(sprintf('tag must be either "%s".', implode(', ', $allowedTag)));
throw new InvalidKeyException(sprintf('"tag" must be either "%s".', implode(', ', $allowedTag)));
}

if (\in_array($tag, $deprecatedTag, true)) {
$message = sprintf('The %s tag is deprecated, use %s, %s, %s instead.',
$message = sprintf('Tag "%s" is deprecated, use one of "%s" instead.',
$tag,
SendInterface::TAG_CONFIRMED_EVENT_UPDATE,
SendInterface::TAG_POST_PURCHASE_UPDATE,
SendInterface::TAG_ACCOUNT_UPDATE);
implode(',', [
SendInterface::TAG_CONFIRMED_EVENT_UPDATE,
SendInterface::TAG_POST_PURCHASE_UPDATE,
SendInterface::TAG_ACCOUNT_UPDATE,
])
);
@trigger_error($message, E_USER_DEPRECATED);
}

if ($tag === SendInterface::TAG_ISSUE_RESOLUTION && $message !== null && !$message instanceof GenericTemplate) {
throw new InvalidClassException(sprintf('message must be an instance of %s if tag is set to %s.', GenericTemplate::class, SendInterface::TAG_ISSUE_RESOLUTION));
throw new InvalidClassException(sprintf('"message" must be an instance of "%s" if tag is set to "%s".', GenericTemplate::class, SendInterface::TAG_ISSUE_RESOLUTION));
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Model/Common/Button/AbstractButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ abstract class AbstractButton implements \JsonSerializable
public const TYPE_POSTBACK = 'postback';
public const TYPE_PHONE_NUMBER = 'phone_number';
public const TYPE_WEB_URL = 'web_url';
public const TYPE_SHARE = 'element_share';
public const TYPE_PAYMENT = 'payment';
public const TYPE_ACCOUNT_LINK = 'account_link';
public const TYPE_ACCOUNT_UNLINK = 'account_unlink';

/** @deprecated Since version 3.3.1 and will be removed in version 4.0.0. */
public const TYPE_SHARE = 'element_share';

/** @deprecated Since version 3.3.1 and will be removed in version 4.0.0. */
public const TYPE_NESTED = 'nested';

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/Api/SendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testSendAttachmentToUser(): void
public function testBadMessage(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('message must be a string or an instance of Kerox\Messenger\Model\Message or Kerox\Messenger\Model\Message\Attachment.');
$this->expectExceptionMessage('"message" must be a string or an instance of "Kerox\Messenger\Model\Message" or "Kerox\Messenger\Model\Message\Attachment".');
$this->sendApi->message('1008372609250235', 1234);
}

Expand All @@ -113,14 +113,14 @@ public function testSendMessageWithBadOptionsKey(): void
public function testBadActionType(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('action must be either "typing_on, typing_off, mark_seen".');
$this->expectExceptionMessage('"action" must be either "typing_on, typing_off, mark_seen".');
$this->sendApi->action('1008372609250235', 'typing_seen');
}

public function testBadMessagingType(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('messagingType must be either "RESPONSE, MESSAGE_TAG, NON_PROMOTIONAL_SUBSCRIPTION, UPDATE".');
$this->expectExceptionMessage('"messagingType" must be either "RESPONSE, MESSAGE_TAG, NON_PROMOTIONAL_SUBSCRIPTION, UPDATE".');
$this->sendApi->message('1008372609250235', 'Hello World!', [
'messaging_type' => 'PROMOTIONAL_SUBSCRIPTION',
]);
Expand All @@ -129,7 +129,7 @@ public function testBadMessagingType(): void
public function testBadNotificationType(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('notificationType must be either "REGULAR, SILENT_PUSH, NO_PUSH".');
$this->expectExceptionMessage('"notificationType" must be either "REGULAR, SILENT_PUSH, NO_PUSH".');
$this->sendApi->message('1008372609250235', 'Hello World!', [
'notification_type' => 'UPDATE',
]);
Expand All @@ -138,7 +138,7 @@ public function testBadNotificationType(): void
public function testBadTagType(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('tag must be either "CONFIRMED_EVENT_UPDATE, POST_PURCHASE_UPDATE, ACCOUNT_UPDATE, BUSINESS_PRODUCTIVITY, COMMUNITY_ALERT, CONFIRMED_EVENT_REMINDER, NON_PROMOTIONAL_SUBSCRIPTION, PAIRING_UPDATE, APPLICATION_UPDATE, PAYMENT_UPDATE, PERSONAL_FINANCE_UPDATE, SHIPPING_UPDATE, RESERVATION_UPDATE, ISSUE_RESOLUTION, APPOINTMENT_UPDATE, GAME_EVENT, TRANSPORTATION_UPDATE, FEATURE_FUNCTIONALITY_UPDATE, TICKET_UPDATE".');
$this->expectExceptionMessage('"tag" must be either "CONFIRMED_EVENT_UPDATE, POST_PURCHASE_UPDATE, ACCOUNT_UPDATE, BUSINESS_PRODUCTIVITY, COMMUNITY_ALERT, CONFIRMED_EVENT_REMINDER, NON_PROMOTIONAL_SUBSCRIPTION, PAIRING_UPDATE, APPLICATION_UPDATE, PAYMENT_UPDATE, PERSONAL_FINANCE_UPDATE, SHIPPING_UPDATE, RESERVATION_UPDATE, ISSUE_RESOLUTION, APPOINTMENT_UPDATE, GAME_EVENT, TRANSPORTATION_UPDATE, FEATURE_FUNCTIONALITY_UPDATE, TICKET_UPDATE".');
$this->sendApi->message('1008372609250235', 'Hello World!', [
'notification_type' => SendInterface::NOTIFICATION_TYPE_REGULAR,
'tag' => 'INVOICE_UPDATE',
Expand All @@ -150,7 +150,7 @@ public function testBadMessageForTagIssueResolution(): void
$message = $this->getReceipt();

$this->expectException(MessengerException::class);
$this->expectExceptionMessage('message must be an instance of Kerox\Messenger\Model\Message\Attachment\Template\GenericTemplate if tag is set to ISSUE_RESOLUTION.');
$this->expectExceptionMessage('"message" must be an instance of "Kerox\Messenger\Model\Message\Attachment\Template\GenericTemplate" if tag is set to "ISSUE_RESOLUTION".');
$this->sendApi->message('1008372609250235', $message, [
'notification_type' => SendInterface::NOTIFICATION_TYPE_REGULAR,
'tag' => SendInterface::TAG_ISSUE_RESOLUTION,
Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase/Helper/ValidatorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ public function testInvalidString(): void
public function testInvalidUrl(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('./img/image.png is not a valid url.');
$this->expectExceptionMessage('"./img/image.png" is not a valid url.');
$this->isValidUrl('./img/image.png');
}

public function testInvalidLocale(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('FR_fr is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.');
$this->expectExceptionMessage('"FR_fr" is not valid. Locale must be in ISO-639-1 and ISO-3166-1 format like fr_FR.');
$this->isValidLocale('FR_fr');
}

public function testInvalidCountry(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('us is not valid. Country must be in ISO 3166 Alpha-2 format like FR.');
$this->expectExceptionMessage('"us" is not valid. Country must be in ISO 3166 Alpha-2 format like FR.');
$this->isValidCountry('us');
}

public function testInvalidDateTime(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('20-11-2016T15:00 is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format.');
$this->expectExceptionMessage('"20-11-2016T15:00" is not valid. DateTime must be in ISO-8601 AAAA-MM-JJThh:mm format.');
$this->isValidDateTime('20-11-2016T15:00');
}

Expand All @@ -70,14 +70,14 @@ public function testInvalidArrayForMin(): void
public function testInvalidCurrency(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage(' is not a valid currency. Currency must be in ISO-4217-3 format.');
$this->expectExceptionMessage('"€" is not a valid currency. Currency must be in ISO-4217-3 format.');
$this->isValidCurrency('');
}

public function testInvalidExtension(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('http://example.com/img/image.bmp does not have a valid extension. Allowed extensions are "jpg, png, gif".');
$this->expectExceptionMessage('"http://example.com/img/image.bmp" does not have a valid extension. Allowed extensions are "jpg, png, gif".');
$this->isValidExtension('http://example.com/img/image.bmp', ['jpg', 'png', 'gif']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MediaElementTest extends AbstractTestCase
public function testInvalidButton(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('Buttons can only be an instance of web_url.');
$this->expectExceptionMessage('Buttons can only be an instance of "web_url".');

$element = MediaElement::create('https://www.facebook.com/photo.php?fbid=1234567890')
->setButtons([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class OpenGraphElementTest extends AbstractTestCase
public function testInvalidButton(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('Buttons can only be an instance of web_url');
$this->expectExceptionMessage('Buttons can only be an instance of "web_url"');

$element = OpenGraphElement::create('https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb')
->setButtons([
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Model/ProfileSettings/PersistentMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PersistentMenuTest extends AbstractTestCase
public function testInvalidButton(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('Array can only contain instance of Kerox\Messenger\Model\Common\Button\AbstractButton.');
$this->expectExceptionMessage('Array can only contain instance of "Kerox\Messenger\Model\Common\Button\AbstractButton".');

$persistentMenu = PersistentMenu::create()->setComposerInputDisabled(true)->addButtons([
'Phone Number' => [
Expand All @@ -29,7 +29,7 @@ public function testInvalidButton(): void
public function testInvalidButtonType(): void
{
$this->expectException(MessengerException::class);
$this->expectExceptionMessage('Buttons can only be an instance of web_url, postback, nested.');
$this->expectExceptionMessage('Buttons can only be an instance of "web_url, postback, nested".');

$persistentMenu = PersistentMenu::create()->setComposerInputDisabled(true)->addButtons([
PhoneNumber::create('Phone number', 'PHONE_NUMBER_PAYLOAD'),
Expand Down

0 comments on commit bb0f5a0

Please sign in to comment.