1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2019 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees; 19 20/** 21 * Test harness for the class Menu 22 */ 23class MenuTest extends \Fisharebest\Webtrees\TestCase 24{ 25 /** 26 * Test the constructor with default parameters. 27 * 28 * @return void 29 */ 30 public function testConstructorDefaults() 31 { 32 $menu = new Menu('Test!'); 33 34 $this->assertSame('Test!', $menu->getLabel()); 35 $this->assertSame('#', $menu->getLink()); 36 $this->assertSame('', $menu->getClass()); 37 $this->assertSame([], $menu->getAttrs()); 38 $this->assertSame([], $menu->getSubmenus()); 39 } 40 41 /** 42 * Test the constructor with non-default parameters. 43 * 44 * @return void 45 */ 46 public function testConstructorNonDefaults() 47 { 48 $submenus = [new Menu('Submenu')]; 49 $menu = new Menu('Test!', 'link.html', 'link-class', ['foo' => 'bar'], $submenus); 50 51 $this->assertSame('Test!', $menu->getLabel()); 52 $this->assertSame('link.html', $menu->getLink()); 53 $this->assertSame('link-class', $menu->getClass()); 54 $this->assertSame(['foo' => 'bar'], $menu->getAttrs()); 55 $this->assertSame($submenus, $menu->getSubmenus()); 56 } 57 58 /** 59 * Test the getter/setter for the label. 60 * 61 * @return void 62 */ 63 public function testGetterSetterLabel() 64 { 65 $menu = new Menu('Test!'); 66 67 $return = $menu->setLabel('Label'); 68 69 $this->assertSame($return, $menu); 70 $this->assertSame('Label', $menu->getLabel()); 71 } 72 73 /** 74 * Test the getter/setter for the link. 75 * 76 * @return void 77 */ 78 public function testGetterSetterLink() 79 { 80 $menu = new Menu('Test!'); 81 82 $return = $menu->setLink('link.html'); 83 84 $this->assertSame($return, $menu); 85 $this->assertSame('link.html', $menu->getLink()); 86 } 87 88 /** 89 * Test the getter/setter for the ID. 90 * 91 * @return void 92 */ 93 public function testGetterSetterId() 94 { 95 $menu = new Menu('Test!'); 96 97 $return = $menu->setClass('link-class'); 98 99 $this->assertSame($return, $menu); 100 $this->assertSame('link-class', $menu->getClass()); 101 } 102 103 /** 104 * Test the getter/setter for the Attrs event. 105 * 106 * @return void 107 */ 108 public function testGetterSetterAttrs() 109 { 110 $menu = new Menu('Test!'); 111 112 $return = $menu->setAttrs(['foo' => 'bar']); 113 114 $this->assertSame($return, $menu); 115 $this->assertSame(['foo' => 'bar'], $menu->getAttrs()); 116 } 117 118 /** 119 * Test the getter/setter for the submenus. 120 * 121 * @return void 122 */ 123 public function testGetterSetterSubmenus() 124 { 125 $menu = new Menu('Test!'); 126 $submenus = [ 127 new Menu('Sub1'), 128 new Menu('Sub2'), 129 ]; 130 131 $return = $menu->setSubmenus($submenus); 132 133 $this->assertSame($return, $menu); 134 $this->assertSame($submenus, $menu->getSubmenus()); 135 } 136 137 /** 138 * Test the list rendering for a simple link. 139 * 140 * @return void 141 */ 142 public function testFormatAsList() 143 { 144 $menu = new Menu('Test!', 'link.html'); 145 146 $this->assertSame('<li class=""><a href="link.html">Test!</a></li>', $menu->getMenuAsList()); 147 } 148 149 /** 150 * Test the list rendering for a simple link with a CSS ID. 151 * 152 * @return void 153 */ 154 public function testFormatAsListWithClass() 155 { 156 $menu = new Menu('Test!', 'link.html', 'link-class'); 157 158 $this->assertSame('<li class="link-class"><a href="link.html">Test!</a></li>', $menu->getMenuAsList()); 159 } 160 161 /** 162 * Test the list rendering for an empty target. 163 * 164 * @return void 165 */ 166 public function testFormatAsListWithNoTarget() 167 { 168 $menu = new Menu('Test!', ''); 169 170 $this->assertSame('<li class=""><a>Test!</a></li>', $menu->getMenuAsList()); 171 } 172 173 /** 174 * Test the list rendering for a default (hash) target. 175 * 176 * @return void 177 */ 178 public function testFormatAsListWithHashTarget() 179 { 180 $menu = new Menu('Test!'); 181 182 $this->assertSame('<li class=""><a href="#">Test!</a></li>', $menu->getMenuAsList()); 183 } 184 185 /** 186 * Test the list rendering for an onclick link. 187 * 188 * @return void 189 */ 190 public function testFormatAsListWithAttrs() 191 { 192 $menu = new Menu('Test!', '#', '', ['foo' => 'bar']); 193 194 $this->assertSame('<li class=""><a href="#" foo="bar">Test!</a></li>', $menu->getMenuAsList()); 195 } 196 197 /** 198 * Test the list rendering for an onclick link. 199 * 200 * @return void 201 */ 202 public function testFormatAsListWithAttrsAndId() 203 { 204 $menu = new Menu('Test!', '#', 'link-class', ['foo' => 'bar']); 205 206 $this->assertSame('<li class="link-class"><a href="#" foo="bar">Test!</a></li>', $menu->getMenuAsList()); 207 } 208} 209