/
ObjectTest.php
673 lines (579 loc) · 17.7 KB
/
ObjectTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
<?php
/**
* ObjectTest file
*
* PHP 5
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Core
* @since CakePHP(tm) v 1.2.0.5432
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
App::uses('Object', 'Core');
App::uses('Router', 'Routing');
App::uses('Controller', 'Controller');
App::uses('Model', 'Model');
/**
* RequestActionPost class
*
* @package Cake.Test.Case.Core
*/
class RequestActionPost extends CakeTestModel {
/**
* name property
*
* @var string 'ControllerPost'
*/
public $name = 'RequestActionPost';
/**
* useTable property
*
* @var string 'posts'
*/
public $useTable = 'posts';
}
/**
* RequestActionController class
*
* @package Cake.Test.Case.Core
*/
class RequestActionController extends Controller {
/**
* uses property
*
* @var array
* @access public
*/
public $uses = array('RequestActionPost');
/**
* test_request_action method
*
* @access public
* @return void
*/
public function test_request_action() {
return 'This is a test';
}
/**
* another_ra_test method
*
* @param mixed $id
* @param mixed $other
* @access public
* @return void
*/
public function another_ra_test($id, $other) {
return $id + $other;
}
/**
* normal_request_action method
*
* @return void
*/
public function normal_request_action() {
return 'Hello World';
}
/**
* returns $this->here
*
* @return void
*/
public function return_here() {
return $this->here;
}
/**
* paginate_request_action method
*
* @return void
*/
public function paginate_request_action() {
$data = $this->paginate();
return true;
}
/**
* post pass, testing post passing
*
* @return array
*/
public function post_pass() {
return $this->request->data;
}
/**
* test param passing and parsing.
*
* @return array
*/
public function params_pass() {
return $this->request;
}
public function param_check() {
$this->autoRender = false;
$content = '';
if (isset($this->request->params[0])) {
$content = 'return found';
}
$this->response->body($content);
}
}
/**
* TestObject class
*
* @package Cake.Test.Case.Core
*/
class TestObject extends Object {
/**
* firstName property
*
* @var string 'Joel'
*/
public $firstName = 'Joel';
/**
* lastName property
*
* @var string 'Moss'
*/
public $lastName = 'Moss';
/**
* methodCalls property
*
* @var array
*/
public $methodCalls = array();
/**
* emptyMethod method
*
* @return void
*/
public function emptyMethod() {
$this->methodCalls[] = 'emptyMethod';
}
/**
* oneParamMethod method
*
* @param mixed $param
* @return void
*/
public function oneParamMethod($param) {
$this->methodCalls[] = array('oneParamMethod' => array($param));
}
/**
* twoParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @return void
*/
public function twoParamMethod($param, $paramTwo) {
$this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
}
/**
* threeParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @return void
*/
public function threeParamMethod($param, $paramTwo, $paramThree) {
$this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
}
/**
* fourParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @param mixed $paramFour
* @return void
*/
public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
$this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
}
/**
* fiveParamMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @param mixed $paramFour
* @param mixed $paramFive
* @return void
*/
public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
$this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
}
/**
* crazyMethod method
*
* @param mixed $param
* @param mixed $paramTwo
* @param mixed $paramThree
* @param mixed $paramFour
* @param mixed $paramFive
* @param mixed $paramSix
* @param mixed $paramSeven
* @return void
*/
public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
$this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
}
/**
* methodWithOptionalParam method
*
* @param mixed $param
* @return void
*/
public function methodWithOptionalParam($param = null) {
$this->methodCalls[] = array('methodWithOptionalParam' => array($param));
}
/**
* undocumented function
*
* @return void
*/
public function set($properties = array()) {
return parent::_set($properties);
}
}
/**
* ObjectTestModel class
*
* @package Cake.Test.Case.Core
*/
class ObjectTestModel extends CakeTestModel {
public $useTable = false;
public $name = 'ObjectTestModel';
}
/**
* Object Test class
*
* @package Cake.Test.Case.Core
*/
class ObjectTest extends CakeTestCase {
/**
* fixtures
*
* @var string
*/
public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->object = new TestObject();
}
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
CakePlugin::unload();
unset($this->object);
}
/**
* testLog method
*
* @return void
*/
public function testLog() {
if (file_exists(LOGS . 'error.log')) {
unlink(LOGS . 'error.log');
}
$this->assertTrue($this->object->log('Test warning 1'));
$this->assertTrue($this->object->log(array('Test' => 'warning 2')));
$result = file(LOGS . 'error.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
$this->assertRegExp('/^\($/', $result[2]);
$this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
$this->assertRegExp('/^\)$/', $result[4]);
unlink(LOGS . 'error.log');
$this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
$this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
$result = file(LOGS . 'error.log');
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
$this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
$this->assertRegExp('/^\($/', $result[2]);
$this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
$this->assertRegExp('/^\)$/', $result[4]);
unlink(LOGS . 'error.log');
}
/**
* testSet method
*
* @return void
*/
public function testSet() {
$this->object->set('a string');
$this->assertEquals('Joel', $this->object->firstName);
$this->object->set(array('firstName'));
$this->assertEquals('Joel', $this->object->firstName);
$this->object->set(array('firstName' => 'Ashley'));
$this->assertEquals('Ashley', $this->object->firstName);
$this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
$this->assertEquals('Joel', $this->object->firstName);
$this->assertEquals('Moose', $this->object->lastName);
}
/**
* testToString method
*
* @return void
*/
public function testToString() {
$result = strtolower($this->object->toString());
$this->assertEquals('testobject', $result);
}
/**
* testMethodDispatching method
*
* @return void
*/
public function testMethodDispatching() {
$this->object->emptyMethod();
$expected = array('emptyMethod');
$this->assertSame($this->object->methodCalls, $expected);
$this->object->oneParamMethod('Hello');
$expected[] = array('oneParamMethod' => array('Hello'));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->twoParamMethod(true, false);
$expected[] = array('twoParamMethod' => array(true, false));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->threeParamMethod(true, false, null);
$expected[] = array('threeParamMethod' => array(true, false, null));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
$expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
$this->assertSame($this->object->methodCalls, $expected);
$this->object = new TestObject();
$this->assertSame($this->object->methodCalls, array());
$this->object->dispatchMethod('emptyMethod');
$expected = array('emptyMethod');
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('oneParamMethod', array('Hello'));
$expected[] = array('oneParamMethod' => array('Hello'));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('twoParamMethod', array(true, false));
$expected[] = array('twoParamMethod' => array(true, false));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('threeParamMethod', array(true, false, null));
$expected[] = array('threeParamMethod' => array(true, false, null));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
$expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
$expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
$expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
$expected[] = array('methodWithOptionalParam' => array("Hello"));
$this->assertSame($this->object->methodCalls, $expected);
$this->object->dispatchMethod('methodWithOptionalParam');
$expected[] = array('methodWithOptionalParam' => array(null));
$this->assertSame($this->object->methodCalls, $expected);
}
/**
* testRequestAction method
*
* @return void
*/
public function testRequestAction() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS)
), App::RESET);
$this->assertNull(Router::getRequest(), 'request stack should be empty.');
$result = $this->object->requestAction('');
$this->assertFalse($result);
$result = $this->object->requestAction('/request_action/test_request_action');
$expected = 'This is a test';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(FULL_BASE_URL . '/request_action/test_request_action');
$expected = 'This is a test';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/request_action/another_ra_test/2/5');
$expected = 7;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/tests_apps/index', array('return'));
$expected = 'This is the TestsAppsController index view ';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/tests_apps/some_method');
$expected = 5;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/request_action/paginate_request_action');
$this->assertTrue($result);
$result = $this->object->requestAction('/request_action/normal_request_action');
$expected = 'Hello World';
$this->assertEquals($expected, $result);
$this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
}
/**
* test requestAction() and plugins.
*
* @return void
*/
public function testRequestActionPlugins() {
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
), App::RESET);
CakePlugin::load('TestPlugin');
Router::reload();
$result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
$expected = 'test plugin index';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
$expected = 'test plugin index';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
);
$expected = 'test plugin index';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/test_plugin/tests/some_method');
$expected = 25;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
);
$expected = 25;
$this->assertEquals($expected, $result);
}
/**
* test requestAction() with arrays.
*
* @return void
*/
public function testRequestActionArray() {
App::build(array(
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
), App::RESET);
CakePlugin::load(array('TestPlugin'));
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'test_request_action')
);
$expected = 'This is a test';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'another_ra_test'),
array('pass' => array('5', '7'))
);
$expected = 12;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'tests_apps', 'action' => 'index'), array('return')
);
$expected = 'This is the TestsAppsController index view ';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
$expected = 5;
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'normal_request_action')
);
$expected = 'Hello World';
$this->assertEquals($expected, $result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'paginate_request_action')
);
$this->assertTrue($result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'paginate_request_action'),
array('pass' => array(5), 'named' => array('param' => 'value'))
);
$this->assertTrue($result);
}
/**
* Test that requestAction() does not forward the 0 => return value.
*
* @return void
*/
public function testRequestActionRemoveReturnParam() {
$result = $this->object->requestAction(
'/request_action/param_check', array('return')
);
$this->assertEquals('', $result, 'Return key was found');
}
/**
* Test that requestAction() is populating $this->params properly
*
* @return void
*/
public function testRequestActionParamParseAndPass() {
$result = $this->object->requestAction('/request_action/params_pass');
$this->assertEquals('request_action/params_pass', $result->url);
$this->assertEquals('request_action', $result['controller']);
$this->assertEquals('params_pass', $result['action']);
$this->assertEquals(null, $result['plugin']);
$result = $this->object->requestAction('/request_action/params_pass/sort:desc/limit:5');
$expected = array('sort' => 'desc', 'limit' => 5,);
$this->assertEquals($expected, $result['named']);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'params_pass'),
array('named' => array('sort' => 'desc', 'limit' => 5))
);
$this->assertEquals($expected, $result['named']);
}
/**
* test that requestAction does not fish data out of the POST
* superglobal.
*
* @return void
*/
public function testRequestActionNoPostPassing() {
$_tmp = $_POST;
$_POST = array('data' => array(
'item' => 'value'
));
$result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
$expected = null;
$this->assertEmpty($result);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'post_pass'),
array('data' => $_POST['data'])
);
$expected = $_POST['data'];
$this->assertEquals($expected, $result);
$result = $this->object->requestAction('/request_action/post_pass');
$expected = $_POST['data'];
$this->assertEquals($expected, $result);
$_POST = $_tmp;
}
/**
* Test requestAction with post data.
*
* @return void
*/
public function testRequestActionPostWithData() {
$data = array(
'Post' => array('id' => 2)
);
$result = $this->object->requestAction(
array('controller' => 'request_action', 'action' => 'post_pass'),
array('data' => $data)
);
$this->assertEquals($data, $result);
$result = $this->object->requestAction(
'/request_action/post_pass',
array('data' => $data)
);
$this->assertEquals($data, $result);
}
}