-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
tests/integration/TemplatesMessage/GenericTemplateTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |