| [ Index ] |
PHP Cross Reference of Web Application Component Toolkit |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @package WACT_TESTS 4 * @version $Id: selectsingle.test.php,v 1.7 2004/11/17 00:41:37 jeffmoore Exp $ 5 */ 6 /** 7 * Includes 8 */ 9 require_once WACT_ROOT . 'template/template.inc.php'; 10 require_once WACT_ROOT . 'template/components/form/form.inc.php'; 11 12 require_once TEST_CASES.'/template/components.test.php'; 13 14 /** 15 * @package WACT_TESTS 16 */ 17 class SelectSingleComponentTestCase extends TagComponentTestCase { 18 19 function SelectSingleComponentTestCase ($name = 'SelectSingleComponentTestCase') { 20 $this->UnitTestCase($name); 21 } 22 23 /** 24 * @todo Should the first element of the index really be selected automatically ? 25 */ 26 function testSetChoicesWithIndex() { 27 28 $Template = '<form id="testForm" runat="server"> 29 <select id="test" name="mySelect" runat="server"></select> 30 </form>'; 31 RegisterTestingTemplate('/components/form/selectsingle/setchoiceswithindex.html', $Template); 32 33 $Page =& new Template('/components/form/selectsingle/setchoiceswithindex.html'); 34 35 $choices = array('red','green','blue'); 36 $Select =& $Page->getChild('test'); 37 $Select->setChoices($choices); 38 39 /** 40 * Please note this test should fail right now ($Id: selectsingle.test.php,v 1.7 2004/11/17 00:41:37 jeffmoore Exp $) 41 * An array with index 0 is getting selected when it's not selected 42 * If it can be done better with a regex, pls do (I hate regexs) 43 */ 44 $testOut = ''; 45 foreach ( $choices as $key => $choice ) { 46 $testOut .= '<option value="'.$key.'"'; 47 $testOut .='>'.$key.'</option>'; 48 } 49 50 $Select =& $Page->getChild('test'); 51 52 $Select->setChoices($choices); 53 54 $output = $Page->capture(); 55 $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output); 56 $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output); 57 $this->assertNoUnwantedPattern('~<option\s+value="0"(?U)[^>]*selected[^>]*>[^<]*</option>~ims', $output); 58 } 59 60 61 function testSetChoicesWithHash() { 62 63 $Template = '<form id="testForm" runat="server"> 64 <select id="test" name="mySelect" runat="server"></select> 65 </form>'; 66 RegisterTestingTemplate('/components/form/selectsingle/setchoiceswithhash.html', $Template); 67 68 $Page =& new Template('/components/form/selectsingle/setchoiceswithhash.html'); 69 70 $choices = array('a'=>'red','b'=>'green','c'=>'blue'); 71 $Select =& $Page->getChild('test'); 72 $Select->setChoices($choices); 73 74 $output = $Page->capture(); 75 $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output); 76 $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[a-c]"[^>]*>[^<]*</option>)+.*</select>~ims', $output); 77 } 78 79 function testSetSelectionWithIndex() { 80 81 $Template = '<form id="testForm" runat="server"> 82 <select id="test" name="mySelect" runat="server"></select> 83 </form>'; 84 RegisterTestingTemplate('/components/form/selectsingle/setselectionwithindex.html', $Template); 85 86 $Page =& new Template('/components/form/selectsingle/setselectionwithindex.html'); 87 88 $choices = array('red','green','blue'); 89 $selectedKey = '1'; 90 $Select =& $Page->getChild('test'); 91 $Select->setChoices($choices); 92 $Select->setSelection($selectedKey); 93 94 $output = $Page->capture(); 95 $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output); 96 $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output); 97 $this->assertWantedPattern('~<option[^>]+value="1"[^>]+selected[^>]*>green</option>~ims', $output); 98 } 99 100 function testSetSelectionWithIndexByForm() { 101 102 $Template = '<form id="testForm" runat="server"> 103 <select id="test" name="mySelect" runat="server"></select> 104 </form>'; 105 RegisterTestingTemplate('/components/form/selectsingle/setselectionwithindexbyform.html', $Template); 106 107 $Page =& new Template('/components/form/selectsingle/setselectionwithindexbyform.html'); 108 109 $Form =& $Page->getChild('testForm'); 110 111 $choices = array('red','green','blue'); 112 $selectedKey = '1'; 113 $Select =& $Page->getChild('test'); 114 $Select->setChoices($choices); 115 116 $data =& new DataSpace(); 117 $data->set('mySelect', $selectedKey); 118 119 $Form->registerDataSource($data); 120 121 122 $output = $Page->capture(); 123 $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output); 124 $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="\d+"[^>]*>[^<]*</option>)+.*</select>~ims', $output); 125 $this->assertWantedPattern('~<option[^>]+value="1"[^>]+selected[^>]*>green</option>~ims', $output); 126 } 127 128 function testSetSelectionWithHash() { 129 130 $Template = '<form id="testForm" runat="server"> 131 <select id="test" name="mySelect" runat="server"></select> 132 </form>'; 133 RegisterTestingTemplate('/components/form/selectsingle/setselectionwithhash.html', $Template); 134 135 $Page =& new Template('/components/form/selectsingle/setselectionwithhash.html'); 136 137 $choices = array('a'=>'red','b'=>'green','c'=>'blue'); 138 $selectedKey = 'b'; 139 $Select =& $Page->getChild('test'); 140 $Select->setChoices($choices); 141 $Select->setSelection('b'); 142 143 $output = $Page->capture(); 144 $this->assertWantedPattern('~<form[^>]+id="testForm"[^>]*>.*</form>$~ims', $output); 145 $this->assertWantedPattern('~<select[^>]+id="test"[^>]*>(\s*<option\svalue="[a-c]"[^>]*>[^<]*</option>)+.*</select>~ims', $output); 146 $this->assertWantedPattern('~<option[^>]+value="b"[^>]+selected[^>]*>green</option>~ims', $output); 147 } 148 149 /************************************************************ 150 Tests below use the API as it's expected to be used 151 ************************************************************/ 152 153 function testSetChoicesWithKeys() { 154 155 $Template = '<form id="testForm" runat="server"> 156 <select id="test" name="mySelect" runat="server"></select> 157 </form>'; 158 RegisterTestingTemplate('/components/form/selectsingle/setchoiceswithkeys.html', $Template); 159 160 $Page =& new Template('/components/form/selectsingle/setchoiceswithkeys.html'); 161 162 $choices = array('red'=>'','green'=>'','blue'=>''); 163 164 $testOut = ''; 165 foreach ( $choices as $key => $choice ) { 166 $testOut .= '<option value="'.$key.'"'; 167 $testOut .='>'.$key.'</option>'; 168 } 169 170 $Select =& $Page->getChild('test'); 171 172 // Array flip? 173 $Select->setChoices($choices); 174 175 ob_start(); 176 $Select->renderContents(); 177 $out = ob_get_contents(); 178 ob_end_clean(); 179 180 $this->assertEqual($out,$testOut); 181 182 } 183 184 function testSetChoicesWithKeysSelection() { 185 186 $Template = '<form id="testForm" runat="server"> 187 <select id="test" name="mySelect" runat="server"></select> 188 </form>'; 189 RegisterTestingTemplate('/components/form/selectsingle/setchoiceswithkeysselection.html', $Template); 190 191 $Page =& new Template('/components/form/selectsingle/setchoiceswithkeysselection.html'); 192 193 $choices = array('red'=>'','green'=>'','blue'=>''); 194 $selected = 'green'; 195 196 $testOut = ''; 197 198 foreach ( $choices as $key => $choice ) { 199 $testOut .= '<option value="'.$key.'"'; 200 if ( $key == $selected ) { 201 $testOut .= ' selected'; 202 } 203 $testOut .='>'.$key.'</option>'; 204 } 205 206 $Select =& $Page->getChild('test'); 207 208 $Select->setChoices($choices); 209 210 $Select->setSelection($selected); 211 212 ob_start(); 213 $Select->renderContents(); 214 $out = ob_get_contents(); 215 ob_end_clean(); 216 217 $this->assertEqual($out,$testOut); 218 219 } 220 221 } 222 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sun Nov 28 19:36:09 2004 | Cross-referenced by PHPXref 0.5 |