Skip to content

Commit

Permalink
Fix OpenGraph template and element class, fix tests (#82)
Browse files Browse the repository at this point in the history
* Fix OpenGraph template and element class, fix tests

* Fix variables name for OpenGraphElementTest

* Revert setUrl() to url()
  • Loading branch information
ker0x authored and Christoph Rumpel committed Oct 4, 2018
1 parent a289f88 commit 8071f77
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 54 deletions.
29 changes: 17 additions & 12 deletions src/Extensions/OpenGraphElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@

class OpenGraphElement implements JsonSerializable
{
/** @var string */
/**
* @var string
*/
protected $url;

/** @var object */
/**
* @var array
*/
protected $buttons;

/**
Expand All @@ -22,9 +26,10 @@ public static function create()

/**
* @param string $url
*
* @return $this
*/
public function url($url)
public function url(string $url): self
{
$this->url = $url;

Expand All @@ -33,9 +38,10 @@ public function url($url)

/**
* @param ElementButton $button
*
* @return $this
*/
public function addButton(ElementButton $button)
public function addButton(ElementButton $button): self
{
$this->buttons[] = $button->toArray();

Expand All @@ -44,15 +50,14 @@ public function addButton(ElementButton $button)

/**
* @param array $buttons
*
* @return $this
*/
public function addButtons(array $buttons)
public function addButtons(array $buttons): self
{
if (isset($buttons) && is_array($buttons)) {
foreach ($buttons as $button) {
if ($button instanceof ElementButton) {
$this->buttons[] = $button->toArray();
}
foreach ($buttons as $button) {
if ($button instanceof ElementButton) {
$this->buttons[] = $button->toArray();
}
}

Expand All @@ -62,7 +67,7 @@ public function addButtons(array $buttons)
/**
* @return array
*/
public function toArray()
public function toArray(): array
{
return [
'url' => $this->url,
Expand All @@ -73,7 +78,7 @@ public function toArray()
/**
* @return array
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand Down
37 changes: 29 additions & 8 deletions src/Extensions/OpenGraphTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

class OpenGraphTemplate implements JsonSerializable, WebAccess
{
/** @var string */
/**
* @var string
*/
protected $mediaType;

/** @var array */
/**
* @var array
*/
protected $elements = [];

/**
Expand All @@ -22,20 +26,37 @@ public static function create()
}

/**
* @param $element
* @param OpenGraphElement $element
*
* @return $this
*/
public function addElement(OpenGraphElement $element): self
{
$this->elements[] = $element->toArray();

return $this;
}

/**
* @param OpenGraphElement[] $elements
*
* @return $this
*/
public function element($element)
public function addElements(array $elements): self
{
$this->elements = [$element->toArray()];
foreach ($elements as $element) {
if ($element instanceof OpenGraphElement) {
$this->elements[] = $element->toArray();
}
}

return $this;
}

/**
* @return array
*/
public function toArray()
public function toArray(): array
{
return [
'attachment' => [
Expand All @@ -51,7 +72,7 @@ public function toArray()
/**
* @return array
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand All @@ -62,7 +83,7 @@ public function jsonSerialize()
*
* @return array
*/
public function toWebDriver()
public function toWebDriver(): array
{
return [
'type' => $this->mediaType,
Expand Down
48 changes: 17 additions & 31 deletions tests/Extensions/OpenGraphElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,58 +12,44 @@ class OpenGraphElementTest extends PHPUnit_Framework_TestCase
/** @test */
public function it_can_be_created()
{
$button = new OpenGraphElement('Rick Roll');
$this->assertInstanceOf(Element::class, $button);
$element = new OpenGraphElement();
$this->assertInstanceOf(OpenGraphElement::class, $element);
}

/**
* @test
**/
public function it_can_set_title()
public function it_can_set_an_url()
{
$element = new Element('Rick Roll');
$element = new OpenGraphElement();
$element->url('https://example.com');

$this->assertSame('Rick Roll', Arr::get($element->toArray(), 'title'));
}

/**
* @test
**/
public function it_can_set_url()
{
$element = new Element('Rick Roll');
$element->url('https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb');

$this->assertSame('https://open.spotify.com/track/7GhIk7Il098yCjg4BQjzvb', Arr::get($element->toArray(), 'url'));
$this->assertSame('https://example.com', Arr::get($element->toArray(), 'url'));
}

/**
* @test
**/
public function it_can_add_a_button()
{
$template = new Element('Here are some buttons');
$template->addButton(ElementButton::create('button1')
->url('https://en.wikipedia.org/wiki/Rickrolling')
->removeHeightRatio());
$element = new OpenGraphElement();
$element->addButton(ElementButton::create('button1')->url('https://en.wikipedia.org/wiki/Rickrolling')->removeHeightRatio());

$this->assertSame('button1', Arr::get($template->toArray(), 'buttons.0.title'));
$this->assertSame('button1', Arr::get($element->toArray(), 'buttons.0.title'));
}

/**
* @test
**/
public function it_can_add_multiple_buttons()
{
$template = new Element('Here are some buttons');
$template->addButtons([ElementButton::create('button1')
->url('https://en.wikipedia.org/wiki/Rickrolling')
->removeHeightRatio(),
ElementButton::create('button2')
->url('https://en.wikipedia.org/')
->removeHeightRatio(), ]);

$this->assertSame('button1', Arr::get($template->toArray(), 'buttons.0.title'));
$this->assertSame('button2', Arr::get($template->toArray(), 'buttons.1.title'));
$element = new OpenGraphElement();
$element->addButtons([
ElementButton::create('button1')->url('https://en.wikipedia.org/wiki/Rickrolling')->removeHeightRatio(),
ElementButton::create('button2')->url('https://en.wikipedia.org/')->removeHeightRatio(),
]);

$this->assertSame('button1', Arr::get($element->toArray(), 'buttons.0.title'));
$this->assertSame('button2', Arr::get($element->toArray(), 'buttons.1.title'));
}
}
20 changes: 17 additions & 3 deletions tests/Extensions/OpenGraphTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,23 @@ public function it_can_be_created()
public function it_can_add_an_element()
{
$template = new OpenGraphTemplate;
$template->element(OpenGraphElement::create('Rick Roll'));
$template->addElement(OpenGraphElement::create()->url('https://example.com'));

$this->assertSame('Rick Roll',
Arr::get($template->toArray(), 'attachment.payload.elements.0.title'));
$this->assertSame('https://example.com', Arr::get($template->toArray(), 'attachment.payload.elements.0.url'));
}

/**
* @test
**/
public function it_can_add_multiple_elements()
{
$template = new OpenGraphTemplate;
$template->addElements([
OpenGraphElement::create()->url('https://example.com'),
OpenGraphElement::create()->url('https://example.com'),
]);

$this->assertSame('https://example.com', Arr::get($template->toArray(), 'attachment.payload.elements.0.url'));
$this->assertSame('https://example.com', Arr::get($template->toArray(), 'attachment.payload.elements.1.url'));
}
}

0 comments on commit 8071f77

Please sign in to comment.