1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 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 20use Fisharebest\Webtrees\Menu; 21 22/** 23 * Test harness for the class Menu 24 */ 25class MenuTest extends \Fisharebest\Webtrees\TestCase 26{ 27 /** 28 * Prepare the environment for these tests. 29 * 30 * @return void 31 */ 32 public function setUp() 33 { 34 } 35 36 /** 37 * Test the constructor with default parameters. 38 * 39 * @return void 40 */ 41 public function testConstructorDefaults() 42 { 43 $menu = new Menu('Test!'); 44 45 $this->assertSame('Test!', $menu->getLabel()); 46 $this->assertSame('#', $menu->getLink()); 47 $this->assertSame('', $menu->getClass()); 48 $this->assertSame([], $menu->getAttrs()); 49 $this->assertSame([], $menu->getSubmenus()); 50 } 51 52 /** 53 * Test the constructor with non-default parameters. 54 * 55 * @return void 56 */ 57 public function testConstructorNonDefaults() 58 { 59 $submenus = [new Menu('Submenu')]; 60 $menu = new Menu('Test!', 'link.html', 'link-class', ['foo' => 'bar'], $submenus); 61 62 $this->assertSame('Test!', $menu->getLabel()); 63 $this->assertSame('link.html', $menu->getLink()); 64 $this->assertSame('link-class', $menu->getClass()); 65 $this->assertSame(['foo' => 'bar'], $menu->getAttrs()); 66 $this->assertSame($submenus, $menu->getSubmenus()); 67 } 68 69 /** 70 * Test the getter/setter for the label. 71 * 72 * @return void 73 */ 74 public function testGetterSetterLabel() 75 { 76 $menu = new Menu('Test!'); 77 78 $return = $menu->setLabel('Label'); 79 80 $this->assertSame($return, $menu); 81 $this->assertSame('Label', $menu->getLabel()); 82 } 83 84 /** 85 * Test the getter/setter for the link. 86 * 87 * @return void 88 */ 89 public function testGetterSetterLink() 90 { 91 $menu = new Menu('Test!'); 92 93 $return = $menu->setLink('link.html'); 94 95 $this->assertSame($return, $menu); 96 $this->assertSame('link.html', $menu->getLink()); 97 } 98 99 /** 100 * Test the getter/setter for the ID. 101 * 102 * @return void 103 */ 104 public function testGetterSetterId() 105 { 106 $menu = new Menu('Test!'); 107 108 $return = $menu->setClass('link-class'); 109 110 $this->assertSame($return, $menu); 111 $this->assertSame('link-class', $menu->getClass()); 112 } 113 114 /** 115 * Test the getter/setter for the Attrs event. 116 * 117 * @return void 118 */ 119 public function testGetterSetterAttrs() 120 { 121 $menu = new Menu('Test!'); 122 123 $return = $menu->setAttrs(['foo' => 'bar']); 124 125 $this->assertSame($return, $menu); 126 $this->assertSame(['foo' => 'bar'], $menu->getAttrs()); 127 } 128 129 /** 130 * Test the getter/setter for the submenus. 131 * 132 * @return void 133 */ 134 public function testGetterSetterSubmenus() 135 { 136 $menu = new Menu('Test!'); 137 $submenus = [ 138 new Menu('Sub1'), 139 new Menu('Sub2'), 140 ]; 141 142 $return = $menu->setSubmenus($submenus); 143 144 $this->assertSame($return, $menu); 145 $this->assertSame($submenus, $menu->getSubmenus()); 146 } 147 148 /** 149 * Test the list rendering for a simple link. 150 * 151 * @return void 152 */ 153 public function testFormatAsList() 154 { 155 $menu = new Menu('Test!', 'link.html'); 156 157 $this->assertSame('<li class=""><a href="link.html">Test!</a></li>', $menu->getMenuAsList()); 158 } 159 160 /** 161 * Test the list rendering for a simple link with a CSS ID. 162 * 163 * @return void 164 */ 165 public function testFormatAsListWithClass() 166 { 167 $menu = new Menu('Test!', 'link.html', 'link-class'); 168 169 $this->assertSame('<li class="link-class"><a href="link.html">Test!</a></li>', $menu->getMenuAsList()); 170 } 171 172 /** 173 * Test the list rendering for an empty target. 174 * 175 * @return void 176 */ 177 public function testFormatAsListWithNoTarget() 178 { 179 $menu = new Menu('Test!', ''); 180 181 $this->assertSame('<li class=""><a>Test!</a></li>', $menu->getMenuAsList()); 182 } 183 184 /** 185 * Test the list rendering for a default (hash) target. 186 * 187 * @return void 188 */ 189 public function testFormatAsListWithHashTarget() 190 { 191 $menu = new Menu('Test!'); 192 193 $this->assertSame('<li class=""><a href="#">Test!</a></li>', $menu->getMenuAsList()); 194 } 195 196 /** 197 * Test the list rendering for an onclick link. 198 * 199 * @return void 200 */ 201 public function testFormatAsListWithAttrs() 202 { 203 $menu = new Menu('Test!', '#', '', ['foo' => 'bar']); 204 205 $this->assertSame('<li class=""><a href="#" foo="bar">Test!</a></li>', $menu->getMenuAsList()); 206 } 207 208 /** 209 * Test the list rendering for an onclick link. 210 * 211 * @return void 212 */ 213 public function testFormatAsListWithAttrsAndId() 214 { 215 $menu = new Menu('Test!', '#', 'link-class', ['foo' => 'bar']); 216 217 $this->assertSame('<li class="link-class"><a href="#" foo="bar">Test!</a></li>', $menu->getMenuAsList()); 218 } 219} 220