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