Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/Elements/BodyComponents/MjSpacer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/**
* PHP MJML Renderer library
*
* @package MadeByDenis\PhpMjmlRenderer
* @link https://github.com/dingo-d/php-mjml-renderer
* @license https://opensource.org/licenses/MIT MIT
*/

declare(strict_types=1);

namespace MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents;

use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement;

/**
* Mjml Spacer Element
*
* @link https://documentation.mjml.io/#mj-spacer
*
* @since 1.0.0
*/
class MjSpacer extends AbstractElement
{
public const string TAG_NAME = 'mj-spacer';

public const bool ENDING_TAG = false;

/**
* List of allowed attributes on the element
*
* @var array<string, array<string, string>>
*/
protected array $allowedAttributes = [
'container-background-color' => [
'unit' => 'color',
'type' => 'color',
'description' => 'inner element background color',
'default_value' => '',
],
'css-class' => [
'unit' => 'string',
'type' => 'string',
'description' => 'class name added to root HTML element',
'default_value' => '',
],
'height' => [
'unit' => 'px',
'type' => 'measure',
'description' => 'spacer height',
'default_value' => '20px',
],
'padding' => [
'unit' => 'px',
'type' => 'measure',
'description' => 'supports up to 4 parameters',
'default_value' => '',
],
'padding-top' => [
'unit' => 'px',
'type' => 'measure',
'description' => 'top offset',
'default_value' => '',
],
'padding-bottom' => [
'unit' => 'px',
'type' => 'measure',
'description' => 'bottom offset',
'default_value' => '',
],
'padding-left' => [
'unit' => 'px',
'type' => 'measure',
'description' => 'left offset',
'default_value' => '',
],
'padding-right' => [
'unit' => 'px',
'type' => 'measure',
'description' => 'right offset',
'default_value' => '',
],
];

protected array $defaultAttributes = [
'height' => '20px',
];

public function render(): string
{
$divAttributes = $this->getHtmlAttributes([
'style' => 'div',
]);

return "<div $divAttributes>&nbsp;</div>";
}

/**
* @return array<string, array<string, string>>
*/
public function getStyles(): array
{
return [
'div' => [
'height' => $this->getAttribute('height'),
'line-height' => $this->getAttribute('height'),
],
];
}
}
117 changes: 117 additions & 0 deletions tests/Unit/Elements/BodyComponents/MjSpacerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace MadeByDenis\PhpMjmlRenderer\Tests\Unit\Elements\BodyComponents;

use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjSpacer;
use MadeByDenis\PhpMjmlRenderer\Elements\ElementFactory;
use MadeByDenis\PhpMjmlRenderer\Parser\MjmlNode;
use OutOfBoundsException;

beforeEach(function () {
$this->element = new MjSpacer();
});

it('is not ending tag', function () {
expect($this->element->isEndingTag())->toBe(false);
});

it('returns the correct component name', function () {
expect($this->element->getTagName())->toBe('mj-spacer');
});

it('returns the correct default attributes', function () {
$attributes = [
'height' => '20px',
];

foreach ($attributes as $key => $value) {
expect($this->element->getAttribute($key))->toBe($value);
}
});

it('will throw out of bounds exception if the allowed attribute is not existing', function () {
$this->element->getAllowedAttributeData('invalid-attribute');
})->throws(OutOfBoundsException::class);

it('will return allowed attribute data', function () {
$data = $this->element->getAllowedAttributeData('height');
expect($data)->toBeArray();
expect($data)->toHaveKey('type');
expect($data)->toHaveKey('unit');
});

it('will correctly render a simple spacer', function () {
$spacerNode = new MjmlNode(
'mj-spacer',
null,
null,
false,
null
);

$factory = new ElementFactory();
$mjSpacerElement = $factory->create($spacerNode);

expect($mjSpacerElement)->toBeInstanceOf(MjSpacer::class);

$out = $mjSpacerElement->render();

expect($out)->toContain('<div');
expect($out)->toContain('height');
expect($out)->toContain('20px');
expect($out)->not->toBeEmpty();
});

it('will correctly render a spacer with custom height', function () {
$spacerNode = new MjmlNode(
'mj-spacer',
['height' => '50px'],
null,
false,
null
);

$factory = new ElementFactory();
$mjSpacerElement = $factory->create($spacerNode);

$out = $mjSpacerElement->render();

expect($out)->toContain('50px');
expect($out)->not->toBeEmpty();
});

it('will correctly render a spacer with small height', function () {
$spacerNode = new MjmlNode(
'mj-spacer',
['height' => '10px'],
null,
false,
null
);

$factory = new ElementFactory();
$mjSpacerElement = $factory->create($spacerNode);

$out = $mjSpacerElement->render();

expect($out)->toContain('10px');
expect($out)->not->toBeEmpty();
});

it('will correctly render a spacer with large height', function () {
$spacerNode = new MjmlNode(
'mj-spacer',
['height' => '100px'],
null,
false,
null
);

$factory = new ElementFactory();
$mjSpacerElement = $factory->create($spacerNode);

$out = $mjSpacerElement->render();

expect($out)->toContain('100px');
expect($out)->not->toBeEmpty();
});