pictcode / lib / Cake / Test / Case / View / Helper / MootoolsEngineHelperTest.php @ c69e6898
履歴 | 表示 | アノテート | ダウンロード (12.037 KB)
| 1 | 635eef61 | spyder1211 | <?php
|
|---|---|---|---|
| 2 | /**
|
||
| 3 | * MooEngineTestCase
|
||
| 4 | *
|
||
| 5 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||
| 6 | * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
| 7 | *
|
||
| 8 | * Licensed under The MIT License
|
||
| 9 | * For full copyright and license information, please see the LICENSE.txt
|
||
| 10 | * Redistributions of files must retain the above copyright notice.
|
||
| 11 | *
|
||
| 12 | * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
||
| 13 | * @link http://cakephp.org CakePHP Project
|
||
| 14 | * @package Cake.Test.Case.View.Helper
|
||
| 15 | * @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||
| 16 | */
|
||
| 17 | |||
| 18 | App::uses('View', 'View'); |
||
| 19 | App::uses('HtmlHelper', 'View/Helper'); |
||
| 20 | App::uses('JsHelper', 'View/Helper'); |
||
| 21 | App::uses('MootoolsEngineHelper', 'View/Helper'); |
||
| 22 | |||
| 23 | /**
|
||
| 24 | * Class MootoolsEngineHelperTest
|
||
| 25 | *
|
||
| 26 | * @package Cake.Test.Case.View.Helper
|
||
| 27 | */
|
||
| 28 | class MootoolsEngineHelperTest extends CakeTestCase { |
||
| 29 | |||
| 30 | /**
|
||
| 31 | * setUp
|
||
| 32 | *
|
||
| 33 | * @return void
|
||
| 34 | */
|
||
| 35 | public function setUp() { |
||
| 36 | parent::setUp();
|
||
| 37 | $controller = null; |
||
| 38 | $this->View = $this->getMock('View', array('addScript'), array(&$controller)); |
||
| 39 | $this->Moo = new MootoolsEngineHelper($this->View); |
||
| 40 | } |
||
| 41 | |||
| 42 | /**
|
||
| 43 | * tearDown
|
||
| 44 | *
|
||
| 45 | * @return void
|
||
| 46 | */
|
||
| 47 | public function tearDown() { |
||
| 48 | parent::tearDown();
|
||
| 49 | unset($this->Moo); |
||
| 50 | } |
||
| 51 | |||
| 52 | /**
|
||
| 53 | * test selector method
|
||
| 54 | *
|
||
| 55 | * @return void
|
||
| 56 | */
|
||
| 57 | public function testSelector() { |
||
| 58 | $result = $this->Moo->get('#content'); |
||
| 59 | $this->assertEquals($this->Moo, $result); |
||
| 60 | $this->assertEquals($this->Moo->selection, '$("content")'); |
||
| 61 | |||
| 62 | $result = $this->Moo->get('a .remove'); |
||
| 63 | $this->assertEquals($this->Moo, $result); |
||
| 64 | $this->assertEquals($this->Moo->selection, '$$("a .remove")'); |
||
| 65 | |||
| 66 | $result = $this->Moo->get('document'); |
||
| 67 | $this->assertEquals($this->Moo, $result); |
||
| 68 | $this->assertEquals($this->Moo->selection, "$(document)"); |
||
| 69 | |||
| 70 | $result = $this->Moo->get('window'); |
||
| 71 | $this->assertEquals($this->Moo, $result); |
||
| 72 | $this->assertEquals($this->Moo->selection, "$(window)"); |
||
| 73 | |||
| 74 | $result = $this->Moo->get('ul'); |
||
| 75 | $this->assertEquals($this->Moo, $result); |
||
| 76 | $this->assertEquals($this->Moo->selection, '$$("ul")'); |
||
| 77 | |||
| 78 | $result = $this->Moo->get('#some_long-id.class'); |
||
| 79 | $this->assertEquals($this->Moo, $result); |
||
| 80 | $this->assertEquals($this->Moo->selection, '$$("#some_long-id.class")'); |
||
| 81 | } |
||
| 82 | |||
| 83 | /**
|
||
| 84 | * test event binding
|
||
| 85 | *
|
||
| 86 | * @return void
|
||
| 87 | */
|
||
| 88 | public function testEvent() { |
||
| 89 | $this->Moo->get('#myLink'); |
||
| 90 | $result = $this->Moo->event('click', 'doClick', array('wrap' => false)); |
||
| 91 | $expected = '$("myLink").addEvent("click", doClick);'; |
||
| 92 | $this->assertEquals($expected, $result); |
||
| 93 | |||
| 94 | $result = $this->Moo->event('click', 'this.setStyle("display", "");', array('stop' => false)); |
||
| 95 | $expected = '$("myLink").addEvent("click", function (event) {this.setStyle("display", "");});'; |
||
| 96 | $this->assertEquals($expected, $result); |
||
| 97 | |||
| 98 | $result = $this->Moo->event('click', 'this.setStyle("display", "none");'); |
||
| 99 | $expected = "\$(\"myLink\").addEvent(\"click\", function (event) {event.stop();\nthis.setStyle(\"display\", \"none\");});"; |
||
| 100 | $this->assertEquals($expected, $result); |
||
| 101 | } |
||
| 102 | |||
| 103 | /**
|
||
| 104 | * test dom ready event creation
|
||
| 105 | *
|
||
| 106 | * @return void
|
||
| 107 | */
|
||
| 108 | public function testDomReady() { |
||
| 109 | $result = $this->Moo->domReady('foo.name = "bar";'); |
||
| 110 | $expected = 'window.addEvent("domready", function (event) {foo.name = "bar";});'; |
||
| 111 | $this->assertEquals($expected, $result); |
||
| 112 | } |
||
| 113 | |||
| 114 | /**
|
||
| 115 | * test Each method
|
||
| 116 | *
|
||
| 117 | * @return void
|
||
| 118 | */
|
||
| 119 | public function testEach() { |
||
| 120 | $this->Moo->get('#foo'); |
||
| 121 | $result = $this->Moo->each('item.setStyle("display", "none");'); |
||
| 122 | $expected = '$("foo").each(function (item, index) {item.setStyle("display", "none");});'; |
||
| 123 | $this->assertEquals($expected, $result); |
||
| 124 | } |
||
| 125 | |||
| 126 | /**
|
||
| 127 | * test Effect generation
|
||
| 128 | *
|
||
| 129 | * @return void
|
||
| 130 | */
|
||
| 131 | public function testEffect() { |
||
| 132 | $this->Moo->get('#foo'); |
||
| 133 | $result = $this->Moo->effect('show'); |
||
| 134 | $expected = '$("foo").setStyle("display", "");'; |
||
| 135 | $this->assertEquals($expected, $result); |
||
| 136 | |||
| 137 | $result = $this->Moo->effect('hide'); |
||
| 138 | $expected = '$("foo").setStyle("display", "none");'; |
||
| 139 | $this->assertEquals($expected, $result); |
||
| 140 | |||
| 141 | $result = $this->Moo->effect('fadeIn'); |
||
| 142 | $expected = '$("foo").fade("in");'; |
||
| 143 | $this->assertEquals($expected, $result); |
||
| 144 | |||
| 145 | $result = $this->Moo->effect('fadeOut'); |
||
| 146 | $expected = '$("foo").fade("out");'; |
||
| 147 | $this->assertEquals($expected, $result); |
||
| 148 | |||
| 149 | $result = $this->Moo->effect('slideIn'); |
||
| 150 | $expected = '$("foo").slide("in");'; |
||
| 151 | $this->assertEquals($expected, $result); |
||
| 152 | |||
| 153 | $result = $this->Moo->effect('slideOut'); |
||
| 154 | $expected = '$("foo").slide("out");'; |
||
| 155 | $this->assertEquals($expected, $result); |
||
| 156 | |||
| 157 | $result = $this->Moo->effect('slideOut', array('speed' => 'fast')); |
||
| 158 | $expected = '$("foo").set("slide", {duration:"short"}).slide("out");'; |
||
| 159 | $this->assertEquals($expected, $result); |
||
| 160 | |||
| 161 | $result = $this->Moo->effect('slideOut', array('speed' => 'slow')); |
||
| 162 | $expected = '$("foo").set("slide", {duration:"long"}).slide("out");'; |
||
| 163 | $this->assertEquals($expected, $result); |
||
| 164 | } |
||
| 165 | |||
| 166 | /**
|
||
| 167 | * Test Request Generation
|
||
| 168 | *
|
||
| 169 | * @return void
|
||
| 170 | */
|
||
| 171 | public function testRequest() { |
||
| 172 | $result = $this->Moo->request(array('controller' => 'posts', 'action' => 'view', 1)); |
||
| 173 | $expected = 'var jsRequest = new Request({url:"\\/posts\\/view\\/1"}).send();'; |
||
| 174 | $this->assertEquals($expected, $result); |
||
| 175 | |||
| 176 | $result = $this->Moo->request('/posts/view/1', array('update' => 'content')); |
||
| 177 | $expected = 'var jsRequest = new Request.HTML({update:"content", url:"\\/posts\\/view\\/1"}).send();'; |
||
| 178 | $this->assertEquals($expected, $result); |
||
| 179 | |||
| 180 | $result = $this->Moo->request('/people/edit/1', array( |
||
| 181 | 'method' => 'post', |
||
| 182 | 'complete' => 'doSuccess', |
||
| 183 | 'error' => 'handleError', |
||
| 184 | 'type' => 'json', |
||
| 185 | 'data' => array('name' => 'jim', 'height' => '185cm'), |
||
| 186 | 'wrapCallbacks' => false |
||
| 187 | )); |
||
| 188 | $expected = 'var jsRequest = new Request.JSON({method:"post", onComplete:doSuccess, onFailure:handleError, url:"\\/people\\/edit\\/1"}).send({"name":"jim","height":"185cm"});'; |
||
| 189 | $this->assertEquals($expected, $result); |
||
| 190 | |||
| 191 | $result = $this->Moo->request('/people/edit/1', array( |
||
| 192 | 'method' => 'post', |
||
| 193 | 'complete' => 'doSuccess', |
||
| 194 | 'update' => '#update-zone', |
||
| 195 | 'wrapCallbacks' => false |
||
| 196 | )); |
||
| 197 | $expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doSuccess, update:"update-zone", url:"\\/people\\/edit\\/1"}).send();'; |
||
| 198 | $this->assertEquals($expected, $result); |
||
| 199 | |||
| 200 | $result = $this->Moo->request('/people/edit/1', array( |
||
| 201 | 'method' => 'post', |
||
| 202 | 'complete' => 'doComplete', |
||
| 203 | 'success' => 'doSuccess', |
||
| 204 | 'error' => 'doFailure', |
||
| 205 | 'before' => 'doBefore', |
||
| 206 | 'update' => 'update-zone', |
||
| 207 | 'wrapCallbacks' => false |
||
| 208 | )); |
||
| 209 | $expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doComplete, onFailure:doFailure, onRequest:doBefore, onSuccess:doSuccess, update:"update-zone", url:"\\/people\\/edit\\/1"}).send();'; |
||
| 210 | $this->assertEquals($expected, $result); |
||
| 211 | |||
| 212 | $result = $this->Moo->request('/people/edit/1', array( |
||
| 213 | 'method' => 'post', |
||
| 214 | 'complete' => 'doComplete', |
||
| 215 | 'success' => 'doSuccess', |
||
| 216 | 'error' => 'doFailure', |
||
| 217 | 'before' => 'doBefore', |
||
| 218 | 'update' => 'update-zone', |
||
| 219 | 'dataExpression' => true, |
||
| 220 | 'data' => '$("foo").toQueryString()', |
||
| 221 | 'wrapCallbacks' => false |
||
| 222 | )); |
||
| 223 | $expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:doComplete, onFailure:doFailure, onRequest:doBefore, onSuccess:doSuccess, update:"update-zone", url:"\\/people\\/edit\\/1"}).send($("foo").toQueryString());'; |
||
| 224 | $this->assertEquals($expected, $result); |
||
| 225 | |||
| 226 | $result = $this->Moo->request('/people/edit/1', array( |
||
| 227 | 'method' => 'post', |
||
| 228 | 'before' => 'doBefore', |
||
| 229 | 'success' => 'doSuccess', |
||
| 230 | 'complete' => 'doComplete', |
||
| 231 | 'update' => '#update-zone', |
||
| 232 | )); |
||
| 233 | $expected = 'var jsRequest = new Request.HTML({method:"post", onComplete:function () {doComplete}, onRequest:function () {doBefore}, onSuccess:function (responseText, responseXML) {doSuccess}, update:"update-zone", url:"\\/people\\/edit\\/1"}).send();'; |
||
| 234 | $this->assertEquals($expected, $result); |
||
| 235 | } |
||
| 236 | |||
| 237 | /**
|
||
| 238 | * test sortable list generation
|
||
| 239 | *
|
||
| 240 | * @return void
|
||
| 241 | */
|
||
| 242 | public function testSortable() { |
||
| 243 | $this->Moo->get('#myList'); |
||
| 244 | $result = $this->Moo->sortable(array( |
||
| 245 | 'distance' => 5, |
||
| 246 | 'containment' => 'parent', |
||
| 247 | 'start' => 'onStart', |
||
| 248 | 'complete' => 'onStop', |
||
| 249 | 'sort' => 'onSort', |
||
| 250 | 'wrapCallbacks' => false |
||
| 251 | )); |
||
| 252 | $expected = 'var jsSortable = new Sortables($("myList"), {constrain:"parent", onComplete:onStop, onSort:onSort, onStart:onStart, snap:5});'; |
||
| 253 | $this->assertEquals($expected, $result); |
||
| 254 | } |
||
| 255 | |||
| 256 | /**
|
||
| 257 | * test drag() method
|
||
| 258 | *
|
||
| 259 | * @return void
|
||
| 260 | */
|
||
| 261 | public function testDrag() { |
||
| 262 | $this->Moo->get('#drag-me'); |
||
| 263 | $result = $this->Moo->drag(array( |
||
| 264 | 'start' => 'onStart', |
||
| 265 | 'drag' => 'onDrag', |
||
| 266 | 'stop' => 'onStop', |
||
| 267 | 'snapGrid' => array(10, 10), |
||
| 268 | 'wrapCallbacks' => false |
||
| 269 | )); |
||
| 270 | $expected = '$("drag-me").makeDraggable({onComplete:onStop, onDrag:onDrag, onStart:onStart, snap:[10,10]});'; |
||
| 271 | $this->assertEquals($expected, $result); |
||
| 272 | } |
||
| 273 | |||
| 274 | /**
|
||
| 275 | * test drop() method with the required drag option missing
|
||
| 276 | *
|
||
| 277 | * @expectedException PHPUnit_Framework_Error_Warning
|
||
| 278 | * @return void
|
||
| 279 | */
|
||
| 280 | public function testDropWithMissingOption() { |
||
| 281 | $this->Moo->get('#drop-me'); |
||
| 282 | $this->Moo->drop(array( |
||
| 283 | 'drop' => 'onDrop', |
||
| 284 | 'leave' => 'onLeave', |
||
| 285 | 'hover' => 'onHover', |
||
| 286 | )); |
||
| 287 | } |
||
| 288 | |||
| 289 | /**
|
||
| 290 | * test drop() method
|
||
| 291 | *
|
||
| 292 | * @return void
|
||
| 293 | */
|
||
| 294 | public function testDrop() { |
||
| 295 | $this->Moo->get('#drop-me'); |
||
| 296 | $result = $this->Moo->drop(array( |
||
| 297 | 'drop' => 'onDrop', |
||
| 298 | 'leave' => 'onLeave', |
||
| 299 | 'hover' => 'onHover', |
||
| 300 | 'drag' => '#my-drag', |
||
| 301 | 'wrapCallbacks' => false |
||
| 302 | )); |
||
| 303 | $expected = '$("my-drag").makeDraggable({droppables:$("drop-me"), onDrop:onDrop, onEnter:onHover, onLeave:onLeave});'; |
||
| 304 | $this->assertEquals($expected, $result); |
||
| 305 | $this->assertEquals($this->Moo->selection, '$("drop-me")'); |
||
| 306 | |||
| 307 | $result = $this->Moo->drop(array( |
||
| 308 | 'drop' => 'onDrop', |
||
| 309 | 'leave' => 'onLeave', |
||
| 310 | 'hover' => 'onHover', |
||
| 311 | 'drag' => '#my-drag', |
||
| 312 | )); |
||
| 313 | $expected = '$("my-drag").makeDraggable({droppables:$("drop-me"), onDrop:function (element, droppable, event) {onDrop}, onEnter:function (element, droppable) {onHover}, onLeave:function (element, droppable) {onLeave}});'; |
||
| 314 | $this->assertEquals($expected, $result); |
||
| 315 | } |
||
| 316 | |||
| 317 | /**
|
||
| 318 | * test slider generation
|
||
| 319 | *
|
||
| 320 | * @return void
|
||
| 321 | */
|
||
| 322 | public function testSlider() { |
||
| 323 | $this->Moo->get('#slider'); |
||
| 324 | $result = $this->Moo->slider(array( |
||
| 325 | 'handle' => '#my-handle', |
||
| 326 | 'complete' => 'onComplete', |
||
| 327 | 'change' => 'onChange', |
||
| 328 | 'direction' => 'horizontal', |
||
| 329 | 'wrapCallbacks' => false |
||
| 330 | )); |
||
| 331 | $expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:onChange, onComplete:onComplete});'; |
||
| 332 | $this->assertEquals($expected, $result); |
||
| 333 | $this->assertEquals($this->Moo->selection, '$("slider")'); |
||
| 334 | |||
| 335 | $this->Moo->get('#slider'); |
||
| 336 | $result = $this->Moo->slider(array( |
||
| 337 | 'handle' => '#my-handle', |
||
| 338 | 'complete' => 'onComplete', |
||
| 339 | 'change' => 'onChange', |
||
| 340 | 'direction' => 'horizontal', |
||
| 341 | 'min' => 10, |
||
| 342 | 'max' => 40, |
||
| 343 | 'wrapCallbacks' => false |
||
| 344 | )); |
||
| 345 | $expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:onChange, onComplete:onComplete, range:[10,40]});'; |
||
| 346 | $this->assertEquals($expected, $result); |
||
| 347 | |||
| 348 | $this->Moo->get('#slider'); |
||
| 349 | $result = $this->Moo->slider(array( |
||
| 350 | 'handle' => '#my-handle', |
||
| 351 | 'complete' => 'complete;', |
||
| 352 | 'change' => 'change;', |
||
| 353 | 'direction' => 'horizontal', |
||
| 354 | )); |
||
| 355 | $expected = 'var jsSlider = new Slider($("slider"), $("my-handle"), {mode:"horizontal", onChange:function (step) {change;}, onComplete:function (event) {complete;}});'; |
||
| 356 | $this->assertEquals($expected, $result); |
||
| 357 | } |
||
| 358 | |||
| 359 | /**
|
||
| 360 | * test the serializeForm implementation.
|
||
| 361 | *
|
||
| 362 | * @return void
|
||
| 363 | */
|
||
| 364 | public function testSerializeForm() { |
||
| 365 | $this->Moo->get('#element'); |
||
| 366 | $result = $this->Moo->serializeForm(array('isForm' => true)); |
||
| 367 | $expected = '$("element").toQueryString();'; |
||
| 368 | $this->assertEquals($expected, $result); |
||
| 369 | |||
| 370 | $result = $this->Moo->serializeForm(array('isForm' => true, 'inline' => true)); |
||
| 371 | $expected = '$("element").toQueryString()'; |
||
| 372 | $this->assertEquals($expected, $result); |
||
| 373 | |||
| 374 | $result = $this->Moo->serializeForm(array('isForm' => false)); |
||
| 375 | $expected = '$($("element").form).toQueryString();'; |
||
| 376 | $this->assertEquals($expected, $result); |
||
| 377 | |||
| 378 | $result = $this->Moo->serializeForm(array('isForm' => false, 'inline' => true)); |
||
| 379 | $expected = '$($("element").form).toQueryString()'; |
||
| 380 | $this->assertEquals($expected, $result); |
||
| 381 | } |
||
| 382 | } |