Skip to content

Commit 253121d

Browse files
authored
Merge pull request #22 from dingo-d/feature/mj-spacer
Implement mj-spacer element
2 parents 9c2a600 + b00cd8e commit 253121d

File tree

2 files changed

+228
-0
lines changed

2 files changed

+228
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
/**
4+
* PHP MJML Renderer library
5+
*
6+
* @package MadeByDenis\PhpMjmlRenderer
7+
* @link https://github.com/dingo-d/php-mjml-renderer
8+
* @license https://opensource.org/licenses/MIT MIT
9+
*/
10+
11+
declare(strict_types=1);
12+
13+
namespace MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents;
14+
15+
use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement;
16+
17+
/**
18+
* Mjml Spacer Element
19+
*
20+
* @link https://documentation.mjml.io/#mj-spacer
21+
*
22+
* @since 1.0.0
23+
*/
24+
class MjSpacer extends AbstractElement
25+
{
26+
public const string TAG_NAME = 'mj-spacer';
27+
28+
public const bool ENDING_TAG = false;
29+
30+
/**
31+
* List of allowed attributes on the element
32+
*
33+
* @var array<string, array<string, string>>
34+
*/
35+
protected array $allowedAttributes = [
36+
'container-background-color' => [
37+
'unit' => 'color',
38+
'type' => 'color',
39+
'description' => 'inner element background color',
40+
'default_value' => '',
41+
],
42+
'css-class' => [
43+
'unit' => 'string',
44+
'type' => 'string',
45+
'description' => 'class name added to root HTML element',
46+
'default_value' => '',
47+
],
48+
'height' => [
49+
'unit' => 'px',
50+
'type' => 'measure',
51+
'description' => 'spacer height',
52+
'default_value' => '20px',
53+
],
54+
'padding' => [
55+
'unit' => 'px',
56+
'type' => 'measure',
57+
'description' => 'supports up to 4 parameters',
58+
'default_value' => '',
59+
],
60+
'padding-top' => [
61+
'unit' => 'px',
62+
'type' => 'measure',
63+
'description' => 'top offset',
64+
'default_value' => '',
65+
],
66+
'padding-bottom' => [
67+
'unit' => 'px',
68+
'type' => 'measure',
69+
'description' => 'bottom offset',
70+
'default_value' => '',
71+
],
72+
'padding-left' => [
73+
'unit' => 'px',
74+
'type' => 'measure',
75+
'description' => 'left offset',
76+
'default_value' => '',
77+
],
78+
'padding-right' => [
79+
'unit' => 'px',
80+
'type' => 'measure',
81+
'description' => 'right offset',
82+
'default_value' => '',
83+
],
84+
];
85+
86+
protected array $defaultAttributes = [
87+
'height' => '20px',
88+
];
89+
90+
public function render(): string
91+
{
92+
$divAttributes = $this->getHtmlAttributes([
93+
'style' => 'div',
94+
]);
95+
96+
return "<div $divAttributes>&nbsp;</div>";
97+
}
98+
99+
/**
100+
* @return array<string, array<string, string>>
101+
*/
102+
public function getStyles(): array
103+
{
104+
return [
105+
'div' => [
106+
'height' => $this->getAttribute('height'),
107+
'line-height' => $this->getAttribute('height'),
108+
],
109+
];
110+
}
111+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace MadeByDenis\PhpMjmlRenderer\Tests\Unit\Elements\BodyComponents;
4+
5+
use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjSpacer;
6+
use MadeByDenis\PhpMjmlRenderer\Elements\ElementFactory;
7+
use MadeByDenis\PhpMjmlRenderer\Parser\MjmlNode;
8+
use OutOfBoundsException;
9+
10+
beforeEach(function () {
11+
$this->element = new MjSpacer();
12+
});
13+
14+
it('is not ending tag', function () {
15+
expect($this->element->isEndingTag())->toBe(false);
16+
});
17+
18+
it('returns the correct component name', function () {
19+
expect($this->element->getTagName())->toBe('mj-spacer');
20+
});
21+
22+
it('returns the correct default attributes', function () {
23+
$attributes = [
24+
'height' => '20px',
25+
];
26+
27+
foreach ($attributes as $key => $value) {
28+
expect($this->element->getAttribute($key))->toBe($value);
29+
}
30+
});
31+
32+
it('will throw out of bounds exception if the allowed attribute is not existing', function () {
33+
$this->element->getAllowedAttributeData('invalid-attribute');
34+
})->throws(OutOfBoundsException::class);
35+
36+
it('will return allowed attribute data', function () {
37+
$data = $this->element->getAllowedAttributeData('height');
38+
expect($data)->toBeArray();
39+
expect($data)->toHaveKey('type');
40+
expect($data)->toHaveKey('unit');
41+
});
42+
43+
it('will correctly render a simple spacer', function () {
44+
$spacerNode = new MjmlNode(
45+
'mj-spacer',
46+
null,
47+
null,
48+
false,
49+
null
50+
);
51+
52+
$factory = new ElementFactory();
53+
$mjSpacerElement = $factory->create($spacerNode);
54+
55+
expect($mjSpacerElement)->toBeInstanceOf(MjSpacer::class);
56+
57+
$out = $mjSpacerElement->render();
58+
59+
expect($out)->toContain('<div');
60+
expect($out)->toContain('height');
61+
expect($out)->toContain('20px');
62+
expect($out)->not->toBeEmpty();
63+
});
64+
65+
it('will correctly render a spacer with custom height', function () {
66+
$spacerNode = new MjmlNode(
67+
'mj-spacer',
68+
['height' => '50px'],
69+
null,
70+
false,
71+
null
72+
);
73+
74+
$factory = new ElementFactory();
75+
$mjSpacerElement = $factory->create($spacerNode);
76+
77+
$out = $mjSpacerElement->render();
78+
79+
expect($out)->toContain('50px');
80+
expect($out)->not->toBeEmpty();
81+
});
82+
83+
it('will correctly render a spacer with small height', function () {
84+
$spacerNode = new MjmlNode(
85+
'mj-spacer',
86+
['height' => '10px'],
87+
null,
88+
false,
89+
null
90+
);
91+
92+
$factory = new ElementFactory();
93+
$mjSpacerElement = $factory->create($spacerNode);
94+
95+
$out = $mjSpacerElement->render();
96+
97+
expect($out)->toContain('10px');
98+
expect($out)->not->toBeEmpty();
99+
});
100+
101+
it('will correctly render a spacer with large height', function () {
102+
$spacerNode = new MjmlNode(
103+
'mj-spacer',
104+
['height' => '100px'],
105+
null,
106+
false,
107+
null
108+
);
109+
110+
$factory = new ElementFactory();
111+
$mjSpacerElement = $factory->create($spacerNode);
112+
113+
$out = $mjSpacerElement->render();
114+
115+
expect($out)->toContain('100px');
116+
expect($out)->not->toBeEmpty();
117+
});

0 commit comments

Comments
 (0)