Skip to content

Commit

Permalink
feat: Add image aspect ratio to generic template
Browse files Browse the repository at this point in the history
  • Loading branch information
CasperLaiTW committed May 31, 2017
1 parent 15babbd commit 7309e5e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
36 changes: 36 additions & 0 deletions src/Messages/GenericTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@
*/
class GenericTemplate extends Template
{
/**
* Horizontal image
*/
const IMAGE_HORIZONTAL = 'horizontal';
/**
* Square image
*/
const IMAGE_SQUARE = 'square';

/**
* @var string
*/
private $imageRatio = self::IMAGE_HORIZONTAL;

/**
* Generic constructor.
*
Expand All @@ -42,6 +56,28 @@ public function toData()
return parent::toData();
}

/**
* Set image aspect ratio
*
* @param $value
* @return $this
*/
public function setImageRatio($value)
{
$this->imageRatio = $value;

return $this;
}

/**
* Get image ratio
* @return string
*/
public function getImageRatio()
{
return $this->imageRatio;
}

/**
* @return mixed
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Transformers/GenericTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Casperlaitw\LaravelFbMessenger\Transformers;

use Casperlaitw\LaravelFbMessenger\Contracts\Messages\Template;
use Casperlaitw\LaravelFbMessenger\Messages\GenericTemplate;

/**
* Class GenericTransformer
Expand All @@ -18,14 +19,15 @@ class GenericTransformer implements StructuredTransformer
/**
* Transform payload
*
* @param Template $message
* @param Template|GenericTemplate $message
*
* @return array
*/
public function transform(Template $message)
{
return [
'template_type' => 'generic',
'image_aspect_ratio' => $message->getImageRatio(),
'elements' => $message->getCollections()->toData()
];
}
Expand Down
36 changes: 33 additions & 3 deletions tests/Messages/GenericTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public function test_to_data()
'type' => 'template',
'payload' => [
'template_type' => 'generic',
'elements' => $elementExpected
'elements' => $elementExpected,
'image_aspect_ratio' => 'horizontal',
],
]
],
],
];

Expand All @@ -67,8 +68,9 @@ public function test_disable_share()
'template_type' => 'generic',
'elements' => $elementExpected,
'sharable' => false,
'image_aspect_ratio' => 'horizontal',
],
]
],
],
];

Expand All @@ -77,4 +79,32 @@ public function test_disable_share()

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

public function test_image_ratio()
{
$elementExpected = [];
foreach ($this->case as $case) {
$elementExpected[] = $case->toData();
}
$expected = [
'recipient' => [
'id' => $this->sender,
],
'message' => [
'attachment' => [
'type' => 'template',
'payload' => [
'template_type' => 'generic',
'elements' => $elementExpected,
'image_aspect_ratio' => 'square',
],
],
],
];

$actual = new GenericTemplate($this->sender, $this->case);
$actual->setImageRatio(GenericTemplate::IMAGE_SQUARE);

$this->assertEquals($expected, $actual->toData());
}
}
2 changes: 2 additions & 0 deletions tests/Transformers/GenericTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function test_transform()

$expected = [
'template_type' => 'generic',
'image_aspect_ratio' => 'horizontal',
'elements' => $expectedCase,
];

Expand All @@ -46,6 +47,7 @@ private function createMessageMock($testCase, $testSender)
$message = m::mock(Template::class)
->shouldReceive('getSender')->andReturn($testSender)
->shouldReceive('getCollections')->andReturn($elements)
->shouldReceive('getImageRatio')->andReturn('horizontal')
->getMock();

return $message;
Expand Down

0 comments on commit 7309e5e

Please sign in to comment.