Skip to content

Commit

Permalink
Merge pull request #3 from tgallice/rework_attachments
Browse files Browse the repository at this point in the history
Add concretes structured attachment
  • Loading branch information
tgallice committed May 10, 2016
2 parents da90f76 + 49389a5 commit c29a3bb
Show file tree
Hide file tree
Showing 18 changed files with 376 additions and 523 deletions.
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ $response = $messenger->sendMessage($message);

require_once __DIR__.'/vendor/autoload.php';

use Tgallice\FBMessenger\Attachment\Structured;
use Tgallice\FBMessenger\Attachment\Structured\Receipt;
use Tgallice\FBMessenger\Messenger;
use Tgallice\FBMessenger\Message\Message;
use Tgallice\FBMessenger\Model\Receipt\Element;
use Tgallice\FBMessenger\Model\Summary;
use Tgallice\FBMessenger\Template\Receipt;

$messenger = new Messenger('page_token');

Expand All @@ -61,8 +60,7 @@ $elements = [

$summary = new Summary(<total_cost>);

$receipt = new Receipt('My Receipt', '123456789', 'EUR', 'Visa', $elements, $summary);
$attachment = new Structured($receipt);
$attachment = new Receipt('My Receipt', '123456789', 'EUR', 'Visa', $elements, $summary);
$message = new Message('<USER_ID>', $attachment);

$response = $messenger->sendMessage($message);
Expand All @@ -75,10 +73,9 @@ $response = $messenger->sendMessage($message);

require_once __DIR__.'/vendor/autoload.php';

use Tgallice\FBMessenger\Attachment\Structured;
use Tgallice\FBMessenger\Attachment\Structured\Button;
use Tgallice\FBMessenger\Messenger;
use Tgallice\FBMessenger\Message\Message;
use Tgallice\FBMessenger\Template\Button as ButtonTemplate;
use Tgallice\FBMessenger\Model\Button\WebUrl;
use Tgallice\FBMessenger\Model\Button\Postback;

Expand All @@ -89,8 +86,7 @@ $elements = [
new Postback('Button2', 'EVENT_NAME'),
];

$template = new ButtonTemplate('My template', $elements);
$attachment = new Structured($template);
$attachment = new Button('My template', $elements);

$message = new Message('<USER_ID>', $attachment);
$response = $messenger->sendMessage($message);
Expand Down
67 changes: 67 additions & 0 deletions spec/Attachment/Structured/ButtonSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace spec\Tgallice\FBMessenger\Attachment\Structured;

use PhpSpec\ObjectBehavior;
use Tgallice\FBMessenger\Attachment;
use Tgallice\FBMessenger\Attachment\Structured;
use Tgallice\FBMessenger\Attachment\Structured\Button;
use Tgallice\FBMessenger\Model\Button as ButtonModel;

class ButtonSpec extends ObjectBehavior
{
function let(ButtonModel $button)
{
$this->beConstructedWith('text', [$button]);
}

function it_is_initializable()
{
$this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured\Button');
}

function it_is_a_structured_attachment()
{
$this->shouldImplement(Structured::class);
}

function it_should_return_the_type()
{
$this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE);
}

function it_should_return_the_main_text()
{
$this->getText()->shouldReturn('text');
}

function it_should_return_the_buttons($button)
{
$this->getButtons()->shouldReturn([$button]);
}

function it_should_return_the_payload($button)
{
$this->getPayload()->shouldReturn([
'template_type' => Button::TEMPLATE_TYPE,
'text' => 'text',
'buttons' => [$button],
]);
}

function it_should_be_serializable($button)
{
$this->shouldImplement(\JsonSerializable::class);

$expected = [
'type' => Attachment::TYPE_TEMPLATE,
'payload' => [
'template_type' => Button::TEMPLATE_TYPE,
'text' => 'text',
'buttons' => [$button],
],
];

$this->jsonSerialize()->shouldBeLike($expected);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

namespace spec\Tgallice\FBMessenger\Template;
namespace spec\Tgallice\FBMessenger\Attachment\Structured;

use PhpSpec\ObjectBehavior;
use Tgallice\FBMessenger\Attachment;
use Tgallice\FBMessenger\Attachment\Structured;
use Tgallice\FBMessenger\Attachment\Structured\Generic;
use Tgallice\FBMessenger\Model\Generic\Element;
use Tgallice\FBMessenger\Template;

class GenericSpec extends ObjectBehavior
{
Expand All @@ -15,31 +17,42 @@ function let(Element $element)

function it_is_initializable()
{
$this->shouldHaveType('Tgallice\FBMessenger\Template\Generic');
$this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured\Generic');
}

function it_is_a_template()
function it_is_a_structured_attachment()
{
$this->shouldImplement(Template::class);
$this->shouldImplement(Structured::class);
}

function it_should_return_the_type()
{
$this->getType()->shouldReturn(Template::TYPE_GENERIC);
$this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE);
}

function it_should_return_the_elements($element)
{
$this->getElements()->shouldReturn([$element]);
}

function it_should_return_the_payload($element)
{
$this->getPayload()->shouldReturn([
'template_type' => Generic::TEMPLATE_TYPE,
'elements' => [$element],
]);
}

function it_should_be_serializable($element)
{
$this->shouldImplement(\JsonSerializable::class);

$expected = [
'template_type' => Template::TYPE_GENERIC,
'elements' => [$element],
'type' => Attachment::TYPE_TEMPLATE,
'payload' => [
'template_type' => Generic::TEMPLATE_TYPE,
'elements' => [$element],
],
];

$this->jsonSerialize()->shouldBeLike($expected);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace spec\Tgallice\FBMessenger\Template;
namespace spec\Tgallice\FBMessenger\Attachment\Structured;

use PhpSpec\ObjectBehavior;
use Tgallice\FBMessenger\Attachment;
use Tgallice\FBMessenger\Attachment\Structured;
use Tgallice\FBMessenger\Attachment\Structured\Receipt;
use Tgallice\FBMessenger\Model\Address;
use Tgallice\FBMessenger\Model\Adjustment;
use Tgallice\FBMessenger\Model\Receipt\Element;
use Tgallice\FBMessenger\Model\Summary;
use Tgallice\FBMessenger\Template;

class ReceiptSpec extends ObjectBehavior
{
Expand All @@ -18,17 +20,17 @@ function let(Element $element, Summary $summary)

function it_is_initializable()
{
$this->shouldHaveType('Tgallice\FBMessenger\Template\Receipt');
$this->shouldHaveType('Tgallice\FBMessenger\Attachment\Structured\Receipt');
}

function it_is_a_template()
function it_is_a_structured_attachment()
{
$this->shouldImplement(Template::class);
$this->shouldImplement(Structured::class);
}

function it_should_return_type()
{
$this->getType()->shouldReturn(Template::TYPE_RECEIPT);
$this->getType()->shouldReturn(Attachment::TYPE_TEMPLATE);
}

function it_should_return_the_recipient_name()
Expand Down Expand Up @@ -106,12 +108,10 @@ function its_adjustments_are_mutable(Adjustment $adjustment)
$this->getAdjustments()->shouldReturn([$adjustment]);
}

function it_should_be_serializable($element, $summary, Address $address, Adjustment $adjustment)
function it_should_return_the_payload($element, $summary)
{
$this->shouldImplement(\JsonSerializable::class);

$expected = [
'template_type' => Template::TYPE_RECEIPT,
$this->getPayload()->shouldReturn([
'template_type' => Receipt::TEMPLATE_TYPE,
'recipient_name' => 'recipient',
'order_number' => '1a2b',
'currency' => 'currency',
Expand All @@ -122,12 +122,35 @@ function it_should_be_serializable($element, $summary, Address $address, Adjustm
'address' => null,
'summary' => $summary,
'adjustments' => [],
]);
}

function it_should_be_serializable($element, $summary, Address $address, Adjustment $adjustment)
{
$this->shouldImplement(\JsonSerializable::class);

$expected = [
'type' => Attachment::TYPE_TEMPLATE,
'payload' => [
'template_type' => Receipt::TEMPLATE_TYPE,
'recipient_name' => 'recipient',
'order_number' => '1a2b',
'currency' => 'currency',
'payment_method' => 'payment_method',
'timestamp' => null,
'order_url' => null,
'elements' => [$element],
'address' => null,
'summary' => $summary,
'adjustments' => [],
],
];

$this->jsonSerialize()->shouldBeLike($expected);

$time = time();

$expected = array_merge($expected, [
$expected['payload'] = array_merge($expected['payload'], [
'timestamp' => $time,
'order_url' => 'http://order.com',
'address' => $address,
Expand Down
47 changes: 0 additions & 47 deletions spec/Attachment/StructuredSpec.php

This file was deleted.

2 changes: 1 addition & 1 deletion spec/Model/Generic/ElementSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace spec\Tgallice\FBMessenger\Model\Generic;

use PhpSpec\ObjectBehavior;
use Tgallice\FBMessenger\Template\Button;
use Tgallice\FBMessenger\Model\Button;

class ElementSpec extends ObjectBehavior
{
Expand Down
53 changes: 0 additions & 53 deletions spec/Template/ButtonSpec.php

This file was deleted.

Loading

0 comments on commit c29a3bb

Please sign in to comment.