PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Core/ObjectTest.php

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