ObjectTest.php 8.9 KB

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