Skip to content

Commit

Permalink
Merge pull request #147 from ker0x/feature/tests
Browse files Browse the repository at this point in the history
Merge event tests
  • Loading branch information
ker0x authored Feb 16, 2020
2 parents 9483eaa + 7fe1638 commit 77b86cb
Show file tree
Hide file tree
Showing 21 changed files with 189 additions and 542 deletions.
8 changes: 4 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
| Q | A
| ------------- | ---
| Branch? | master for features / 1.x up to 3.x for bug fixes <!-- see below -->
| Branch? | master
| Bug fix? | yes/no
| New feature? | yes/no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks? | no <!-- see https://symfony.com/bc -->
| Deprecations? | yes/no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| New feature? | yes/no <!-- don't forget to update CHANGELOG.md files -->
| BC breaks? | yes/no
| Deprecations? | yes/no <!-- don't forget to update CHANGELOG.md files -->
| Tests pass? | yes <!-- please add some, will be required by reviewers -->
| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->
| License | MIT
Expand Down
8 changes: 8 additions & 0 deletions tests/Mocks/Event/message.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
"quick_reply": {
"payload": "DEVELOPER_DEFINED_PAYLOAD"
},
"attachments": [
{
"type": "image",
"payload": {
"url": "IMAGE_URL"
}
}
],
"nlp": {
"entities": {
"datetime": [
Expand Down
174 changes: 174 additions & 0 deletions tests/TestCase/Event/EventFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Kerox\Messenger\Model\Callback\Optin;
use Kerox\Messenger\Model\Callback\PassThreadControl;
use Kerox\Messenger\Model\Callback\Payment;
use Kerox\Messenger\Model\Callback\Payment\PaymentCredential;
use Kerox\Messenger\Model\Callback\Payment\RequestedUserInfo;
use Kerox\Messenger\Model\Callback\PolicyEnforcement;
use Kerox\Messenger\Model\Callback\Postback;
use Kerox\Messenger\Model\Callback\PreCheckout;
Expand All @@ -42,6 +44,7 @@
use Kerox\Messenger\Model\Callback\Referral;
use Kerox\Messenger\Model\Callback\RequestThreadControl;
use Kerox\Messenger\Model\Callback\TakeThreadControl;
use Kerox\Messenger\Model\Common\Address;
use Kerox\Messenger\Test\TestCase\AbstractTestCase;

class EventFactoryTest extends AbstractTestCase
Expand All @@ -57,6 +60,11 @@ public function testRawEvent(): void
$this->assertEquals($expectedEvent, $event);
}

public function testEntryEvent(): void
{

}

public function testMessageEvent(): void
{
$json = file_get_contents(__DIR__ . '/../../Mocks/Event/message.json');
Expand All @@ -66,6 +74,24 @@ public function testMessageEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$message = $event->getMessage();
$this->assertSame('mid.1457764197618:41d102a3e1ae206a38', $message->getMessageId());
$this->assertTrue($message->hasText());
$this->assertSame('hello, world!', $message->getText());
$this->assertTrue($message->hasQuickReply());
$this->assertSame('DEVELOPER_DEFINED_PAYLOAD', $message->getQuickReply());
$this->assertTrue($message->hasAttachments());
$this->assertSame([['type' => 'image', 'payload' => ['url' => 'IMAGE_URL']]], $message->getAttachments());
$this->assertTrue($message->hasEntities());
$this->assertSame([
'datetime' => [
['confidence' => 0.97249440664957, 'values' => ['...'], 'value' => '2017-05-10T14:00:00.000-07:00', 'grain' => 'hour', 'type' => 'value']
],
'greetings' => [
['confidence' => 1, 'value' => 'true']
]
], $message->getEntities());
}

public function testMessageEchoEvent(): void
Expand All @@ -77,6 +103,12 @@ public function testMessageEchoEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$messageEcho = $event->getMessageEcho();
$this->assertTrue($messageEcho->isEcho());
$this->assertSame(1517776481860111, $messageEcho->getAppId());
$this->assertSame('DEVELOPER_DEFINED_METADATA_STRING', $messageEcho->getMetadata());
$this->assertSame('mid.1457764197618:41d102a3e1ae206a38', $messageEcho->getMessageId());
}

public function testPostbackEvent(): void
Expand All @@ -88,6 +120,31 @@ public function testPostbackEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$postback = $event->getPostback();
$this->assertSame('TITLE_FOR_THE_CTA', $postback->getTitle());
$this->assertTrue($postback->hasPayload());
$this->assertSame('USER_DEFINED_PAYLOAD', $postback->getPayload());
$this->assertTrue($postback->hasReferral());
$this->assertInstanceOf(Referral::class, $postback->getReferral());
}

public function testPostbackEventFromStandBy(): void
{
$json = file_get_contents(__DIR__ . '/../../Mocks/Event/postback_from_stand_by.json');
$array = json_decode($json, true);

$expectedEvent = new PostbackEvent('USER_ID', 'PAGE_ID', 1458692752478, Postback::create($array['postback']));
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$postback = $event->getPostback();
$this->assertSame('TITLE_FOR_THE_CTA', $postback->getTitle());
$this->assertFalse($postback->hasPayload());
$this->assertNull($postback->getPayload());
$this->assertFalse($postback->hasReferral());
$this->assertNull($postback->getReferral());
}

public function testOptinEvent(): void
Expand All @@ -99,6 +156,9 @@ public function testOptinEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$optin = $event->getOptin();
$this->assertSame('PASS_THROUGH_PARAM', $optin->getRef());
}

public function testAccountLinkingEvent(): void
Expand All @@ -110,6 +170,11 @@ public function testAccountLinkingEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$accountLinking = $event->getAccountLinking();
$this->assertSame('linked', $accountLinking->getStatus());
$this->assertTrue($accountLinking->hasAuthorizationCode());
$this->assertSame('PASS_THROUGH_AUTHORIZATION_CODE', $accountLinking->getAuthorizationCode());
}

public function testDeliveryEvent(): void
Expand All @@ -121,6 +186,10 @@ public function testDeliveryEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$delivery = $event->getDelivery();
$this->assertSame(1458668856253, $delivery->getWatermark());
$this->assertSame(['mid.1458668856218:ed81099e15d3f4f233'], $delivery->getMessageIds());
}

public function testReadEvent(): void
Expand All @@ -132,6 +201,9 @@ public function testReadEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$read = $event->getRead();
$this->assertSame(1458668856253, $read->getWatermark());
}

public function testPaymentEvent(): void
Expand All @@ -143,6 +215,37 @@ public function testPaymentEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$payment = $event->getPayment();
$this->assertSame('DEVELOPER_DEFINED_PAYLOAD', $payment->getPayload());
$this->assertSame('123', $payment->getShippingOptionId());
$this->assertSame('USD', $payment->getCurrency());
$this->assertSame('29.62', $payment->getAmount());
$this->assertInstanceOf(RequestedUserInfo::class, $payment->getRequestedUserInfo());
$this->assertInstanceOf(PaymentCredential::class, $payment->getPaymentCredential());
$this->assertInstanceOf(Address::class, $payment->getShippingAddress());

$requestedUserInfo = $payment->getRequestedUserInfo();
$this->assertSame('Peter Chang', $requestedUserInfo->getContactName());
$this->assertSame('[email protected]', $requestedUserInfo->getContactEmail());
$this->assertSame('+15105551234', $requestedUserInfo->getContactPhone());

$paymentCredential = $payment->getPaymentCredential();
$this->assertSame('token', $paymentCredential->getProviderType());
$this->assertSame('ch_18tmdBEoNIH3FPJHa60ep123', $paymentCredential->getChargeId());
$this->assertSame('__tokenized_card__', $paymentCredential->getTokenizedCard());
$this->assertSame('tokenized cvv', $paymentCredential->getTokenizedCvv());
$this->assertSame('3', $paymentCredential->getTokenExpiryMonth());
$this->assertSame('2019', $paymentCredential->getTokenExpiryYear());
$this->assertSame('123456789', $paymentCredential->getFbPaymentId());

$shippingAddress = $payment->getShippingAddress();
$this->assertSame('1 Hacker Way', $shippingAddress->getStreet());
$this->assertSame('', $shippingAddress->getAdditionalStreet());
$this->assertSame('MENLO PARK', $shippingAddress->getCity());
$this->assertSame('CA', $shippingAddress->getState());
$this->assertSame('US', $shippingAddress->getCountry());
$this->assertSame('94025', $shippingAddress->getPostalCode());
}

public function testCheckoutUpdateEvent(): void
Expand All @@ -154,6 +257,19 @@ public function testCheckoutUpdateEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$checkoutUpdate = $event->getCheckoutUpdate();
$this->assertSame('DEVELOPER_DEFINED_PAYLOAD', $checkoutUpdate->getPayload());
$this->assertInstanceOf(Address::class, $checkoutUpdate->getShippingAddress());

$shippingAddress = $checkoutUpdate->getShippingAddress();
$this->assertSame('1 Hacker Way', $shippingAddress->getStreet());
$this->assertSame('', $shippingAddress->getAdditionalStreet());
$this->assertSame('MENLO PARK', $shippingAddress->getCity());
$this->assertSame('CA', $shippingAddress->getState());
$this->assertSame('US', $shippingAddress->getCountry());
$this->assertSame('94025', $shippingAddress->getPostalCode());
$this->assertSame(10105655000959552, $shippingAddress->getId());
}

public function testPreCheckoutEvent(): void
Expand All @@ -165,6 +281,25 @@ public function testPreCheckoutEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$preCheckout = $event->getPreCheckout();
$this->assertSame('DEVELOPER_DEFINED_PAYLOAD', $preCheckout->getPayload());
$this->assertSame('USD', $preCheckout->getCurrency());
$this->assertSame('29.62', $preCheckout->getAmount());
$this->assertInstanceOf(RequestedUserInfo::class, $preCheckout->getRequestedUserInfo());
$this->assertInstanceOf(Address::class, $preCheckout->getShippingAddress());

$requestedUserInfo = $preCheckout->getRequestedUserInfo();
$this->assertSame('Peter Chang', $requestedUserInfo->getContactName());

$shippingAddress = $preCheckout->getShippingAddress();
$this->assertSame('Peter Chang', $shippingAddress->getName());
$this->assertSame('1 Hacker Way', $shippingAddress->getStreet());
$this->assertSame('', $shippingAddress->getAdditionalStreet());
$this->assertSame('MENLO PARK', $shippingAddress->getCity());
$this->assertSame('CA', $shippingAddress->getState());
$this->assertSame('US', $shippingAddress->getCountry());
$this->assertSame('94025', $shippingAddress->getPostalCode());
}

public function testTakeThreadControlEvent(): void
Expand All @@ -176,6 +311,10 @@ public function testTakeThreadControlEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$takeThreadControl = $event->getTakeThreadControl();
$this->assertSame(123456789, $takeThreadControl->getPreviousOwnerAppId());
$this->assertSame('additional content that the caller wants to set', $takeThreadControl->getMetadata());
}

public function testPassThreadControlEvent(): void
Expand All @@ -187,6 +326,10 @@ public function testPassThreadControlEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$passThreadControl = $event->getPassThreadControl();
$this->assertSame(123456789, $passThreadControl->getNewOwnerAppId());
$this->assertSame('additional content that the caller wants to set', $passThreadControl->getMetadata());
}

public function testRequestThreadControlEvent(): void
Expand All @@ -198,6 +341,10 @@ public function testRequestThreadControlEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$requestThreadControl = $event->getRequestThreadControl();
$this->assertSame(123456789, $requestThreadControl->getRequestedOwnerAppId());
$this->assertSame('additional content that the caller wants to set', $requestThreadControl->getMetadata());
}

public function testPolicyEnforcementEvent(): void
Expand All @@ -209,6 +356,11 @@ public function testPolicyEnforcementEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$policyEnforcement = $event->getPolicyEnforcement();
$this->assertSame('block', $policyEnforcement->getAction());
$this->assertSame('The bot violated our Platform Policies (https://developers.facebook.com/policy/#messengerplatform). Common violations include sending out excessive spammy messages or being non-functional.', $policyEnforcement->getReason());

}

public function testAppRolesEvent(): void
Expand All @@ -220,6 +372,9 @@ public function testAppRolesEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$appRoles = $event->getAppRoles();
$this->assertSame(['123456789' => ['automation']], $appRoles->getAppRoles());
}

public function testReferralEvent(): void
Expand All @@ -231,6 +386,11 @@ public function testReferralEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$referral = $event->getReferral();
$this->assertSame('REF DATA PASSED IN M.ME PARAM', $referral->getRef());
$this->assertSame('SHORTLINK', $referral->getSource());
$this->assertSame('OPEN_THREAD', $referral->getType());
}

public function testGamePlayEvent(): void
Expand All @@ -242,6 +402,14 @@ public function testGamePlayEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$gamePlay = $event->getGamePlay();
$this->assertSame('1234', $gamePlay->getGameId());
$this->assertSame('666', $gamePlay->getPlayerId());
$this->assertSame('SOLO|THREAD', $gamePlay->getContextType());
$this->assertSame('123', $gamePlay->getContextId());
$this->assertSame(1234567890, $gamePlay->getScore());
$this->assertSame('DEVELOPER_DEFINED_PAYLOAD', $gamePlay->getPayload());
}

public function testReactionEvent(): void
Expand All @@ -253,5 +421,11 @@ public function testReactionEvent(): void
$event = EventFactory::create($array);

$this->assertEquals($expectedEvent, $event);

$reaction = $event->getReaction();
$this->assertSame('smile', $reaction->getReaction());
$this->assertSame("❤️", $reaction->getEmoji());
$this->assertSame('react', $reaction->getAction());
$this->assertSame('<MID>', $reaction->getMid());
}
}
6 changes: 3 additions & 3 deletions tests/TestCase/Event/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ public function testReferralEvent(): void

public function testReactionEvent(): void
{
$mockedReferral = $this->createMock(Reaction::class);
$event = new ReactionEvent('sender_id', 'recipient_id', 123456, $mockedReferral);
$mockedReaction = $this->createMock(Reaction::class);
$event = new ReactionEvent('sender_id', 'recipient_id', 123456, $mockedReaction);

$this->assertSame('sender_id', $event->getSenderId());
$this->assertSame('recipient_id', $event->getRecipientId());
$this->assertSame(123456, $event->getTimestamp());
$this->assertSame($mockedReferral, $event->getReaction());
$this->assertSame($mockedReaction, $event->getReaction());
$this->assertSame('reaction', $event->getName());
}
}
20 changes: 0 additions & 20 deletions tests/TestCase/Model/Callback/AccountLinkingTest.php

This file was deleted.

Loading

0 comments on commit 77b86cb

Please sign in to comment.