|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MadeByDenis\PhpMjmlRenderer\Tests\Unit\Elements\BodyComponents; |
| 4 | + |
| 5 | +use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjDivider; |
| 6 | +use MadeByDenis\PhpMjmlRenderer\Elements\ElementFactory; |
| 7 | +use MadeByDenis\PhpMjmlRenderer\Parser\MjmlNode; |
| 8 | +use OutOfBoundsException; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->element = new MjDivider(); |
| 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-divider'); |
| 20 | +}); |
| 21 | + |
| 22 | +it('returns the correct default attributes', function () { |
| 23 | + $attributes = [ |
| 24 | + 'border-color' => '#000000', |
| 25 | + 'border-style' => 'solid', |
| 26 | + 'border-width' => '4px', |
| 27 | + 'padding' => '10px 25px', |
| 28 | + 'width' => '100%', |
| 29 | + ]; |
| 30 | + |
| 31 | + foreach ($attributes as $key => $value) { |
| 32 | + expect($this->element->getAttribute($key))->toBe($value); |
| 33 | + } |
| 34 | +}); |
| 35 | + |
| 36 | +it('will throw out of bounds exception if the allowed attribute is not existing', function () { |
| 37 | + $this->element->getAllowedAttributeData('invalid-attribute'); |
| 38 | +})->throws(OutOfBoundsException::class); |
| 39 | + |
| 40 | +it('will return allowed attribute data', function () { |
| 41 | + $data = $this->element->getAllowedAttributeData('border-color'); |
| 42 | + expect($data)->toBeArray(); |
| 43 | + expect($data)->toHaveKey('type'); |
| 44 | + expect($data)->toHaveKey('unit'); |
| 45 | +}); |
| 46 | + |
| 47 | +it('will correctly render a simple divider', function () { |
| 48 | + $dividerNode = new MjmlNode( |
| 49 | + 'mj-divider', |
| 50 | + null, |
| 51 | + null, |
| 52 | + false, |
| 53 | + null |
| 54 | + ); |
| 55 | + |
| 56 | + $factory = new ElementFactory(); |
| 57 | + $mjDividerElement = $factory->create($dividerNode); |
| 58 | + |
| 59 | + expect($mjDividerElement)->toBeInstanceOf(MjDivider::class); |
| 60 | + |
| 61 | + $out = $mjDividerElement->render(); |
| 62 | + |
| 63 | + expect($out)->toContain('<p'); |
| 64 | + expect($out)->toContain('border-top'); |
| 65 | + expect($out)->not->toBeEmpty(); |
| 66 | +}); |
| 67 | + |
| 68 | +it('will correctly render a divider with custom color', function () { |
| 69 | + $dividerNode = new MjmlNode( |
| 70 | + 'mj-divider', |
| 71 | + ['border-color' => '#ff0000'], |
| 72 | + null, |
| 73 | + false, |
| 74 | + null |
| 75 | + ); |
| 76 | + |
| 77 | + $factory = new ElementFactory(); |
| 78 | + $mjDividerElement = $factory->create($dividerNode); |
| 79 | + |
| 80 | + $out = $mjDividerElement->render(); |
| 81 | + |
| 82 | + expect($out)->toContain('#ff0000'); |
| 83 | + expect($out)->not->toBeEmpty(); |
| 84 | +}); |
| 85 | + |
| 86 | +it('will correctly render a divider with custom width', function () { |
| 87 | + $dividerNode = new MjmlNode( |
| 88 | + 'mj-divider', |
| 89 | + ['border-width' => '2px'], |
| 90 | + null, |
| 91 | + false, |
| 92 | + null |
| 93 | + ); |
| 94 | + |
| 95 | + $factory = new ElementFactory(); |
| 96 | + $mjDividerElement = $factory->create($dividerNode); |
| 97 | + |
| 98 | + $out = $mjDividerElement->render(); |
| 99 | + |
| 100 | + expect($out)->toContain('2px'); |
| 101 | + expect($out)->not->toBeEmpty(); |
| 102 | +}); |
| 103 | + |
| 104 | +it('will correctly render a divider with dashed style', function () { |
| 105 | + $dividerNode = new MjmlNode( |
| 106 | + 'mj-divider', |
| 107 | + ['border-style' => 'dashed'], |
| 108 | + null, |
| 109 | + false, |
| 110 | + null |
| 111 | + ); |
| 112 | + |
| 113 | + $factory = new ElementFactory(); |
| 114 | + $mjDividerElement = $factory->create($dividerNode); |
| 115 | + |
| 116 | + $out = $mjDividerElement->render(); |
| 117 | + |
| 118 | + expect($out)->toContain('dashed'); |
| 119 | + expect($out)->not->toBeEmpty(); |
| 120 | +}); |
| 121 | + |
| 122 | +it('will correctly render a divider with dotted style', function () { |
| 123 | + $dividerNode = new MjmlNode( |
| 124 | + 'mj-divider', |
| 125 | + ['border-style' => 'dotted'], |
| 126 | + null, |
| 127 | + false, |
| 128 | + null |
| 129 | + ); |
| 130 | + |
| 131 | + $factory = new ElementFactory(); |
| 132 | + $mjDividerElement = $factory->create($dividerNode); |
| 133 | + |
| 134 | + $out = $mjDividerElement->render(); |
| 135 | + |
| 136 | + expect($out)->toContain('dotted'); |
| 137 | + expect($out)->not->toBeEmpty(); |
| 138 | +}); |
| 139 | + |
| 140 | +it('will correctly render a divider with custom width percentage', function () { |
| 141 | + $dividerNode = new MjmlNode( |
| 142 | + 'mj-divider', |
| 143 | + ['width' => '50%'], |
| 144 | + null, |
| 145 | + false, |
| 146 | + null |
| 147 | + ); |
| 148 | + |
| 149 | + $factory = new ElementFactory(); |
| 150 | + $mjDividerElement = $factory->create($dividerNode); |
| 151 | + |
| 152 | + $out = $mjDividerElement->render(); |
| 153 | + |
| 154 | + expect($out)->toContain('width'); |
| 155 | + expect($out)->toContain('50%'); |
| 156 | + expect($out)->not->toBeEmpty(); |
| 157 | +}); |
| 158 | + |
| 159 | +it('will correctly render a divider with all custom properties', function () { |
| 160 | + $dividerNode = new MjmlNode( |
| 161 | + 'mj-divider', |
| 162 | + [ |
| 163 | + 'border-color' => '#cccccc', |
| 164 | + 'border-style' => 'dashed', |
| 165 | + 'border-width' => '1px', |
| 166 | + 'width' => '80%', |
| 167 | + ], |
| 168 | + null, |
| 169 | + false, |
| 170 | + null |
| 171 | + ); |
| 172 | + |
| 173 | + $factory = new ElementFactory(); |
| 174 | + $mjDividerElement = $factory->create($dividerNode); |
| 175 | + |
| 176 | + $out = $mjDividerElement->render(); |
| 177 | + |
| 178 | + expect($out)->toContain('#cccccc'); |
| 179 | + expect($out)->toContain('dashed'); |
| 180 | + expect($out)->toContain('1px'); |
| 181 | + expect($out)->toContain('80%'); |
| 182 | + expect($out)->not->toBeEmpty(); |
| 183 | +}); |
0 commit comments