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

https://github.com/shama/cakephp · PHP · 340 lines · 144 code · 46 blank · 150 comment · 1 complexity · e261087de0bf0e2f3a9fec3b56f66afe MD5 · raw file

  1. <?php
  2. /**
  3. * ObjectTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html 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. namespace Cake\Test\TestCase\Core;
  20. use Cake\Core\App;
  21. use Cake\Core\Configure;
  22. use Cake\Core\Object;
  23. use Cake\Core\Plugin;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\Fixture\TestModel;
  26. use Cake\TestSuite\TestCase;
  27. /**
  28. * TestObject class
  29. *
  30. * @package Cake.Test.Case.Core
  31. */
  32. class TestObject extends Object {
  33. /**
  34. * firstName property
  35. *
  36. * @var string 'Joel'
  37. */
  38. public $firstName = 'Joel';
  39. /**
  40. * lastName property
  41. *
  42. * @var string 'Moss'
  43. */
  44. public $lastName = 'Moss';
  45. /**
  46. * methodCalls property
  47. *
  48. * @var array
  49. */
  50. public $methodCalls = array();
  51. /**
  52. * emptyMethod method
  53. *
  54. * @return void
  55. */
  56. public function emptyMethod() {
  57. $this->methodCalls[] = 'emptyMethod';
  58. }
  59. /**
  60. * oneParamMethod method
  61. *
  62. * @param mixed $param
  63. * @return void
  64. */
  65. public function oneParamMethod($param) {
  66. $this->methodCalls[] = array('oneParamMethod' => array($param));
  67. }
  68. /**
  69. * twoParamMethod method
  70. *
  71. * @param mixed $param
  72. * @param mixed $paramTwo
  73. * @return void
  74. */
  75. public function twoParamMethod($param, $paramTwo) {
  76. $this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
  77. }
  78. /**
  79. * threeParamMethod method
  80. *
  81. * @param mixed $param
  82. * @param mixed $paramTwo
  83. * @param mixed $paramThree
  84. * @return void
  85. */
  86. public function threeParamMethod($param, $paramTwo, $paramThree) {
  87. $this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
  88. }
  89. /**
  90. * fourParamMethod method
  91. *
  92. * @param mixed $param
  93. * @param mixed $paramTwo
  94. * @param mixed $paramThree
  95. * @param mixed $paramFour
  96. * @return void
  97. */
  98. public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
  99. $this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
  100. }
  101. /**
  102. * fiveParamMethod method
  103. *
  104. * @param mixed $param
  105. * @param mixed $paramTwo
  106. * @param mixed $paramThree
  107. * @param mixed $paramFour
  108. * @param mixed $paramFive
  109. * @return void
  110. */
  111. public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
  112. $this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
  113. }
  114. /**
  115. * crazyMethod method
  116. *
  117. * @param mixed $param
  118. * @param mixed $paramTwo
  119. * @param mixed $paramThree
  120. * @param mixed $paramFour
  121. * @param mixed $paramFive
  122. * @param mixed $paramSix
  123. * @param mixed $paramSeven
  124. * @return void
  125. */
  126. public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
  127. $this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
  128. }
  129. /**
  130. * methodWithOptionalParam method
  131. *
  132. * @param mixed $param
  133. * @return void
  134. */
  135. public function methodWithOptionalParam($param = null) {
  136. $this->methodCalls[] = array('methodWithOptionalParam' => array($param));
  137. }
  138. /**
  139. * undocumented function
  140. *
  141. * @return void
  142. */
  143. public function set($properties = array()) {
  144. return parent::_set($properties);
  145. }
  146. }
  147. /**
  148. * ObjectTestModel class
  149. *
  150. * @package Cake.Test.Case.Core
  151. */
  152. class ObjectTestModel extends TestModel {
  153. public $useTable = false;
  154. public $name = 'ObjectTestModel';
  155. }
  156. /**
  157. * Object Test class
  158. *
  159. * @package Cake.Test.Case.Core
  160. */
  161. class ObjectTest extends TestCase {
  162. /**
  163. * fixtures
  164. *
  165. * @var string
  166. */
  167. public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
  168. /**
  169. * setUp method
  170. *
  171. * @return void
  172. */
  173. public function setUp() {
  174. parent::setUp();
  175. $this->object = new TestObject();
  176. Configure::write('App.namespace', 'TestApp');
  177. Configure::write('Security.salt', 'not-the-default');
  178. Configure::write('Security.cipherSeed', '123456');
  179. }
  180. /**
  181. * tearDown method
  182. *
  183. * @return void
  184. */
  185. public function tearDown() {
  186. parent::tearDown();
  187. Plugin::unload();
  188. unset($this->object);
  189. }
  190. /**
  191. * testLog method
  192. *
  193. * @return void
  194. */
  195. public function testLog() {
  196. if (file_exists(LOGS . 'error.log')) {
  197. unlink(LOGS . 'error.log');
  198. }
  199. $this->assertTrue($this->object->log('Test warning 1'));
  200. $this->assertTrue($this->object->log(array('Test' => 'warning 2')));
  201. $result = file(LOGS . 'error.log');
  202. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
  203. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
  204. $this->assertRegExp('/^\($/', $result[2]);
  205. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  206. $this->assertRegExp('/^\)$/', $result[4]);
  207. unlink(LOGS . 'error.log');
  208. $this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
  209. $this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
  210. $result = file(LOGS . 'error.log');
  211. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
  212. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
  213. $this->assertRegExp('/^\($/', $result[2]);
  214. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  215. $this->assertRegExp('/^\)$/', $result[4]);
  216. unlink(LOGS . 'error.log');
  217. }
  218. /**
  219. * testSet method
  220. *
  221. * @return void
  222. */
  223. public function testSet() {
  224. $this->object->set('a string');
  225. $this->assertEquals('Joel', $this->object->firstName);
  226. $this->object->set(array('firstName'));
  227. $this->assertEquals('Joel', $this->object->firstName);
  228. $this->object->set(array('firstName' => 'Ashley'));
  229. $this->assertEquals('Ashley', $this->object->firstName);
  230. $this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
  231. $this->assertEquals('Joel', $this->object->firstName);
  232. $this->assertEquals('Moose', $this->object->lastName);
  233. }
  234. /**
  235. * testToString method
  236. *
  237. * @return void
  238. */
  239. public function testToString() {
  240. $result = strtolower($this->object->toString());
  241. $this->assertEquals(strtolower(__NAMESPACE__) . '\testobject', $result);
  242. }
  243. /**
  244. * testMethodDispatching method
  245. *
  246. * @return void
  247. */
  248. public function testMethodDispatching() {
  249. $this->object->emptyMethod();
  250. $expected = array('emptyMethod');
  251. $this->assertSame($this->object->methodCalls, $expected);
  252. $this->object->oneParamMethod('Hello');
  253. $expected[] = array('oneParamMethod' => array('Hello'));
  254. $this->assertSame($this->object->methodCalls, $expected);
  255. $this->object->twoParamMethod(true, false);
  256. $expected[] = array('twoParamMethod' => array(true, false));
  257. $this->assertSame($this->object->methodCalls, $expected);
  258. $this->object->threeParamMethod(true, false, null);
  259. $expected[] = array('threeParamMethod' => array(true, false, null));
  260. $this->assertSame($this->object->methodCalls, $expected);
  261. $this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
  262. $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
  263. $this->assertSame($this->object->methodCalls, $expected);
  264. $this->object = new TestObject();
  265. $this->assertSame($this->object->methodCalls, array());
  266. $this->object->dispatchMethod('emptyMethod');
  267. $expected = array('emptyMethod');
  268. $this->assertSame($this->object->methodCalls, $expected);
  269. $this->object->dispatchMethod('oneParamMethod', array('Hello'));
  270. $expected[] = array('oneParamMethod' => array('Hello'));
  271. $this->assertSame($this->object->methodCalls, $expected);
  272. $this->object->dispatchMethod('twoParamMethod', array(true, false));
  273. $expected[] = array('twoParamMethod' => array(true, false));
  274. $this->assertSame($this->object->methodCalls, $expected);
  275. $this->object->dispatchMethod('threeParamMethod', array(true, false, null));
  276. $expected[] = array('threeParamMethod' => array(true, false, null));
  277. $this->assertSame($this->object->methodCalls, $expected);
  278. $this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
  279. $expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
  280. $this->assertSame($this->object->methodCalls, $expected);
  281. $this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
  282. $expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
  283. $this->assertSame($this->object->methodCalls, $expected);
  284. $this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
  285. $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
  286. $this->assertSame($this->object->methodCalls, $expected);
  287. $this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
  288. $expected[] = array('methodWithOptionalParam' => array("Hello"));
  289. $this->assertSame($this->object->methodCalls, $expected);
  290. $this->object->dispatchMethod('methodWithOptionalParam');
  291. $expected[] = array('methodWithOptionalParam' => array(null));
  292. $this->assertSame($this->object->methodCalls, $expected);
  293. }
  294. }