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