Skip to content

Commit

Permalink
Templates finalizados
Browse files Browse the repository at this point in the history
  • Loading branch information
vipeol committed Jul 1, 2017
1 parent 735a7bc commit 477ec2b
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 1 deletion.
50 changes: 50 additions & 0 deletions src/Element/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace CodeBot\Element;

class Product implements ElementInterface
{
private $title;
private $subtitle;
private $image_url;
private $default_action;
private $buttons;

public function __construct(string $title, ? string $image_url = null, ? string $subtitle = null, Button $default_action = null)
{
$this->title = $title;
$this->subtitle = $subtitle;
$this->image_url = $image_url;
$this->default_action = $default_action;
}

public function add(Button $element)
{
$this->buttons[] = $element->get();
}

public function get(): array
{
$result['title'] = $this->title;

if ($this->image_url !== null) {
$result['image_url'] = $this->image_url;
}

if ($this->subtitle !== null) {
$result['subtitle'] = $this->subtitle;
}

if ($this->default_action !== null) {
$default_action = $this->default_action->get();
unset($default_action['title']);
$result['default_action'] = $default_action;
}

if ($this->buttons !== null) {
$result['buttons'] = $this->buttons;
}

return $result;
}
}
44 changes: 44 additions & 0 deletions src/Element/ProductOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace CodeBot\Element;

class ProductOrder implements ElementInterface
{
private $title;
private $subtitle;
private $quantity;
private $price;
private $currency;
private $image_url;

public function __construct(string $title, string $subtitle, ? int $quantity = null, float $price = 0, ? string $currency = null, string $image_url = null)
{
$this->title = $title;
$this->subtitle = $subtitle;
$this->quantity = $quantity;
$this->price = $price;
$this->currency = $currency;
$this->image_url = $image_url;
}

public function get() :array
{
$result['title'] = $this->title;
$result['subtitle'] = $this->subtitle;
$result['price'] = $this->price;

if ($this->quantity !== null) {
$result['quantity'] = $this->quantity;
}

if ($this->currency !== null) {
$result['currency'] = $this->currency;
}

if ($this->image_url !== null) {
$result['image_url'] = $this->image_url;
}

return $result;
}
}
1 change: 0 additions & 1 deletion src/TemplatesMessage/ListTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function message(string $messageText) : array
'type' => 'template',
'payload' => [
'template_type' => 'list',
'text' => $messageText,
'buttons' => $this->products
]
]
Expand Down
61 changes: 61 additions & 0 deletions tests/integration/TemplatesMessage/GenericTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace CodeBot\TemplatesMessage;

use CodeBot\Element\Button;
use CodeBot\Element\Product;
use PHPUnit\Framework\TestCase;

class GenericTemplateTest extends TestCase
{
public function testListaComDoisProdutos()
{
$button = new Button('web_url', null, 'https://angular.io/');
$product = new Product('Produto 1','https://www.w3schools.com/angular/pic_angular.jpg','Curso de Angular', $button);

$button = new Button('web_url', null, 'http://php.net/');
$product2 = new Product('Produto 2','https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/PHP-logo.svg/711px-PHP-logo.svg.png','Curso de PHP', $button);

$template = new GenericTemplate(1234);
$template->add($product);
$template->add($product2);

$actual = $template->message('qwe');

$expected = [
'recipient' => [
'id' => 1234
],
'message' => [
'attachment' => [
'type' => 'template',
'payload' => [
'template_type' => 'generic',
'buttons' => [
[
'title' => 'Produto 1',
'subtitle' => 'Curso de Angular',
'image_url' => 'https://www.w3schools.com/angular/pic_angular.jpg',
'default_action' => [
'type' => 'web_url',
'url' => 'https://angular.io/'
]
],
[
'title' => 'Produto 2',
'subtitle' => 'Curso de PHP',
'image_url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/PHP-logo.svg/711px-PHP-logo.svg.png',
'default_action' => [
'type' => 'web_url',
'url' => 'http://php.net/'
]
]
]
]
]
]
];

$this->assertEquals($expected, $actual);
}
}
62 changes: 62 additions & 0 deletions tests/integration/TemplatesMessage/ListTemplateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace CodeBot\TemplatesMessage;

use CodeBot\Element\Button;
use CodeBot\Element\Product;
use PHPUnit\Framework\TestCase;

class ListTemplateTest extends TestCase
{
public function testListaComDoisProdutos()
{
$button = new Button('web_url', null, 'https://angular.io/');
$product = new Product('Produto 1','https://www.w3schools.com/angular/pic_angular.jpg','Curso de Angular', $button);

$button = new Button('web_url', null, 'http://php.net/');
$product2 = new Product('Produto 2','https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/PHP-logo.svg/711px-PHP-logo.svg.png','Curso de PHP', $button);

$template = new ListTemplate(1234);
$template->add($product);
$template->add($product2);

$actual = $template->message('qwe');

$expected = [
'recipient' => [
'id' => 1234
],
'message' => [
'attachment' => [
'type' => 'template',
'payload' => [
'template_type' => 'list',
'buttons' => [
[
'title' => 'Produto 1',
'subtitle' => 'Curso de Angular',
'image_url' => 'https://www.w3schools.com/angular/pic_angular.jpg',
'default_action' => [
'type' => 'web_url',
'url' => 'https://angular.io/'
]
],
[
'title' => 'Produto 2',
'subtitle' => 'Curso de PHP',
'image_url' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/27/PHP-logo.svg/711px-PHP-logo.svg.png',
'default_action' => [
'type' => 'web_url',
'url' => 'http://php.net/'
]
]
]
]
]
]
];

$this->assertEquals($expected, $actual);

}
}

0 comments on commit 477ec2b

Please sign in to comment.